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

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

Chart.js type error after migration v3 to v4

问题

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

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

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

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

  useEffect(() => {
    const ctx = canvasRef.current.getContext('2d');

    if (chartRef.current) {
      chartRef.current.destroy();
    }
    chartRef.current = new Chart(ctx, {
      type: 'bar',
      data: {
        labels,
        datasets: [
          {
            label: 'Month 1',
            data: WLossData as any,
            backgroundColor: colors.GraphBlue,
          },
        ],
      },
      plugins: [ChartDataLabels],
      options: {
        // 其他部分未提供,保留原样
      },
    });
  });

我尝试使用不同版本的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:

chart.js:5199 Uncaught TypeError: Cannot read properties of undefined (reading &#39;axis&#39;)
    at determineAxis (chart.js:5199:1)
    at eval (chart.js:5237:1)
    at Array.forEach (&lt;anonymous&gt;)
    at mergeScaleConfig (chart.js:5229:1)
    at initOptions (chart.js:5279:1)
    at initConfig (chart.js:5290:1)
    at new Config (chart.js:5312:1)
    at new Chart (chart.js:5593:1)
    at eval (ActivityChart.tsx:65:1)
    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

  useEffect(() =&gt; {
    const ctx = canvasRef.current.getContext(&#39;2d&#39;);

    if (chartRef.current) {
      chartRef.current.destroy();
    }
    chartRef.current = new Chart(ctx, {
      type: &#39;bar&#39;,
      data: {
        labels,
        datasets: [
          {
            label: &#39;Month 1&#39;,
            data: WLossData as any,
            backgroundColor: colors.GraphBlue,
          },
        ],
      },
      plugins: [ChartDataLabels],
      options: {
        responsive: true,
        maintainAspectRatio: isMobile,
        layout: {
          padding: {
            left: 0,
            right: 0,
            top: isMobile ? 15 : 25,
            bottom: 0,
          },
        },
        scales: {
          title: {
            display: false,
          },
          x: {
            display: true,
            title: {
              display: true,
              text: intl.formatMessage({ id: messages.monthsInto.id }),
              font: {
                size: isMobile ? 10 : 13,
                family: &#39;GT-America-Standard-Regular&#39;,
              },
              color: colors.DarkSilver,
              padding: isMobile ? 5 : 20,
            },
            ticks: {
              display: true,
              // userCallback: function (item, index) {
              //   return null;
              // },
            },
            grid: {
              display: false,
              drawBorder: true,
            },
          },
          y: {
            title: {
              display: false,
            },
            beginAtZero: true,
            ticks: {
              display: false,

              callback: (item: number | string) =&gt;
                // this only allows the first/base horizontal line
                item === 0 ? item : null,
            },
            grid: {
              display: true,
              drawBorder: false,
            },
          },
        },
        plugins: {
          // @ts-ignore
          datalabels: {
            display: true,
            align: &#39;center&#39;,
            anchor: &#39;center&#39;,
            color: &#39;#ffffff&#39;,
            font: {
              weight: &#39;bold&#39;,
              size: 11,
            },
            labels: {
              value: {
                font: {
                  weight: &#39;bold&#39;,
                  family: &#39;GT-America-Standard-Medium&#39;,
                },
              },
            },
            formatter(value: number | string) {
              return `${value}%`;
            },
          },
          legend: {
            display: false,
          },
          tooltip: {
            ...baseTooltip,
            intersect: true,
            mode: &#39;nearest&#39;,
            callbacks: {
              title: () =&gt; null,
              label: (tooltipItem) =&gt; `${tooltipItem.parsed.y}%`,
            },
          },
        },
      },
    });
  });

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:

确定