<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 中的旧数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)
                <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'>