最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NVD3 - configuring ticks on axis - Stack Overflow

programmeradmin1浏览0评论

I have a nvd3 line chart which displays a time series and can't get the ticks on the x axis right.

For longer time spans, it works as expected. But for shorter time spans (here: 12/31/05 to 01/01/06), the same date is displayed for multiple ticks:

Please have a look at the code for this chart on JSFiddle

I want the chart to only display ticks at data points, and not in between. Is that possible with a line chart? From my understanding, it is possible with d3, but I can't figure out if this functionality is exposed by nvd3.

I've tried explicitly setting the number of ticks with chart.xAxis.ticks() without success. The only thing that has any effect is explicitly setting the tick values with chart.xAxis.tickValues([...]), but I would prefer not having to calculate them myself.

I have a nvd3 line chart which displays a time series and can't get the ticks on the x axis right.

For longer time spans, it works as expected. But for shorter time spans (here: 12/31/05 to 01/01/06), the same date is displayed for multiple ticks:

Please have a look at the code for this chart on JSFiddle

I want the chart to only display ticks at data points, and not in between. Is that possible with a line chart? From my understanding, it is possible with d3, but I can't figure out if this functionality is exposed by nvd3.

I've tried explicitly setting the number of ticks with chart.xAxis.ticks() without success. The only thing that has any effect is explicitly setting the tick values with chart.xAxis.tickValues([...]), but I would prefer not having to calculate them myself.

Share Improve this question asked Jan 21, 2014 at 16:28 Daniel HepperDaniel Hepper 30k10 gold badges77 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The way to solve this in general is with custom multi-scale time formats. Note that this example itself will not work with NVD3 because it uses an older version of D3, the examples below will though.

The problem in your case is that the ticks aren't "clean" divisions of time and if you apply a multi-scale format, you get something like this. It always shows the more fine-grained format because anything else would involve a loss of precision.

You can however use a simple heuristic to show the date instead of the time if the hour is less than 3, which works reasonably well in your case. See here for an example. The proper way to do this would be to make your ticks clean divisions.

Which brings us to your actual question. There's no other way than to explicitly set .tickValues() for what you want to do, but you can pute the x positions in your data quite easily:

var xvalues = [],
    tmp = data.map(function(e) { 
            return e.values.map(function(d) { return d[0]; });
          });
xvalues.concat.apply(xvalues, tmp);

The code is not the prettiest because it's a nested structure, but fairly straightforward. Using this, you can set your tick values explicitly, full example here.

发布评论

评论列表(0)

  1. 暂无评论