Chart.js 3.7.1 折线图 - 如何在 y 轴上单独设置每个标签的格式?

发布于 2025-01-15 08:46:43 字数 1170 浏览 3 评论 0原文

我有一个折线图。数据介于 -10 和 10 之间。 y 轴上的标签是正确的(-10 到 10,增量为 1)。

我需要根据颜色数组使每个标签的颜色不同。标签数和颜色数均为 21(-10 到 10,包括 0)。 我真的很想要一个渐变的“条带”,以便每个标签都位于颜色的垂直位置。

我在代码中尝试了这一点,但了解到 html 在图表中不可用:

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 = "";
    }           
}

这就是我的意思。 y 梯度被添加到 Photoshop 中实际图表的图像中。
我可以做这样的事情吗? 输入图片此处描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2025-01-22 08:46:43

您可以将 y.ticks.color 定义为 rgb 颜色数组。这些颜色可以即时生成。

受到来自 的这个令人惊叹的答案的启发Pevara,我想出了以下解决方案:

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>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文