Chart.js数据背景色正在覆盖点的背景色

5

我正在使用Chart.js。我有一条具有特定颜色的单个数据线。我想让这些数据线的点用不同的颜色表示。根据文档,您可以通过 Chart.defaults.global.elements.point.backgroundColor实现此目的。

 var ctxLine = document.getElementById("lineChart").getContext('2d');
 lineChart = new Chart(ctxLine, {
 type: 'line',
 data: {
     labels: dates,
     datasets: [{
           data: ['...'],
           backgroundColor: "rgba(52,152,219,0.4)"
       }]
 },
 options: {
    elements: {
         point: {
              borderColor: "rgb(255,255,0)",
              backgroundColor: "rgb(255,0,0)"
          }
      }                     
   }
});

point.borderColor 已经正常工作,但是如果我删除第一个 backgroundColor 字段,point.backgroundColor 才会正常工作。

2个回答

11
我已经自己找到了解决办法。我没有意识到 chart.js 有不同的版本。
我正在使用 v2.0 ,存在一个名为 "pointBackgroundColor" 的属性。
var ctxLine = document.getElementById("lineChart").getContext('2d');
lineChart = new Chart(ctxLine, {
type: 'line',
data: {
     labels: dates,
     datasets: [{
           data: ['...'],
           backgroundColor: "rgba(52,152,219,0.4)",
           pointBackgroundColor: "#fff"
       }]
}
});

在这个数据集的图例中,它可以正常显示点,但无法更改背景颜色。 - Kaspatoo
需要将(backgroundColor={['rgb(230, 0, 0, 0.7)']})添加为画布的属性。似乎在图表js中,将RGB值设置为170,170,170作为默认值,并覆盖了之前设置的任何背景颜色。这仅适用于第一个数据集。对于任何其他数据集,设置背景颜色都可以正常工作。 - Kaspatoo

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接