简体   繁体   English

如何在d3.js时间刻度中指定tickValues?

[英]How specify tickValues in d3.js time scale?

I am using d3 and in my chart I have a scale that is a time scale in UTC format. 我正在使用d3,在我的图表中,我的刻度是UTC格式的时间刻度。

xScale  = d3.time.scale.utc().range([0,  chartWidth]);

I need to specify the ticks dynamically and change the domain dynamically to avoid clutter in the tick label. 我需要动态指定刻度线,并动态更改域,以避免刻度线标签混乱。

I was calculating ticks based on the domain and setting the ticks to its closest multiple of 5. However, I noticed a problem where sometimes the tick positions were not consistent while changing the the domain. 我正在基于域计算滴答声,并将滴答声设置为最接近的5的倍数。但是,我注意到一个问题,即有时在更改域时滴答声位置不一致。 See image below: 见下图:

在此处输入图片说明

Looking in some forum, I saw that it is possible to set the tick values by the function tickValues(d3.range(starting value, end value, interval)) . 在某个论坛上查看时,我看到可以通过函数tickValues(d3.range(starting value, end value, interval))来设置tickValues(d3.range(starting value, end value, interval)) See this post by mbostock . 请参阅mbostock的这篇文章

Now I am not sure--how can I do the same for my time scale? 现在我不确定-如何在我的时间范围内做同样的事情? If I want to calculate my own tick values for my xAxis (which uses a time scale in UTC) how can I do it? 如果我想为自己的xAxis(在UTC中使用时间标度)计算自己的刻度值,该怎么办?

Thanks. 谢谢。

You want to force ticks of every N minutes? 您要每N分钟强制滴答答答吗? or N [time units]? 或N [时间单位]?

You can use: 您可以使用:

var startDate = new Date(2016, 0, 1, 2, 0, 0),
    endDate = new Date(2016, 0, 1, 4, 0, 0),
    millisecondsBetweenTicks = 300000; //<-- 5 minutes

var x = d3.time.scale.utc()
    .domain([startDate, endDate])
    .range([0, width]);

var xAxis = d3.svg.axis()
    .scale(x)
    .tickValues(d3.range(startDate, endDate, millisecondsBetweenTicks )
                  .map(function(d){ return new Date(d) }) //<-- remap to dates
    );

Full example: 完整示例:

 <!DOCTYPE html> <meta charset="utf-8"> <style> .axis text { font: 10px sans-serif; } .axis line, .axis path { fill: none; stroke: #000; shape-rendering: crispEdges; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 250, right: 40, bottom: 250, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom, startDate = new Date(2016, 0, 1, 2, 0, 0), endDate = new Date(2016, 0, 1, 4, 0, 0); var x = d3.time.scale.utc() .domain([startDate, endDate]) .range([0, width]); var xAxis = d3.svg.axis() .scale(x) .tickValues(d3.range(startDate, endDate, 300000) .map(function(d){ return new Date(d) }) ); 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 + ")"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll("text") .attr("y", 0) .attr("x", 9) .attr("dy", ".35em") .attr("transform", "rotate(90)") .style("text-anchor", "start"); </script> 

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

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