Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

chart.js - Chartjs 2 scaling lots of data points

I'm trying to render a line chart with 24 hours of data (collected every 30 seconds). I can't figure out from the docs how to get it to scale nicely.

The docs say:

When building its ticks, it will automatically calculate the most comfortable unit base on the size of the scale.

But I can't get my line chart to scale "comfortably". I'm not sure it's what they mean, but I get far too many data points to render nicely. So I guess I'm looking for a way to drop data points (using chartjs, not hand rolled).

enter image description here

I'm doing something like this with my options;

const options = {
  spanGaps: false,
  scales: {
    xAxes: [{
      type: 'time',
      time: {
        displayFormats: {
          quarter: 'HH:mm'
        }
      }
    }]
  }
};

Any pointers where to look?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the case that you are still interested in 'dropping data points', you can have a look at the github issue which is recently active again. See link

https://github.com/chartjs/chartjs-plugin-zoom/issues/75.

There you can find a plugin written earlier this year in js for filtering datasets to include only those which are visible; it can be customized with your own filtering rule. I personally filter the dataset on my backend prior to sending it to javascript though....

Else, if you want to render even more time series data in a 'scalable' way, why not use the Chartjs time series/financial line plot ? You can check it out at the official documentation here ...

http://www.chartjs.org/samples/latest/scales/time/financial.html ... Which is a variant of the line chart ; check out the source code. Caveat is, use it as is (the source example) if you don't mind not having interactive tooltips.

Nonetheless in addition to xnakos's comment , a final thought (a bit of extra that crossed my mind..) on performance renders for even larger data sets, you may experiment with the combination of setting smaller pointRadius (non zero), and not rendering the lines instead. I have found this trick useful as well for allot of points on screen. In your datasets, just set showLine = 'false' and see how to make it work for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...