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

javascript - Why does my d3 chart's x-axis include the first of every month as a tick even though I set the tick interva

programmeradmin0浏览0评论

I am new to d3 but I have set the tick interval for my x-axis to every 7 days using d3.timeDay.every(7). However when my graph is created, I also see a tick mark added for the first of every month. For example, my graph shows ticks at 10/1, 10/8, 10/15, 10/22, 10/29, and 11/1, even though 11/1 is not 7 days after 10/29. If I expand this range to include multiple months, I see a tick at the first of every month.

I have included my code for this axis below. What might be causing the first of each month to be displayed as a tick?

  const xScale = d3.scaleTime().range([0, innerWidth]);
  const tickAmount = d3.timeDay.every(7);

  chart.append("g")
    .classed('x-axis', true)
    .attr("transform", "translate(0," + innerHeight + ")")
    .call(d3.axisBottom(xScale)
    .ticks(tickAmount)
    .tickSize(-innerHeight)
    .tickFormat(d3.timeFormat('%m/%d/%y')));

I am new to d3 but I have set the tick interval for my x-axis to every 7 days using d3.timeDay.every(7). However when my graph is created, I also see a tick mark added for the first of every month. For example, my graph shows ticks at 10/1, 10/8, 10/15, 10/22, 10/29, and 11/1, even though 11/1 is not 7 days after 10/29. If I expand this range to include multiple months, I see a tick at the first of every month.

I have included my code for this axis below. What might be causing the first of each month to be displayed as a tick?

  const xScale = d3.scaleTime().range([0, innerWidth]);
  const tickAmount = d3.timeDay.every(7);

  chart.append("g")
    .classed('x-axis', true)
    .attr("transform", "translate(0," + innerHeight + ")")
    .call(d3.axisBottom(xScale)
    .ticks(tickAmount)
    .tickSize(-innerHeight)
    .tickFormat(d3.timeFormat('%m/%d/%y')));
Share Improve this question edited Mar 8, 2018 at 17:02 user95227 asked Mar 8, 2018 at 16:51 user95227user95227 1,9633 gold badges22 silver badges39 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Try const tickAmount = d3.timeWeek.every(1); instead:

Note that for some intervals, the resulting dates may not be uniformly-spaced; d3.timeDay’s parent interval is d3.timeMonth, and thus the interval number resets at the start of each month. If step is not valid, returns null. If step is one, returns this interval. from the d3 docs

You can also use .tickFormat(function(d, i) { return d; }) to filter out given ticks from display if you like (e.g. exclude first tick: return i>0 ? d : ''), though this is more hacky.

Found a better way to do this from d3 issue tracker:

d3.axisBottom(x)
  .ticks(d3.timeDay.filter(d=>d3.timeDay.count(0, d) % N === 0))
  .tickFormat(d3.timeFormat('%m/%d/%y'))
  .tickSizeOuter(0)

This will give you consistent ticks every N-th day without the months repeating. That a tick exists does not mean a point exists at that date in your dataset, but they are scaled correctly over the date range.

Here's a demo: https://beta.observablehq./@thmsdnnr/d3-ticks-every-7-or-10-days

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论