如何在Chart.js中更改特定y轴的颜色?

k3bvogb1  于 2023-04-20  发布在  Chart.js
关注(0)|答案(1)|浏览(189)

我正在使用Chart.js创建一个折线图,该折线图显示两个具有不同y轴的数据集。我想更改其中一个y轴的颜色,x轴包含从1开始的测试数量,所以x轴上的第一个刻度是1,最后一个刻度随着autoSkipmaxTicksLimit的使用而变化,所以有时最后一个刻度不在第二个y轴上

const rootStyles = getComputedStyle(document.documentElement);
    const mainColor = rootStyles.getPropertyValue('--main-color');
    const subColor = rootStyles.getPropertyValue('--sub-color');
    const data = {
        labels: [] as number[],
        datasets: [
            {
                label: 'WPM',
                data: [] as number[],
                fill: true,
                backgroundColor: mainColor,
                borderColor: mainColor,
                lineTension: 0.4,
                yAxisID: 'y',
            },
            {
                label: 'Accuracy',
                data: [] as number[],
                fill: true,
                backgroundColor: subColor,
                borderColor: subColor,
                lineTension: 0.4,
                yAxisID: 'y1',
            },
        ],
    };

    const options: any = {
        tooltips: {
            enabled: true,
            mode: 'label',
        },
        bezierCurve: true,
        responsive: true,
        maintainAspectRatio: false,
        plugins: {
            legend: {
                position: 'top',
                display: false,
                labels: {
                    usePointStyle: true,
                    font: {
                        size: 14,
                        family: 'lexend, sans-serif',
                    },
                },
            },
            title: {
                display: false,
            },
        },
        interaction: {
            intersect: false,
        },
        scales: {
            x: {
                title: {
                    display: true,
                    text: 'Test',
                    color: subColor,
                    font: {
                        size: 16,
                        family: 'lexend, sans-serif',
                    },
                },
                ticks: {
                    autoSkip: true,
                    maxTicksLimit: 10,
                    font: {
                        family: 'lexend, sans-serif',
                    },
                    color: subColor
                },
                grid: {
                    color: (context: any) => {
                        if (context.tick.value === data.labels[data.labels.length - 2] // here is the problem
                           || context.tick.value === 0)
                        {
                            return subColor
                        }
                        else {
                            return subColor + "15"
                        }
                    },
                },
            },
            y: {
                min: 0,
                max: Math.max(...wpmData) > 150 ? 290 : 200,
                position: 'left',
                title: {
                    display: true,
                    text: 'Words per minute',
                    color: subColor,
                    font: {
                        size: 16,
                        family: 'lexend, sans-serif',
                    },
                },
                ticks: {
                    font: {
                        family: 'lexend, sans-serif',
                    },
                    color: subColor,

                },
                grid: {
                    color: (context: any) => {
                        if (context.tick.value === 0) {
                            return subColor
                        }
                        else {
                            return subColor + "15"
                        }
                    },
                },
            },
            y1: {
                position: 'right',
                max: 120,
                min: 0,
                title: {
                    display: true,
                    text: 'Accuracy',
                    color: subColor,
                    font: {
                        size: 16,
                        family: 'lexend, sans-serif',
                    },
                },
                ticks: {
                    font: {
                        family: 'lexend, sans-serif',
                    },
                    color: subColor,
                },
                grid: {
                    color: (context: any) => {
                        if (context.tick.value === 0) {
                            return subColor
                        }
                        else {
                            return "transparent"
                        }
                    },
                },
            },
        },
        elements: {
            point: {
                radius: 2,
            },
        },

请注意,我不想为gridLines着色

当前

预期

ht4b089n

ht4b089n1#

假设你使用的是Chart.js 4,你可以在scales配置中使用border节点。

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['1', '2', '3', '4', '5', '6', '7'],
    datasets: [{
      label: 'Sales',
      data: [12, 19, 3, 5, 2, 3, 14],
      borderWidth: 3,
      yAxisID: 'y'
    }, {
      label: 'Purchases',
      data: [10, 40, 30, 1, 1, 13, 8],
      borderWidth: 3,
      yAxisID: 'y1'
    }],
  },
  options: {
    scales: {
      x: {
        ticks: {
          color: 'red'
        },
        grid: {
          display: false
        },
        border: {
          color: 'red',
        }
      },
      y: {
        ticks: {
          color: 'red'
        },
        grid: {
          display: false
        },
        border: {
          color: 'red',
        }
      },
      y1: {
        position: 'right',
        ticks: {
          color: 'red'
        },
        grid: {
          display: false
        },
        border: {
          color: 'red',
        }
      }
    },
  },
});
.myChartDiv {
  max-width: 600px;
  max-height: 400px;
}
<script src="https://npmcdn.com/chart.js@latest/dist/chart.umd.js"></script>
<div class="myChartDiv">
  <canvas id="myChart" width="600" height="400"></canvas>
</div>

编辑:如果你想让所有图表示例都使用相同的颜色,你也可以考虑在图表默认值中设置borderColor:

Chart.defaults.borderColor = 'red';

相关问题