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

gpnt7bae  于 2023-02-12  发布在  Chart.js
关注(0)|答案(2)|浏览(229)

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

绿色线就是我所说的,即数据集是否可能从左侧开始

lztngnrs

lztngnrs1#

是的,这是可能的,你可以通过两种方式来实现,一种是使用它的x和y坐标来指定每个数据点,另一种是在数据数组的开始放置一些null值:

  1. var options = {
  2. type: 'line',
  3. data: {
  4. labels: [1, 2, 3, 4, 5, 6],
  5. datasets: [{
  6. label: '# of Votes',
  7. data: [12, 19, 3, 5, 2, 3],
  8. borderColor: 'pink'
  9. },
  10. {
  11. label: '# of Points',
  12. data: [{
  13. x: 3,
  14. y: 6
  15. }, {
  16. x: 4,
  17. y: 8
  18. }, {
  19. x: 5,
  20. y: 2
  21. }, {
  22. x: 6,
  23. y: 12
  24. }],
  25. borderColor: 'orange'
  26. },
  27. {
  28. label: '# of Points2',
  29. data: [null, null, null, 9, 13, 15],
  30. borderColor: 'lightblue'
  31. }
  32. ]
  33. },
  34. options: {
  35. scales: {
  36. yAxes: [{
  37. ticks: {
  38. reverse: false
  39. }
  40. }]
  41. }
  42. }
  43. }
  44. var ctx = document.getElementById('chartJSContainer').getContext('2d');
  45. new Chart(ctx, options);
  1. <body>
  2. <canvas id="chartJSContainer" width="600" height="400"></canvas>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
  4. </body>
展开查看全部
mec1mxoz

mec1mxoz2#

您可以使用offset选项快速完成此操作。

    • 示例:**
  1. const options = {
  2. ...otherChartOptions,
  3. scales: {
  4. x: {
  5. offset: true
  6. },
  7. y: {
  8. offset: true
  9. }
  10. }
  11. };

参考:https://www.chartjs.org/docs/latest/axes/cartesian/linear.html

展开查看全部

相关问题