<tfoot id='N9w4e'></tfoot>
  1. <small id='N9w4e'></small><noframes id='N9w4e'>

      <legend id='N9w4e'><style id='N9w4e'><dir id='N9w4e'><q id='N9w4e'></q></dir></style></legend>
        <bdo id='N9w4e'></bdo><ul id='N9w4e'></ul>
      <i id='N9w4e'><tr id='N9w4e'><dt id='N9w4e'><q id='N9w4e'><span id='N9w4e'><b id='N9w4e'><form id='N9w4e'><ins id='N9w4e'></ins><ul id='N9w4e'></ul><sub id='N9w4e'></sub></form><legend id='N9w4e'></legend><bdo id='N9w4e'><pre id='N9w4e'><center id='N9w4e'></center></pre></bdo></b><th id='N9w4e'></th></span></q></dt></tr></i><div id='N9w4e'><tfoot id='N9w4e'></tfoot><dl id='N9w4e'><fieldset id='N9w4e'></fieldset></dl></div>

    1. 自动删除 Chart.js 中的旧数据点

      Auto-Remove old Datapoints in Chart.js(自动删除 Chart.js 中的旧数据点)

    2. <i id='Eaw77'><tr id='Eaw77'><dt id='Eaw77'><q id='Eaw77'><span id='Eaw77'><b id='Eaw77'><form id='Eaw77'><ins id='Eaw77'></ins><ul id='Eaw77'></ul><sub id='Eaw77'></sub></form><legend id='Eaw77'></legend><bdo id='Eaw77'><pre id='Eaw77'><center id='Eaw77'></center></pre></bdo></b><th id='Eaw77'></th></span></q></dt></tr></i><div id='Eaw77'><tfoot id='Eaw77'></tfoot><dl id='Eaw77'><fieldset id='Eaw77'></fieldset></dl></div>

      <legend id='Eaw77'><style id='Eaw77'><dir id='Eaw77'><q id='Eaw77'></q></dir></style></legend>
        <tbody id='Eaw77'></tbody>
      <tfoot id='Eaw77'></tfoot>
          • <bdo id='Eaw77'></bdo><ul id='Eaw77'></ul>

            1. <small id='Eaw77'></small><noframes id='Eaw77'>

                本文介绍了自动删除 Chart.js 中的旧数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个实时数据图表,由 Javascript 更新.

                I have a Chart for Real time data, which is updated by Javascript.

                但如果脚本运行时间过长,数据就会被塞满.

                But if the script runs to long, the data is getting crammed up.

                旧数据必须移到左侧然后被删除.

                The old Data would have to be shifted to the left side an then be deleted.

                现场演示:https://codepen.io/benni_de/pen/ajGood

                代码:

                function reLoad() {
                
                  adddata(Math.random() * 100, Math.random() * 10);
                  setTimeout(reLoad, 1000);
                }
                
                var canvas = document.getElementById('myChart');
                var data = {
                  datasets: [{
                      label: 'Download',
                      borderColor: 'rgb(237, 18, 237)',
                      fill: false,
                      borderWidth: 3
                    },
                    {
                      label: 'Upload',
                      borderColor: 'rgb(0, 115, 255)',
                      fill: false,
                      borderWidth: 3
                    }
                  ]
                }
                var options = {
                  scales: {
                    yAxes: [{
                      type: 'linear',
                    }],
                
                    xAxes: [{
                      type: 'time',
                      ticks: {
                        maxTicksLimit: 5,
                      },
                      time: {
                        unit: 'second',
                        displayFormats: {
                          'second': 'HH:mm:ss',
                        },
                        tooltipFormat: 'HH:mm:ss',
                      }
                    }]
                  },
                  showLines: true
                };
                var myChart = new Chart.Line(canvas, {
                  data: data,
                  options: options
                });
                
                function adddata(download = NaN, upload = NaN, label = moment()) {
                  var datasets = myChart.data.datasets;
                  myChart.data.labels.push(label);
                  datasets[0].data.push(download);
                  datasets[1].data.push(upload);
                  myChart.update();
                }
                
                reLoad();

                <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
                <canvas id="myChart" width="400px" height="100px"></canvas>

                推荐答案

                为了完成这项工作,我添加了一个配置变量 MAX_DATA_SET_LENGTH 然后在 addData() 中检查查看上传/下载数据集的长度是否大于您的配置.

                To make this work, I added a config var MAX_DATA_SET_LENGTH and then in addData() I check to see if the length of either the upload/download data set is greater than your config.

                如果数据集比你想要的大,我 shift() 数据集数组(删除第一个条目).我也移动/删除第一个标签以保持数据准确.

                If the data set is larger than you want, I shift() the data set array (remove the first entry). I also shift/remove the first label too to keep the data accurate.

                如果您仍想保留所有数据,我建议将其保存在 Chart.js 不使用的单独数组中.

                If you would still like to keep all the data, I would suggest keeping it in a separate array that Chart.js does not use.

                另外,我将代码切换为使用 setInterval() 相反,因为它在这里更有意义.您希望在某个时间间隔而不是在超时后运行一次 addData(即使您仍然通过多次设置超时使其工作)

                Also, I switched the code to use setInterval() instead since it makes more sense here. You want to run addData on an interval instead of once after a timeout (even though you still make it work by setting the timeout multiple times)

                var MAX_DATA_SET_LENGTH = 15;
                
                function reLoad() {
                  adddata(Math.random() * 100, Math.random() * 10);
                }
                
                var canvas = document.getElementById('myChart');
                var data = {
                  datasets: [{
                      label: 'Download',
                      borderColor: 'rgb(237, 18, 237)',
                      fill: false,
                      borderWidth: 3
                    },
                    {
                      label: 'Upload',
                      borderColor: 'rgb(0, 115, 255)',
                      fill: false,
                      borderWidth: 3
                    }
                  ]
                }
                var options = {
                  scales: {
                    yAxes: [{
                      type: 'linear',
                    }],
                
                    xAxes: [{
                      type: 'time',
                      ticks: {
                        maxTicksLimit: 5,
                      },
                      time: {
                        unit: 'second',
                        displayFormats: {
                          'second': 'HH:mm:ss',
                        },
                        tooltipFormat: 'HH:mm:ss',
                      }
                    }]
                  },
                  showLines: true
                };
                var myChart = new Chart.Line(canvas, {
                  data: data,
                  options: options
                });
                
                function adddata(download = NaN, upload = NaN, label = moment()) {
                  var datasets = myChart.data.datasets;
                  var labels = myChart.data.labels;
                  var downloadDataSet = datasets[0].data;
                  var uploadDataSet = datasets[1].data;
                
                  var downloadDataLength = downloadDataSet.length;
                  var uploadDataLength = uploadDataSet.length;
                
                  // if upload/download's data set has more than MAX_DATA_SET_LENGTH entries, 
                  // remove the first one entry and push on a new data entry
                  var didRemoveData = false;
                  if (downloadDataLength > MAX_DATA_SET_LENGTH) {
                    downloadDataSet.shift();
                    didRemoveData = true;
                  }
                
                  if (uploadDataLength > MAX_DATA_SET_LENGTH) {
                    uploadDataSet.shift();
                    didRemoveData = true;
                  }
                
                  // if either download or upload data was removed, we also need to remove
                  // the first label to keep the data from squeezing in.
                  if (didRemoveData) {
                    labels.shift();
                  }
                
                  labels.push(label);
                  downloadDataSet.push(download);
                  uploadDataSet.push(upload);
                  myChart.update();
                }
                
                
                setInterval(reLoad, 1000);

                <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
                <canvas id="myChart" width="400px" height="100px"></canvas>

                这篇关于自动删除 Chart.js 中的旧数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ChartJS Version 3 - common legend for multiple line charts(ChartJS 版本 3 - 多个折线图的常用图例)
                Charts.js scales yaxes ticks min max doesnt work(Charts.js 缩放 yaxes 刻度 min max 不起作用)
                How to expand the slice of donut chart in chartjs?(如何在chartjs中扩展圆环图的切片?)
                Chart.js yAxes Ticks stepSize not working (fiddle)(Chart.js yAxes Ticks stepSize 不起作用(小提琴))
                Rounded corners on chartJS v.2 - bar charts (with negative values)(chartJS v.2 上的圆角 - 条形图(带负值))
                How to hide value in Chart JS bar(如何在 Chart JS 栏中隐藏值)
                <i id='0qwkJ'><tr id='0qwkJ'><dt id='0qwkJ'><q id='0qwkJ'><span id='0qwkJ'><b id='0qwkJ'><form id='0qwkJ'><ins id='0qwkJ'></ins><ul id='0qwkJ'></ul><sub id='0qwkJ'></sub></form><legend id='0qwkJ'></legend><bdo id='0qwkJ'><pre id='0qwkJ'><center id='0qwkJ'></center></pre></bdo></b><th id='0qwkJ'></th></span></q></dt></tr></i><div id='0qwkJ'><tfoot id='0qwkJ'></tfoot><dl id='0qwkJ'><fieldset id='0qwkJ'></fieldset></dl></div>

                    <tbody id='0qwkJ'></tbody>

                    <tfoot id='0qwkJ'></tfoot>

                          <bdo id='0qwkJ'></bdo><ul id='0qwkJ'></ul>
                        • <legend id='0qwkJ'><style id='0qwkJ'><dir id='0qwkJ'><q id='0qwkJ'></q></dir></style></legend>

                          <small id='0qwkJ'></small><noframes id='0qwkJ'>