(chart.js v3.5.1)单击时更改栏背景、标签字体颜色

sy5wg1nm  于 2022-11-23  发布在  Chart.js
关注(0)|答案(2)|浏览(165)

我想更改条形图背景、标签字体颜色和标签字体粗细。但只有条形图背景颜色起作用。
如何在单击时更改字体颜色和字体粗细?
请帮帮我

const backgroundColor = ['rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)','rgb(197,197,197)'];
const fontColor = ['rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)','rgb(153,153,153)'];
const fontWeight = [400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400];          
const barChart = new Chart(
            document.getElementById("barChart"), {
            type: 'bar',
            data: {
                labels: ["jan", "fab", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],
                datasets: [{
                    data: [8, 2, 3, 0, 5, 2, 2, 0, 0, 6, 10, 2],
                    backgroundColor: backgroundColor,
                    borderRadius: 6,
                    borderSkipped: false,
                    barThickness: 20,
                }]      
            },                                                                         
            options:{
                onClick: function(evt){
                    const points = barChart.getElementsAtEventForMode(evt, 'nearest', { intersect: true }, true);
                    if (points.length) {
                        const firstPoint = points[0];
                        for (let i=0; i<backgroundColor.length; i++) {
                            backgroundColor[i] = 'rgb(197,197,197)';
                            fontColor[i] = 'rgb(153,153,153)';
                            fontWeight[i] = 400;
                        }
                        backgroundColor[firstPoint.index] = '#2157e4';
                        fontColor[firstPoint.index] = 'rgb(32,32,32)';
                        fontWeight[firstPoint.index] = 600;

                        this.update();
                    }
                },                                                
                scales: {
                    y: {
                        display: false
                    },  
                    x: {
                        grid: {
                            color: 'white',
                            drawBorder: false
                        },
                        ticks: {
                            color: fontColor,
                            font: {
                                family: "'Pretendard', sans-serif",
                                weight: fontWeight
                            },
                        }
                    }
                },

});
我想更改条形图背景、标签字体颜色和标签字体粗细。但只有条形图背景颜色起作用。
如何在单击时更改字体颜色和字体粗细?
请帮帮我

pgky5nke

pgky5nke1#

关键是在更改数据时处理图表配置中的正确属性。这些属性基本上是chart.data.datasets[0]backgroundColorchart.options.scales.x.ticks.colorchart.options.scales.x.ticks.font.weight
请看一下下面修改后的代码,看看它是如何工作的。
第一个

4smxwvx5

4smxwvx52#

上面的解决方案引导我找到了我的答案。但是由于某种原因,我不能通过简单地更新ticks属性和更新图表来更新权重。我使用的是chartjs 3.9.1,对我有效的解决方案是使用scriptable options。我仍然必须确保从onClick事件调用'chart.update()'。
我将索引存储在一个const变量中,然后在chartOptions下访问该变量以设置权重:

ticks: {
          autoSkip: false,
          font: {
                   size: 12,
                   weight: (w) => {
                            if (highlighted.includes(w.index)) {
                                return 700;
                            } else {
                                return 100;
                            }
                    }
                }
            }

希望这对你有帮助。

相关问题