【问题标题】:ChartJS show gaps in time dataChartJS 显示时间数据的差距
【发布时间】:2018-03-19 18:00:09
【问题描述】:

我有这张图:

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

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

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

ChartOptions 如下所示:

        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}},

            }
        });

提前致谢

【问题讨论】:

    标签: javascript time graph chart.js


    【解决方案1】:

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

    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

    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>

    【讨论】:

    • 谢谢 Beaver,我的数组的结构方式我没有“null”值,如果值为 null,则数组中没有条目,如果您按照我的思路进行操作。我按照您的建议添加了空值,并且确实解决了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多