【问题标题】:chart.js add second line in a Line chart that starts from end of first linechart.js 在从第一行末尾开始的折线图中添加第二行
【发布时间】:2021-10-17 00:09:15
【问题描述】:

我正在使用 chart.js,并且我有一个折线图。我想添加从第一行结束的位置开始的第二行/数据集。

我见过人们通过在datasets 内的data 属性中添加null 来做到这一点,如下所示:

        type: "line",
        data: {
          labels: ["January,"February","March","April","May","June","July","August"],
          datasets: [
            {
              label: "My First Dataset",
              data:[4,5,6,7,8],
              fill: false,
              borderColor: "rgb(75, 192, 192)",
              tension: 0.3,
            },
    
            { label: "My First Dataset",
              data:[null,null,null,null,null,9,10,11],
              fill: false,
              borderColor: "rgb(75, 192, 192)",
              tension: 0.3,
            },
          ],
        },
      });

有没有其他方法可以实现这一点?

【问题讨论】:

    标签: javascript chart.js chart.jsv3


    【解决方案1】:

    是的,您可以像这样使用对象表示法:

    const options = {
      type: 'line',
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [{
              x: "Red",
              y: 5
            }, {
              x: "Blue",
              y: 3
            }, {
              x: "Yellow",
              y: 8
            }],
            borderColor: 'pink'
          },
          {
            label: '# of Points',
            data: [{
              x: "Yellow",
              y: 8
            }, {
              x: "Green",
              y: 5
            }, {
              x: "Purple",
              y: 3
            }, {
              x: "Orange",
              y: 8
            }],
            borderColor: 'orange'
          }
        ]
      },
      options: {}
    }
    
    const ctx = document.getElementById('chartJSContainer').getContext('2d');
    new Chart(ctx, options);
    <body>
      <canvas id="chartJSContainer" width="600" height="400"></canvas>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2015-07-22
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      相关资源
      最近更新 更多