ChartJS - Different color per data point

后端 未结 6 1636
走了就别回头了
走了就别回头了 2020-11-27 18:43

Is there a way to set a different color to a datapoint in a Line Chart if its above a certain value?

I found this example for dxChart - https://stackoverflow.com/a/2

6条回答
  •  一生所求
    2020-11-27 19:40

    Here's what worked for me (v 2.7.0), first I had to set pointBackgroundColor and pointBorderColor in the dataset to an array (you can fill this array with colours in the first place if you want):

    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            datasets: [
                {
                    data: dataPoints,
                    pointBackgroundColor: [],
                    pointBorderColor: [],
                }
            ]
        }
    });
    

    Then you can monkey with the colours of the points directly:

      myChart.data.datasets[0].pointBackgroundColor[4] = "#cc00cc";
      myChart.data.datasets[0].pointBorderColor[4] = "#cc0000";
      myChart.update();
    

    Some other properties to play with to distinguish a point: pointStrokeColor (it apparently exists but I can't seem to get it to work), pointRadius & pointHoverRadius (integers), pointStyle ('triangle', 'rect', 'rectRot', 'cross', 'crossRot', 'star', 'line', and 'dash'), though I can't seem to figure out the defaults for pointRadius and pointStyle.

提交回复
热议问题