ChartJS - Different color per data point

后端 未结 6 1645
走了就别回头了
走了就别回头了 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:23

    In updating to version 2.2.2 of ChartJS, I found that the accepted answer no longer works. The datasets will take an array holding styling information for the properties. In this case:

    var pointBackgroundColors = [];
    var myChart = new Chart($('#myChart').get(0).getContext('2d'), {
        type: 'line',
        data: {
            datasets: [
                {
                    data: dataPoints,
                    pointBackgroundColor: pointBackgroundColors
                }
            ]
        }
    });
    
    for (i = 0; i < myChart.data.datasets[0].data.length; i++) {
        if (myChart.data.datasets[0].data[i] > 100) {
            pointBackgroundColors.push("#90cd8a");
        } else {
            pointBackgroundColors.push("#f58368");
        }
    }
    
    myChart.update();
    

    I found this looking through the samples for ChartJS, specifically this one: "Different Point Sizes Example"

提交回复
热议问题