我可以根据数据集项设置Chart.js图表的背景颜色吗?

mfuanj7w  于 2023-10-18  发布在  Chart.js
关注(0)|答案(3)|浏览(212)

我知道在Chart.js中,你可以将一个颜色数组传递给backgroundColor选项,但是是否可以使用当前的dataset项来选择颜色?理想情况下,我想做的事情是:

  1. new Chart(ctx, {
  2. type: "pie",
  3. data: {
  4. datasets: [{
  5. data: scores,
  6. backgroundColor: (context) => {
  7. // This is the key bit. Can I somehow access the current
  8. // dataset item here from the context?
  9. return context.item.colour
  10. }
  11. }]
  12. }
  13. })

下面是一个工作示例,其中backgroundColor是一个数组,但我想将其更改为使用基于当前数据集项设置背景的回调函数:

  1. const ctx = document.getElementById('chart')
  2. const dataSet = [
  3. { name: 'John', score: 10, colour: 'orange' },
  4. { name: 'Bob', score: 5, colour: 'yellow', },
  5. { name: 'Alice', score: 20, color: 'pink' }
  6. ]
  7. const scores = dataSet.map(data => data.score)
  8. new Chart(ctx, {
  9. type: "pie",
  10. data: {
  11. datasets: [{
  12. data: scores,
  13. backgroundColor: ['red', 'green', 'blue'] // Is there a way I can choose the colour based on the dataset item. Ideally, I'd want to access the array item from dataSet and read the `colour` property from there.
  14. }]
  15. }
  16. })
  1. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  2. <canvas id="chart"></canvas>
c9x0cxw0

c9x0cxw01#

要在scriptable选项中访问颜色,您需要将数据集原样传递给chart.js,并使用配置中的解析选项来告诉chart.js数据必须显示在对象的哪个字段中:

  1. const ctx = document.getElementById('chart')
  2. const dataSet = [{
  3. name: 'John',
  4. score: 10,
  5. colour: 'orange'
  6. },
  7. {
  8. name: 'Bob',
  9. score: 5,
  10. colour: 'yellow',
  11. },
  12. {
  13. name: 'Alice',
  14. score: 20,
  15. colour: 'pink'
  16. }
  17. ]
  18. new Chart(ctx, {
  19. type: "pie",
  20. data: {
  21. datasets: [{
  22. data: dataSet,
  23. backgroundColor: (c) => c.raw.colour,
  24. }]
  25. },
  26. options: {
  27. parsing: {
  28. key: 'score'
  29. }
  30. }
  31. })
  1. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  2. <canvas id="chart"></canvas>
展开查看全部
f3temu5u

f3temu5u2#

你可以使用map()来生成一个基于dataSet的颜色数组:

  1. backgroundColor: dataSet.map(({ colour }) => colour)

但是,您也可以使用reduce()dataSet变量重新处理为CharJS期望的格式。那么你不需要map()来计算分数:

  1. const ctx = document.getElementById('chart')
  2. const dataSet = [
  3. { name: 'John', score: 10, colour: 'orange' },
  4. { name: 'Bob', score: 5, colour: 'yellow', },
  5. { name: 'Alice', score: 20, color: 'pink' }
  6. ]
  7. const mappedDataSet = dataSet.reduce((p, { score, colour }) => {
  8. p.data.push(score);
  9. p.backgroundColor.push(colour);
  10. return p;
  11. }, { data: [], backgroundColor: [] });
  12. new Chart(ctx, {
  13. type: "pie",
  14. data: {
  15. datasets: [ mappedDataSet ]
  16. }
  17. })
  1. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  2. <canvas id="chart"></canvas>
展开查看全部
zaq34kh6

zaq34kh63#

Chart.js背景色问题

要使用回调函数根据当前数据集项设置backgroundColor,您可以使用传递给回调的上下文参数。对于您的特定需求,您可以使用context.dataIndex来获取当前数据点的索引,然后从dataSet数组中访问相关颜色。

用法示例:

  1. const ctx = document.getElementById('chart').getContext('2d');
  2. const dataSet = [{
  3. name: 'John',
  4. score: 10,
  5. color: 'orange'
  6. },
  7. {
  8. name: 'Bob',
  9. score: 5,
  10. color: 'yellow'
  11. },
  12. {
  13. name: 'Alice',
  14. score: 20,
  15. color: 'pink'
  16. }
  17. ];
  18. const scores = dataSet.map(data => data.score);
  19. new Chart(ctx, {
  20. type: "pie",
  21. data: {
  22. labels: dataSet.map(data => data.name),
  23. datasets: [{
  24. data: scores,
  25. backgroundColor: (context) => {
  26. return dataSet[context.dataIndex].color;
  27. }
  28. }]
  29. }
  30. });
  1. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Chart.js Dynamic Color</title>
  8. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  9. </head>
  10. <body>
  11. <canvas id="chart"></canvas>
  12. </body>
  13. </html>
展开查看全部

相关问题