Chart.js 如何使用脚本选项

Chart.js how to use scriptable options

我正在按照迁移指南将 chart.js 迁移到 3.x。
https://www.chartjs.org/docs/master/getting-started/v3-migration/

我正在尝试将 xAxis zeroLineColor 设置为“#FFFFFF”,并且我希望将网格线颜色设置为“#00FFFF”,但根据 chart.js 迁移文档文档 scales.[x/y]Axes.zeroLine* options of axes were removed. Use scriptable scale options instead

我不确定如何使用可编写脚本的比例选项将 xAxis 线从零设置为白色。

更新:
工作示例:
https://jsfiddle.net/g4vq7o2b/2/

为了获得网格零线的不同颜色,您可以为选项gridLines.color定义一个颜色数组。

gridLines: {
  drawBorder: false,
  color: ["#FFFFFF", "#00FFFF", "#00FFFF", "#00FFFF", "#00FFFF"]
}

因为 gridLines.color 是每个基础数据值的 scriptable options, it also accepts a function which is invoked with a context 参数,如下所示:

gridLines: {
  drawBorder: false,
  color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
}

请查看下面的可运行代码,看看它是如何工作的。

new Chart(document.getElementById("myChart"), {
  type: "line",
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My Dataset",
      data: [2, 3, 1, 4, 2, 4, 2],
      fill: false,
      backgroundColor: "rgba(0, 255, 0, 0.3)",
      borderColor: "rgb(0, 255, 0)"
    }]
  },
  options: {
    scales: {
      y: {
        min: 0,
        ticks: {
          stepSize: 1
        },
        gridLines: {
          drawBorder: false,
          color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
        }
      },
      x: {
        gridLines: {
          drawBorder: false,
          color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF"
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="myChart" height="90"></canvas>

使用 chart_v3.7.0.js 笛卡尔时间轴,对我来说,使用脚本选项配置零线和其他网格线是可行的。 将 属性 应用于“context.tick.value == 0”(=基线) 像: ...

options: { 
            scales: {

            
                x: {    
                        gridLines: {
                          color: "#00ff00",
                        },
                        ticks: {        font: { size: 30    },
                                        maxRotation: 90,
                                        minRotation: 90,
                                },
                        type: 'time',       
                        time: {
                                /*tooltipFormat: "DD.MM hh:mm",*/
                                displayFormats: {
                                    minute: 'HH:mm',
                                    hour: 'HH:mm',
                                    day: 'dd.MMM',
                                }
                        },
                },
                            
                y: {    
                        grid: {
                            color: (line) => (line.index === 0 ? 'red' : 'rgba(0, 0, 0, 0.1)') //color of lowest horizontal grid line
                            color: context => context.tick.value == 0 ? "#FFFFFF" : "#00FFFF", // zero-line baseline color
                            lineWidth: context => context.tick.value == 0 ? 3 : 1, // zero-line baseline width
                        },
                        position: 'right', // show axis on the right
                        title: {        display: true,
                                        rotation: 0,
                                        text: "°C",                 }, 
                    },
                                        
            },