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

How to auto-scale a linear chart for a big set of data

Open ponyczek opened this issue 8 years ago • 2 comments

Good Morning,

I was wondering if there is a possibility to auto-scale my labels on the x and y axis.

The problem I have a relatively big amount of data that I want to display on the linear chart. My dataset looks like so:

 [{label: 'Mon Feb 19 2018 21:02:38 GMT+0000 (GMT)', value: 0.2 },
  {label: 'Mon Feb 19 2018 21:02:39 GMT+0000 (GMT)', value: 0.1 },
  {label: 'Mon Feb 19 2018 21:02:40 GMT+0000 (GMT)', value: 0.5 },
  {label: 'Mon Feb 19 2018 21:02:41 GMT+0000 (GMT)', value: -0.3 },
  {label: 'Mon Feb 19 2018 21:02:52 GMT+0000 (GMT)', value: -0.3 }]

I am able to create a chart from this data that contains 5 labels on XAxis and 5 records on YAxis. Easy peasy.

So my records happen in a few seconds one after another and this is very easy to put on the chart. However, I cumulate the data over time so my first time can be:

{label: 'Mon Feb 19 2018 21:02:38 GMT+0000 (GMT)', value: 0.2 }, ...
{label: 'Mon Feb 26 2018 11:02:38 GMT+0000 (GMT)', value: 0.2 }]

Therefore, the data now on the graph should be auto-scaled showing properly the line over that from the first date to the last date from the period with some labels in between.

The only ways that I came up with were focusing on serialising my data, but this is a very hard and tedious way. I do not want to be re-inventing the wheel if that could be handled within the library. From what I have seen so far this is a very powerful library and I think that such thing can be handled if the library is used properly.

I would really appreciate if anyone who has the experience with this library could help me out with writing a code for this task, or point me in the right direction.

Thanks! Adrian

ponyczek avatar Feb 20 '18 09:02 ponyczek

@ponyczek have you had a look at the suggestedMin and suggestedMax settings of the linear axis? http://www.chartjs.org/docs/latest/axes/cartesian/linear.html#axis-range-settings

They might help with what you want to do for the Y axis. You could set suggestedMin to your minimum data value over that time and suggestedMax to the data maximum

etimberg avatar Feb 20 '18 23:02 etimberg

Please ask questions on StackOverflow: https://stackoverflow.com/questions/tagged/chart.js

benmccann avatar Jun 17 '18 15:06 benmccann