D3 ticks() does not return value if provided scale has only 1 result

前端 未结 3 1788
借酒劲吻你
借酒劲吻你 2021-01-28 03:35

I have an x-axis that displays the days that my data occurs on. The data is dynamic and sometimes I have data for only 1 day, 2 days, n days, etc.

Here is my code for di

3条回答
  •  一向
    一向 (楼主)
    2021-01-28 04:03

    Can you show how chart.x is set up? Hard to tell without seeing it, but you may be able to fix it by calling chart.x.nice() (see documentation).

    Otherwise, seems like you'll need to manually check the extents of its domain, and adjust them in the case of single day.

    Clarification

    Your code shows how you call range() but not how you call domain(), which is the important one.

    It seems to me to me that if do

    var domain = chart.x.domain()
    console.log domain[0] == domain[1]
    

    you'll see true getting logged whenever the data is for only one day. If so, it means you're dealing with a single point in time rather than a time range. In that case, you'll need to adjust the domain to be a longer range.

    Really hard to know without even seeing an image of what you're working on.

提交回复
热议问题