Chart.js从v3迁移到v4后出现类型错误。

huangapple go评论99阅读模式
英文:

Chart.js type error after migration v3 to v4

问题

我在我的React项目中升级了chart.js。在v3.9.1版本中它能够正常工作,但在v4.3.0版本中出现了以下错误:

  1. chart.js:5199 Uncaught TypeError: Cannot read properties of undefined (reading 'axis')
  2. at determineAxis (chart.js:5199:1)
  3. at eval (chart.js:5237:1)
  4. at Array.forEach (<anonymous>)
  5. at mergeScaleConfig (chart.js:5229:1)
  6. at initOptions (chart.js:5279:1)
  7. at initConfig (chart.js:5290:1)
  8. at new Config (chart.js:5312:1)
  9. at new Chart (chart.js:5593:1)
  10. at eval (ActivityChart.tsx:65:1)
  11. at invokePassiveEffectCreate (react-dom.development.js:23487:1)

我完全不知道为什么会发生这种情况以及如何修复它。

我的当前代码在v3.9.1版本上能够正常工作:

  1. useEffect(() => {
  2. const ctx = canvasRef.current.getContext('2d');
  3. if (chartRef.current) {
  4. chartRef.current.destroy();
  5. }
  6. chartRef.current = new Chart(ctx, {
  7. type: 'bar',
  8. data: {
  9. labels,
  10. datasets: [
  11. {
  12. label: 'Month 1',
  13. data: WLossData as any,
  14. backgroundColor: colors.GraphBlue,
  15. },
  16. ],
  17. },
  18. plugins: [ChartDataLabels],
  19. options: {
  20. // 其他部分未提供,保留原样
  21. },
  22. });
  23. });

我尝试使用不同版本的chart.js v4.x,但它们都显示了我提到的错误。而且我在文档中找不到任何可以帮助解决这个问题的信息。

英文:

I'm upgrading my chart.js in my React project, it was working fine in v3.9.1 but in the v4.3.0 I'm getting the following error:

  1. chart.js:5199 Uncaught TypeError: Cannot read properties of undefined (reading &#39;axis&#39;)
  2. at determineAxis (chart.js:5199:1)
  3. at eval (chart.js:5237:1)
  4. at Array.forEach (&lt;anonymous&gt;)
  5. at mergeScaleConfig (chart.js:5229:1)
  6. at initOptions (chart.js:5279:1)
  7. at initConfig (chart.js:5290:1)
  8. at new Config (chart.js:5312:1)
  9. at new Chart (chart.js:5593:1)
  10. at eval (ActivityChart.tsx:65:1)
  11. at invokePassiveEffectCreate (react-dom.development.js:23487:1)

I have no idea why this is happening in how to fix it.

My current code, that's working on v3.9.1

  1. useEffect(() =&gt; {
  2. const ctx = canvasRef.current.getContext(&#39;2d&#39;);
  3. if (chartRef.current) {
  4. chartRef.current.destroy();
  5. }
  6. chartRef.current = new Chart(ctx, {
  7. type: &#39;bar&#39;,
  8. data: {
  9. labels,
  10. datasets: [
  11. {
  12. label: &#39;Month 1&#39;,
  13. data: WLossData as any,
  14. backgroundColor: colors.GraphBlue,
  15. },
  16. ],
  17. },
  18. plugins: [ChartDataLabels],
  19. options: {
  20. responsive: true,
  21. maintainAspectRatio: isMobile,
  22. layout: {
  23. padding: {
  24. left: 0,
  25. right: 0,
  26. top: isMobile ? 15 : 25,
  27. bottom: 0,
  28. },
  29. },
  30. scales: {
  31. title: {
  32. display: false,
  33. },
  34. x: {
  35. display: true,
  36. title: {
  37. display: true,
  38. text: intl.formatMessage({ id: messages.monthsInto.id }),
  39. font: {
  40. size: isMobile ? 10 : 13,
  41. family: &#39;GT-America-Standard-Regular&#39;,
  42. },
  43. color: colors.DarkSilver,
  44. padding: isMobile ? 5 : 20,
  45. },
  46. ticks: {
  47. display: true,
  48. // userCallback: function (item, index) {
  49. // return null;
  50. // },
  51. },
  52. grid: {
  53. display: false,
  54. drawBorder: true,
  55. },
  56. },
  57. y: {
  58. title: {
  59. display: false,
  60. },
  61. beginAtZero: true,
  62. ticks: {
  63. display: false,
  64. callback: (item: number | string) =&gt;
  65. // this only allows the first/base horizontal line
  66. item === 0 ? item : null,
  67. },
  68. grid: {
  69. display: true,
  70. drawBorder: false,
  71. },
  72. },
  73. },
  74. plugins: {
  75. // @ts-ignore
  76. datalabels: {
  77. display: true,
  78. align: &#39;center&#39;,
  79. anchor: &#39;center&#39;,
  80. color: &#39;#ffffff&#39;,
  81. font: {
  82. weight: &#39;bold&#39;,
  83. size: 11,
  84. },
  85. labels: {
  86. value: {
  87. font: {
  88. weight: &#39;bold&#39;,
  89. family: &#39;GT-America-Standard-Medium&#39;,
  90. },
  91. },
  92. },
  93. formatter(value: number | string) {
  94. return `${value}%`;
  95. },
  96. },
  97. legend: {
  98. display: false,
  99. },
  100. tooltip: {
  101. ...baseTooltip,
  102. intersect: true,
  103. mode: &#39;nearest&#39;,
  104. callbacks: {
  105. title: () =&gt; null,
  106. label: (tooltipItem) =&gt; `${tooltipItem.parsed.y}%`,
  107. },
  108. },
  109. },
  110. },
  111. });
  112. });

I've tried to use different versions of chart.js v4.x, but all of them is showing the error that I mentioned.
Also I didn't find anything in the documentation that could help deal with this issue.

答案1

得分: 1

我不100%确定为什么您的代码不起作用,但我已经测试过了,如果您从配置属性scales中删除title属性,图表应该可以渲染。文档中也没有显示scales配置的title属性。

英文:

I'm not 100% percent sure why your code doesn't work, but tested it, if you remove the property title from the config-property scales, and the chart should render. The documentation also doesn't show a property title for the scales configuration.

huangapple
  • 本文由 发表于 2023年7月3日 22:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定