I have a dc.js seriesChart using d3.scaleTime()
on the X axis. The data changes dynamically, ranging from a single week to a few years, so I don't define a domain or a range for the scaleTime. Instead I'm using dc.js elasticX(true)
, which I guess calculates them as needed. I'm also using a fake group to remove the empty bins, in order for the X axis to refresh automatically as the user changes the data filters.
Everything works as expected, but if I filter only one week, I get more ticks than I'd like. Notice that I have only one data point, but up to 4 ticks, for each day:
I can use axis.ticks
to generate only one tick per day:
chart.xAxis()
.tickFormat(d3.timeFormat('%Y-%m-%d'))
.ticks(d3.timeDay);
But then it creates one tick per day. If I select a whole year, that's 365 of them overlapping.
I can't use .ticks(n)
to always generate n
ticks, because that would bring back the problem I had in the first place: duplicate ticks when the number I set is greater than the number of selected days. And I can't use .tickValues([ ])
either, because the values are dynamic.
Is there any way to tell d3 to generate ticks for a given interval, but limited to a max number of ticks? So in my case, it would actually be "daily if it fits, or every whatever days otherwise".
The closest thing I've found is this answer, where they change the interval dynamically depending on the range. However, since I'm letting dc.js and Crossfilter deal with the data and filtering, it wouldn't be easy to calculate that. I guess I would have to attach a renderlet event to the chart, and use xAxisMin()
and xAxisMax()
? I think it could work, but it wouldn't look too good. Is there an easier way to do it?