有没有办法以d3时间格式执行操作?我有一个时间序列,我想显示D 0作为“焦点日”,我想让以前的日子被打印为负的日子,得到一个时间轴打印像...,D-2,D-1,D 0,D 1,D 2...我创建了假日期,以便使D 0对应于一年的第一天,然后使用D %j我得到以下结果:D 364,D 365,D 0,D 1注意:这是为了在一个时间序列图中显示时间轴(在超集中),我只能输入一个简单的公式有什么建议吗
sh7euo9m1#
d3-time-format不会有这么具体的东西。为什么不创建自己的格式化程序呢?如果你使用的是时间尺度,那么你可以这样定义你的x轴:
var xAxis = d3.axisBottom(x) .ticks(d3.timeDay.every(4)) // tick every 4 days .tickFormat(function(d){ var diff = Math.round((d-dayZero)/(1000*60*60*24)); return "D" + diff; // calc difference and display });
从here窃取的天数公式的差异。运行代码:
<!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ body { font: 12px Arial; } path { stroke: steelblue; stroke-width: 2; fill: none; } </style> <body> <!-- load the d3.js library --> <script src="https://d3js.org/d3.v4.min.js"></script> <script> // data var data = [{ "date": "2012-05-01T04:00:00.000Z", "close": 58.13 }, { "date": "2012-04-30T04:00:00.000Z", "close": 53.98 }, { "date": "2012-04-27T04:00:00.000Z", "close": 67 }, { "date": "2012-04-26T04:00:00.000Z", "close": 89.7 }, { "date": "2012-04-25T04:00:00.000Z", "close": 99 }, { "date": "2012-04-24T04:00:00.000Z", "close": 130.28 }, { "date": "2012-04-23T04:00:00.000Z", "close": 166.7 }, { "date": "2012-04-20T04:00:00.000Z", "close": 234.98 }, { "date": "2012-04-19T04:00:00.000Z", "close": 345.44 }, { "date": "2012-04-18T04:00:00.000Z", "close": 443.34 }, { "date": "2012-04-17T04:00:00.000Z", "close": 543.7 }, { "date": "2012-04-16T04:00:00.000Z", "close": 580.13 }, { "date": "2012-04-13T04:00:00.000Z", "close": 605.23 }, { "date": "2012-04-12T04:00:00.000Z", "close": 622.77 }, { "date": "2012-04-11T04:00:00.000Z", "close": 626.2 }, { "date": "2012-04-10T04:00:00.000Z", "close": 628.44 }, { "date": "2012-04-09T04:00:00.000Z", "close": 636.23 }, { "date": "2012-04-05T04:00:00.000Z", "close": 633.68 }, { "date": "2012-04-04T04:00:00.000Z", "close": 624.31 }, { "date": "2012-04-03T04:00:00.000Z", "close": 629.32 }, { "date": "2012-04-02T04:00:00.000Z", "close": 618.63 }, { "date": "2012-03-30T04:00:00.000Z", "close": 599.55 }, { "date": "2012-03-29T04:00:00.000Z", "close": 609.86 }, { "date": "2012-03-28T04:00:00.000Z", "close": 617.62 }, { "date": "2012-03-27T04:00:00.000Z", "close": 614.48 }, { "date": "2012-03-26T04:00:00.000Z", "close": 606.98 }]; var dayZero = new Date("2012-04-13T04:00:00.000Z"); data.forEach(function(d){ d.date = new Date(d.date); }); // Set the dimensions of the canvas / graph var margin = { top: 30, right: 20, bottom: 30, left: 50 }, width = 600 - margin.left - margin.right, height = 270 - margin.top - margin.bottom; // Set the ranges var x = d3.scaleTime().range([0, width]); var y = d3.scaleLinear().range([height, 0]); // Define the line var valueline = d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.close); }); // Adds the svg canvas var svg = d3.select("body") .append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // Scale the range of the data x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.close; })]); // Add the valueline path. svg.append("path") .attr("class", "line") .attr("d", valueline(data)); // Define the axes var xAxis = d3.axisBottom(x) .ticks(d3.timeDay.every(4)) .tickFormat(function(d){ var diff = Math.round((d-dayZero)/(1000*60*60*24)); return "D" + diff; }) var yAxis = d3.axisLeft(y) .ticks(5); // Add the X Axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); // Add the Y Axis svg.append("g") .attr("class", "y axis") .call(yAxis); </script> </body>
1条答案
按热度按时间sh7euo9m1#
d3-time-format不会有这么具体的东西。为什么不创建自己的格式化程序呢?如果你使用的是时间尺度,那么你可以这样定义你的x轴:
从here窃取的天数公式的差异。
运行代码: