Chart.js 折线图:在图表中间开始一条线

Mik*_*Tan 3 javascript chart.js

我想知道是否可以有一个包含多条线的图表,但我希望其中一条线从图表的中间开始,而其余的线仍然从左边开始。这可能吗?它看起来像这样: 绿线是我想做的

绿线就是我所说的,数据集是否可以从不完全从左边开始

Lee*_*lee 5

是的,这是可能的,您可以通过两种方式实现这一点:

  1. 使用 x 和 y 坐标指定每个数据点
  2. 将一些null值放在数据数组的开头:

var options = {
  type: 'line',
  data: {
    labels: [1, 2, 3, 4, 5, 6],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [{
          x: 3,
          y: 6
        }, {
          x: 4,
          y: 8
        }, {
          x: 5,
          y: 2
        }, {
          x: 6,
          y: 12
        }],
        borderColor: 'orange'
      },
      {
        label: '# of Points2',
        data: [null, null, null, 9, 13, 15],
        borderColor: 'lightblue'
      }
    ]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false
        }
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
Run Code Online (Sandbox Code Playgroud)
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)