chart.js3

Chart.js 3.7.1 Line Chart - How can I format each label individually on the y axis?


I have a line chart. The data is anywhere between -10 and 10. The labels on the y axis are correct (-10 to 10 incremented by 1).

I need the color of each label to be different, based on an array of colors. The number of labels and the number of colors are both 21 (-10 to 10 including 0). I'd really like a 'strip' of a gradient so that each label is at the vertical position of the color.

I tried this in the code but learned that html is not available within the chart:

options: {
    responsive: true,
    plugins: {
        title: {
            display: true,
            text: 'Topic Sentiment'
        }
    },
    scales: {
        y: {                    
            title: {
                display: true,
                text: 'Sentiment Scores'
            },
            min: -10,        
            max: 10,
            ticks: {
                stepSize: 1,
                callback: function(value, index, ticks) {                           
                    return value + " <div style='height:100%; width:8px; background:" + arColors[index] + ";' ></div>"
                }
            }
        }
    },
    onClick: (e, activeEls) => {
        var oChart = e.chart, label = "";
    }           
}

This is what I mean. The y gradient is added to an image of the actual chart in Photoshop.
Can I do anything like this? enter image description here


Solution

  • You can define y.ticks.color as an array of rgb colors. These colors could be generated on the fly.

    Inspired by this amazing answer from Pevara, I came up with the following solution:

    function hslToRgb(h, s, l) {
      var r, g, b;
      if (s == 0) {
        r = g = b = l; // achromatic
      } else {
        function hue2rgb(p, q, t) {
          if (t < 0) t += 1;
          if (t > 1) t -= 1;
          if (t < 1 / 6) return p + (q - p) * 6 * t;
          if (t < 1 / 2) return q;
          if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
          return p;
        }
        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1 / 3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1 / 3);
      }
      return [Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)];
    }
    
    function yValueToRGB(hue) {
      var rgb = hslToRgb(hue, 1, .5);
      return 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
    }
    
    const yTickColors = Array.from(Array(21).keys()).map(v => yValueToRGB(v / 60));
    
    new Chart('canvas', {
      type: 'line',
      data: {
        labels: ['A', 'B', 'C', 'D', 'E', 'F'],
        datasets: [{
            label: 'Dataset 1',
            data: [3, 9, 7, 5, 9, 2],
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            borderColor: 'rgb(255, 99, 132)',
            fill: false
          },
          {
            label: 'Dataset 2',
            data: [1, 2, -3, -5, -2, 1],
            backgroundColor: 'rgba(255, 159, 64, 0.2)',
            borderColor: 'rgb(255, 159, 64)'
          }
        ]
      },
      options: {
        scales: {
          y: {
            max: 10,
            min: -10,
            ticks: {
              stepSize: 1,
              autoSkip: false,
              color: yTickColors
            }
          }
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
    <canvas id="canvas"></canvas>