Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

[v-2.0] Linechart x-scale gets confused with a lot of datapoints.

Open stephanarts opened this issue 11 years ago • 14 comments

When the number of datapoints / width ratio is too high, the chart is no longer rendering it correctly.

This is with 60 datapoints that can be rendered correctly: okay-chart

But when the width is reduced, it appears like this: broken-chart

This is the configuration (minus the set of datapoints):

var config = {
    type: 'line',
    data: {
        datasets: [
            {   
                label: "",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(110,110,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [{x:'0',y:'1'}]
            }
        ]
    },
    options: {
        responsive: true,
        scales: {
            xAxes: [{
                type: "time",
                display: true,
                time: {
                    format: 'MM/DD/YYYY HH:mm:SS',
                },  
                scaleLabel: {
                    show: true,
                    labelString: 'Date'
                }   
            }],
            yAxes: [{
                display: true,
                ticks: {
                    beginAtZero: true,
                },  
                scaleLabel: {
                    show: true,
                    labelString: 'value'
                }
            }]
        },
        elements: {
            line: {
                tension: 0.3
            }
        },
    }
};

Edit: Looking at it again made me realise this probably has something to do with the rounding of the ticks to whole hours... maybe it's intended behaviour, but I find it looking odd. Especially because I do have data that could be displayed in the 'blank' area if I wanted to.

stephanarts avatar Oct 31 '15 22:10 stephanarts

I think what happens is that the time scale decides that it can't show 1 hour increments for all the data, and jumps up to 1 day instead of trying 2 hours, 3 hours, etc. Shouldn't be too hard to address

etimberg avatar Oct 31 '15 22:10 etimberg

I'm not sure the approach to take to fix that. Any ideas would be appreciated.

tannerlinsley avatar Oct 31 '15 22:10 tannerlinsley

I have some ideas that in theory don't sound too hard. Will try and design it out.

etimberg avatar Oct 31 '15 22:10 etimberg

It actually goes from 2 minute increments to 1 hour increments.

IMHO 1,5,10,15 30 and 60 minute increments would make more sense for a timeline at this scale.

I am curious what kind of solution you come up with.

stephanarts avatar Nov 01 '15 05:11 stephanarts

We talked a bit about partial ticks. I also really like your suggestion. Making another tick level that is in fact 15 minute increments of an hour. To that we could add more like 6 or 12 hour increments for days, etc. Really anything that is a common partial time unit. @etimberg? This way we wouldn't really need to do partial ticks and instead just create enough specificity levels to accommodate any time range.

tannerlinsley avatar Nov 01 '15 05:11 tannerlinsley

@tannerlinsley I don't think this would be a replacement for partial ticks. The behaviour I showed above would still occur with the smaller time units. You would see an empty graph where you would not expect it.

Closely related to this, I come to think of the following. Maybe implement suggestedMin/suggestedMax for the X-axis? If you want to show a graph from 01:10 to 03:10 but happen to only have data starting at 01:15 and ending at 01:45... how would you do that? (Imagine wanting to show a floating window of the last 2 hours of data, and it's 03:10 now).

stephanarts avatar Nov 01 '15 06:11 stephanarts

There the "to" and "from" properties that limit the min and max of the x scale. Will that not do?

tannerlinsley avatar Nov 01 '15 06:11 tannerlinsley

~~Hmm, it might. Let me try.~~

Yes it would.

stephanarts avatar Nov 01 '15 06:11 stephanarts

It's scale.options.time.to/from I think.

tannerlinsley avatar Nov 01 '15 06:11 tannerlinsley

Oh good.

tannerlinsley avatar Nov 01 '15 06:11 tannerlinsley

I set it like this:

config.options.scales.xAxes[0].time.from = config.data.datasets[0].data[0].x

But that does not override the rounding of the intervals.

stephanarts avatar Nov 01 '15 09:11 stephanarts

@stephanarts I have a PR for this in #1724

etimberg avatar Dec 02 '15 02:12 etimberg

Nice, I hope I have the time to check this this week.

stephanarts avatar Dec 02 '15 16:12 stephanarts

Nice, I love it. I think it works nicely. screen shot 2015-12-06 at 13 19 28

stephanarts avatar Dec 06 '15 12:12 stephanarts