Chart.js 3.5.1 关于zeroLineColor的属性问题

Chart.js 3.5.1 Attribute question about zeroLineColor

希望有人能帮忙!我正在使用 chart.js 3.5.1 并且心里有一个特定的图表。然而,许多在线文档似乎是针对 chart.js 的旧版本。我需要一个简单的折线图,其中 y 轴上的数据高于零和低于零。为了强调这一点我想在 0 上有一个更暗的轴线,所有在线 material 建议我可以使用 zeroLineColor。不过chart.js的新版本好像不支持这个。所有在线 material 都使用 xAxis:[] 和 yAxis:[],而我的只使用 x:{} 和 y:{} 这让我觉得 zeroLinecolor 属性已被弃用,如果可以的话怎么办我需要以其他方式实施吗?

干杯

您可以为此使用可编写脚本的选项:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      y: {
        grid: {
          color: (ctx) => (ctx.index === 0 ? 'black' : 'rgba(0, 0, 0, 0.1)')
        }
      }
    }
  }
}

var 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>

终于找到答案了!如果它对任何人有用,那么您需要向网格添加一个可编写脚本的组件:

          grid: {
            color: context => context.tick.value == 0 ? '#555555' : '#CCCCCC'
          }