如何在Chart.js中更改特定y轴的颜色?

k3bvogb1  于 2023-04-20  发布在  Chart.js
关注(0)|答案(1)|浏览(229)

我正在使用Chart.js创建一个折线图,该折线图显示两个具有不同y轴的数据集。我想更改其中一个y轴的颜色,x轴包含从1开始的测试数量,所以x轴上的第一个刻度是1,最后一个刻度随着autoSkipmaxTicksLimit的使用而变化,所以有时最后一个刻度不在第二个y轴上

  1. const rootStyles = getComputedStyle(document.documentElement);
  2. const mainColor = rootStyles.getPropertyValue('--main-color');
  3. const subColor = rootStyles.getPropertyValue('--sub-color');
  4. const data = {
  5. labels: [] as number[],
  6. datasets: [
  7. {
  8. label: 'WPM',
  9. data: [] as number[],
  10. fill: true,
  11. backgroundColor: mainColor,
  12. borderColor: mainColor,
  13. lineTension: 0.4,
  14. yAxisID: 'y',
  15. },
  16. {
  17. label: 'Accuracy',
  18. data: [] as number[],
  19. fill: true,
  20. backgroundColor: subColor,
  21. borderColor: subColor,
  22. lineTension: 0.4,
  23. yAxisID: 'y1',
  24. },
  25. ],
  26. };
  27. const options: any = {
  28. tooltips: {
  29. enabled: true,
  30. mode: 'label',
  31. },
  32. bezierCurve: true,
  33. responsive: true,
  34. maintainAspectRatio: false,
  35. plugins: {
  36. legend: {
  37. position: 'top',
  38. display: false,
  39. labels: {
  40. usePointStyle: true,
  41. font: {
  42. size: 14,
  43. family: 'lexend, sans-serif',
  44. },
  45. },
  46. },
  47. title: {
  48. display: false,
  49. },
  50. },
  51. interaction: {
  52. intersect: false,
  53. },
  54. scales: {
  55. x: {
  56. title: {
  57. display: true,
  58. text: 'Test',
  59. color: subColor,
  60. font: {
  61. size: 16,
  62. family: 'lexend, sans-serif',
  63. },
  64. },
  65. ticks: {
  66. autoSkip: true,
  67. maxTicksLimit: 10,
  68. font: {
  69. family: 'lexend, sans-serif',
  70. },
  71. color: subColor
  72. },
  73. grid: {
  74. color: (context: any) => {
  75. if (context.tick.value === data.labels[data.labels.length - 2] // here is the problem
  76. || context.tick.value === 0)
  77. {
  78. return subColor
  79. }
  80. else {
  81. return subColor + "15"
  82. }
  83. },
  84. },
  85. },
  86. y: {
  87. min: 0,
  88. max: Math.max(...wpmData) > 150 ? 290 : 200,
  89. position: 'left',
  90. title: {
  91. display: true,
  92. text: 'Words per minute',
  93. color: subColor,
  94. font: {
  95. size: 16,
  96. family: 'lexend, sans-serif',
  97. },
  98. },
  99. ticks: {
  100. font: {
  101. family: 'lexend, sans-serif',
  102. },
  103. color: subColor,
  104. },
  105. grid: {
  106. color: (context: any) => {
  107. if (context.tick.value === 0) {
  108. return subColor
  109. }
  110. else {
  111. return subColor + "15"
  112. }
  113. },
  114. },
  115. },
  116. y1: {
  117. position: 'right',
  118. max: 120,
  119. min: 0,
  120. title: {
  121. display: true,
  122. text: 'Accuracy',
  123. color: subColor,
  124. font: {
  125. size: 16,
  126. family: 'lexend, sans-serif',
  127. },
  128. },
  129. ticks: {
  130. font: {
  131. family: 'lexend, sans-serif',
  132. },
  133. color: subColor,
  134. },
  135. grid: {
  136. color: (context: any) => {
  137. if (context.tick.value === 0) {
  138. return subColor
  139. }
  140. else {
  141. return "transparent"
  142. }
  143. },
  144. },
  145. },
  146. },
  147. elements: {
  148. point: {
  149. radius: 2,
  150. },
  151. },

请注意,我不想为gridLines着色

当前

预期

ht4b089n

ht4b089n1#

假设你使用的是Chart.js 4,你可以在scales配置中使用border节点。

  1. const ctx = document.getElementById('myChart').getContext('2d');
  2. const myChart = new Chart(ctx, {
  3. type: 'line',
  4. data: {
  5. labels: ['1', '2', '3', '4', '5', '6', '7'],
  6. datasets: [{
  7. label: 'Sales',
  8. data: [12, 19, 3, 5, 2, 3, 14],
  9. borderWidth: 3,
  10. yAxisID: 'y'
  11. }, {
  12. label: 'Purchases',
  13. data: [10, 40, 30, 1, 1, 13, 8],
  14. borderWidth: 3,
  15. yAxisID: 'y1'
  16. }],
  17. },
  18. options: {
  19. scales: {
  20. x: {
  21. ticks: {
  22. color: 'red'
  23. },
  24. grid: {
  25. display: false
  26. },
  27. border: {
  28. color: 'red',
  29. }
  30. },
  31. y: {
  32. ticks: {
  33. color: 'red'
  34. },
  35. grid: {
  36. display: false
  37. },
  38. border: {
  39. color: 'red',
  40. }
  41. },
  42. y1: {
  43. position: 'right',
  44. ticks: {
  45. color: 'red'
  46. },
  47. grid: {
  48. display: false
  49. },
  50. border: {
  51. color: 'red',
  52. }
  53. }
  54. },
  55. },
  56. });
  1. .myChartDiv {
  2. max-width: 600px;
  3. max-height: 400px;
  4. }
  1. <script src="https://npmcdn.com/chart.js@latest/dist/chart.umd.js"></script>
  2. <div class="myChartDiv">
  3. <canvas id="myChart" width="600" height="400"></canvas>
  4. </div>

编辑:如果你想让所有图表示例都使用相同的颜色,你也可以考虑在图表默认值中设置borderColor:

  1. Chart.defaults.borderColor = 'red';
展开查看全部

相关问题