简体   繁体   English

D3 x轴刻度格式和刻度值问题

[英]D3 x-axis tick format & tick value issues

i am trying to format date-time ticks for the x-axis but i am not able to achieve what i want to. 我正在尝试为X轴设置日期时间刻度,但是我无法实现我想要的功能。

 var margin = { top: 20, right: 30, bottom: 30, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var z = d3.scale.category20c(); 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 + ")"); var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ"); var data = [{ data: [ ["2016-01-28T15:45:26Z", 5.0], ["2016-01-30T15:45:26Z", 0.0], ["2016-02-01T15:45:26Z", 0.0], ["2016-02-03T15:45:26Z", 0.0], ["2016-02-05T15:45:26Z", 0.0], ["2016-02-07T15:45:26Z", 0.0], ["2016-02-09T15:45:26Z", 0.0], ["2016-02-11T15:45:26Z", 0], ["2016-02-13T15:45:26Z", 0], ["2016-02-15T15:45:26Z", 0], ["2016-02-17T15:45:26Z", 0], ["2016-02-19T15:45:26Z", 0], ["2016-02-21T15:45:26Z", 0], ["2016-02-23T15:45:26Z", 0], ["2016-02-25T15:45:26Z", 0] ], label: "a" }] var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("monotone") .x(function(d) { return x(parseDate.parse(d[0])); }) .y(function(d) { return y(d[1]); }); var ary = []; data.forEach(function(d) { ary.push(d.data); }); x.domain(d3.extent(d3.merge(ary), function(d) { return parseDate.parse(d[0]); })); y.domain([ d3.min(data, function(c) { return d3.min(c.data, function(v) { return v[1]; }); }), d3.max(data, function(c) { return d3.max(c.data, function(v) { return v[1]; }); }) ]); function calc(val) { var day = Math.floor((new Date() - val) / (24 * 60 * 60 * 1000)); if (day === 0) { return 'today'; } else { return '-' + day + 'D'; } } xAxis.tickFormat(function(d) { console.log("========="); console.log(d); console.log("========="); return calc(d); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); var series = svg.selectAll(".series") .data(data) .enter().append("g") .attr("class", "series"); series.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.data); }) .style("stroke", function(d, i) { return z(i); }); 
 text.inner-circle { font-weight: 400; font-size: 12px; text-transform: uppercase; } text.inner-text { font-weight: 400; font-size: 36px; font-family: 'Metric Regular', 'Metric'; text-align: center; font-style: normal; text-transform: uppercase; } path { stroke: steelblue; stroke-width: 2; fill: none; } .axis path, .axis line { fill: none; stroke: grey; stroke-width: 2; shape-rendering: crispEdges; } .grid .tick { stroke: lightgrey; stroke-opacity: 0.7; shape-rendering: crispEdges; } .grid path { stroke-width: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

When i printing x.domain() , i am getting output as [Thu Jan 28 2016 15:45:26 GMT+0530 (India Standard Time), Thu Feb 25 2016 15:45:26 GMT+0530 (India Standard Time)] which are correct min & max values according to data with correct time. 当我打印x.domain() ,我的输出为[Thu Jan 28 2016 15:45:26 GMT+0530 (India Standard Time), Thu Feb 25 2016 15:45:26 GMT+0530 (India Standard Time)]是根据正确时间的数据正确的最小值和最大值。

But when below mentioned snippet of code runs, it doesn't starts passing values from Fri Jan 29 2016 00:00:00 GMT+0530 (India Standard Time) instead of Thu Jan 28 2016 15:45:26 GMT+0530 (India Standard Time) - 但是,当下面提到的代码片段运行时,它不会开始传递从Fri Jan 29 2016 00:00:00 GMT+0530 (India Standard Time)开始的值,而不是从Thu Jan 28 2016 15:45:26 GMT+0530 (India Standard Time) Fri Jan 29 2016 00:00:00 GMT+0530 (India Standard Time)开始传递值Thu Jan 28 2016 15:45:26 GMT+0530 (India Standard Time) -

xAxis.tickFormat(function(d) {
console.log("=========");
console.log(d);
console.log("=========");
return calc(d);
});

So when the above snippet runs, it give me these values of d : Console Output 因此,当上面的代码段运行时,它为我提供了d这些值:Console Output

=========
Fri Jan 29 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Sun Jan 31 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Mon Feb 01 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Wed Feb 03 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Fri Feb 05 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Sun Feb 07 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Tue Feb 09 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Thu Feb 11 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Sat Feb 13 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Mon Feb 15 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Wed Feb 17 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Fri Feb 19 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Sun Feb 21 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Tue Feb 23 2016 00:00:00 GMT+0530 (India Standard Time)
=========
=========
Thu Feb 25 2016 00:00:00 GMT+0530 (India Standard Time)
=========

But i want code to pass date-time values that are mentioned in the input without changing them. 但是我希望代码传递输入中提到的日期时间值而不更改它们。

Please help me. 请帮我。

The standard behaviour of axis is to use regularly-spaced ticks with nice round values. axis的标准行为是使用带有良好舍入值的规则间隔刻度。

Since you know precisely where you want your ticks; 因为您确切地知道要在哪里打勾; then axis.tickValues([values]) is your friend. 然后axis.tickValues([values])是您的朋友。 Extract the list of date-times from your data and give them to your xAxis through this function. 从数据中提取日期时间列表,然后通过此函数将其提供给xAxis

Edit: to get your array of values, the following lines should do the work 编辑:要获取值的数组,以下几行应该可以完成工作

myTickValues = ary[0].map(function(d){return parseDate.parse(d[0])})
xAxis.tickValues(myTickValues);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM