<bdo id='9GAJY'></bdo><ul id='9GAJY'></ul>
    <tfoot id='9GAJY'></tfoot>

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

    <small id='9GAJY'></small><noframes id='9GAJY'>

      <legend id='9GAJY'><style id='9GAJY'><dir id='9GAJY'><q id='9GAJY'></q></dir></style></legend>

      ChartJS 显示时间数据的差距

      时间:2023-11-02
        • <bdo id='Sp6v9'></bdo><ul id='Sp6v9'></ul>

          <small id='Sp6v9'></small><noframes id='Sp6v9'>

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

          2. <legend id='Sp6v9'><style id='Sp6v9'><dir id='Sp6v9'><q id='Sp6v9'></q></dir></style></legend>

                  <tbody id='Sp6v9'></tbody>

                本文介绍了ChartJS 显示时间数据的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有这张图:

                这是在 ChartJS 中构建的,但是在下午 1 点到 5:30 之间,没有数据.

                which is built in ChartJS, however, between 1pm and 5:30pm, there was no data.

                我想要图表做的只是显示没有数据,而不是连接两个点.

                All I want the chart to do is display that there is no data, rather than joining the two points.

                这可以吗?理论上,我每 5 秒就有一个新值,但这可能会减少,所以我想我需要能够设置要加入的间隙和要显示的间隙的容差?

                Can this be done? In theory I have a new value every 5 seconds, but this could reduce, so I guess I would need to be able to set a tolerance of gaps to join vs gaps to show?

                ChartOptions 如下所示:

                ChartOptions shown below:

                        myChart = new Chart(ctx, 
                        {
                            type: 'line',
                            data: 
                            {
                                labels: timestamp,
                                datasets: 
                                [{data: speed,backgroundColor: ['rgba(0, 9, 132, 0.2)'],borderColor: ['rgba(0, 0, 192, 1)'],borderWidth: 1},
                                {data: target,borderColor: "rgba(255,0,0,1)",backgroundColor: "rgba(255,0,0,0)",borderWidth: 1,tooltips: {enabled: false}}]
                            },
                            options: 
                            {
                                scales: {yAxes: [{ticks: {beginAtZero:true, min: 0, max: 300}}], xAxes: [{type: 'time',}]},
                                elements: 
                                {point:{radius: 0,hitRadius: 5,hoverRadius: 5},
                                line:{tension: 0}},
                                legend: {display: false},
                                pan: {enabled: true,mode: 'xy',rangeMin: {x: null,y: null},rangeMax: {x: null,y: null}},
                                zoom: {enabled: true,drag: true,mode: 'xy',rangeMin: {x: null,y: null},rangeMax: {x: null,y: null}},
                
                            }
                        });
                

                提前致谢

                推荐答案

                使用 spanGaps 您可以控制没有数据或空数据的点之间折线图的行为:

                Using spanGaps you can control behavior of line chart between points with no or null data:

                var timestamp = [],
                  speed = [10, 100, 20, 30, 40, null, null, null, 100, 40, 60],
                  target = [20, 30, 40, 10, null, null, null, null, 200, 60, 90];
                for (var k = 10; k--; k > 0) {
                  timestamp.push(new Date().getTime() - 60 * 60 * 1000 * k);
                }
                var ctx = document.getElementById('chart').getContext("2d");
                var data = {
                  labels: timestamp,
                  datasets: [{
                      data: speed,
                      backgroundColor: ['rgba(0, 9, 132, 0.2)'],
                      borderColor: ['rgba(0, 0, 192, 1)'],
                      borderWidth: 1,
                      spanGaps: false,
                    },
                    {
                      data: target,
                      borderColor: "rgba(255,0,0,1)",
                      backgroundColor: "rgba(255,0,0,0)",
                      borderWidth: 1,
                      spanGaps: false,
                      tooltips: {
                        enabled: false
                      }
                    }
                  ]
                };
                var options = {
                  scales: {
                    yAxes: [{
                      ticks: {
                        beginAtZero: true,
                        min: 0,
                        max: 300
                      }
                    }],
                    xAxes: [{
                      type: 'time',
                    }]
                  },
                  elements: {
                    point: {
                      radius: 0,
                      hitRadius: 5,
                      hoverRadius: 5
                    },
                    line: {
                      tension: 0
                    }
                  },
                  legend: {
                    display: false
                  },
                  pan: {
                    enabled: true,
                    mode: 'xy',
                    rangeMin: {
                      x: null,
                      y: null
                    },
                    rangeMax: {
                      x: null,
                      y: null
                    }
                  },
                  zoom: {
                    enabled: true,
                    drag: true,
                    mode: 'xy',
                    rangeMin: {
                      x: null,
                      y: null
                    },
                    rangeMax: {
                      x: null,
                      y: null
                    }
                  },
                
                };
                
                var chart = new Chart(ctx, {
                  type: 'line',
                  data: data,
                  options: options
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
                <canvas id="chart"></canvas>

                作为替代方案,您可以修改数据数组并将 null 替换为 zero:

                As alternative you can modify your data array and replace null with zero:

                var timestamp = [],
                    speed = [10, 100, 20, 30, 40, null, null, null, 100, 40, 60],
                    target = [20, 30, 40, 10, null, null, null, null, 200, 60, 90];
                for (var k = 10; k--; k>0) {
                	timestamp.push(new Date().getTime()-60*60*1000*k);
                }
                
                function nullToZero(array) {
                  return array.map(function(v) { 
                    if (v==null) return 0; else return v;
                  });
                }
                
                var ctx = document.getElementById('chart').getContext("2d");
                var data = {
                  labels: timestamp,
                  datasets: [{
                      data: nullToZero(speed),
                      backgroundColor: ['rgba(0, 9, 132, 0.2)'],
                      borderColor: ['rgba(0, 0, 192, 1)'],
                      borderWidth: 1,
                    },
                    {
                      data: nullToZero(target),
                      borderColor: "rgba(255,0,0,1)",
                      backgroundColor: "rgba(255,0,0,0)",
                      borderWidth: 1,
                      tooltips: {
                        enabled: false
                      }
                    }
                  ]
                };
                var options = {
                  scales: {
                    yAxes: [{
                      ticks: {
                        beginAtZero: true,
                        min: 0,
                        max: 300
                      }
                    }],
                    xAxes: [{
                      type: 'time',
                    }]
                  },
                  elements: {
                    point: {
                      radius: 0,
                      hitRadius: 5,
                      hoverRadius: 5
                    },
                    line: {
                      tension: 0
                    }
                  },
                  legend: {
                    display: false
                  },
                  pan: {
                    enabled: true,
                    mode: 'xy',
                    rangeMin: {
                      x: null,
                      y: null
                    },
                    rangeMax: {
                      x: null,
                      y: null
                    }
                  },
                  zoom: {
                    enabled: true,
                    drag: true,
                    mode: 'xy',
                    rangeMin: {
                      x: null,
                      y: null
                    },
                    rangeMax: {
                      x: null,
                      y: null
                    }
                  },
                
                };
                
                var chart = new Chart(ctx, {
                  type: 'line',
                  data: data,
                  options: options
                });

                <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
                <canvas id="chart"></canvas>

                这篇关于ChartJS 显示时间数据的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:两点之间的Chartjs线条颜色 下一篇:图表 js y 轴的不同背景

                相关文章

                  <legend id='yBvYD'><style id='yBvYD'><dir id='yBvYD'><q id='yBvYD'></q></dir></style></legend>
                1. <small id='yBvYD'></small><noframes id='yBvYD'>

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

                      <bdo id='yBvYD'></bdo><ul id='yBvYD'></ul>