开发者问题收集

从 chart.js 1 升级到 3.5 时出现问题

2021-08-17
168

我正在检查所有 PHP (V7.3) 页面并将所有图表升级到最新版本。

在某些页面上,如果图表数据是子页面的一部分或通过 AJAX 调用创建,我总是收到此错误

Uncaught TypeError: Cannot read property 'axis' of undefined
    at vo.parse (chart3.min.js:13)
    at vo._insertElements (chart3.min.js:13)
    at vo._resyncElements (chart3.min.js:13)
    at vo.buildOrUpdateElements (chart3.min.js:13)
    at oo.update (chart3.min.js:13)
    at new oo (chart3.min.js:13)
    at eval (eval at <anonymous> (jquery.min.js:2), <anonymous>:18:19)
    at eval (<anonymous>)
    at jquery.min.js:2
    at Function.globalEval (jquery.min.js:2)

我的代码已从:

<canvas id='chart_canvas' width='620' height='300'></canvas>
<script>
    var data = <?php echo json_encode($data); ?>;
    var ctx = document.getElementById("chart_canvas").getContext("2d");
    var myNewChart = new Chart(ctx).Line(data, {
         pointHitDetectionRadius: 5
     });
</script>

更改为:

<canvas id='chart_canvas' width='620' height='300'></canvas>
<script>
    var data = <?php echo json_encode($data); ?>;
    const config = {
        type: 'line',
        data,
        options: {
            responsive: true,
        }
    };
    var myChart = new Chart(
        document.getElementById('chart_canvas'),
        config
    );
</script>

数据变量为:

var data = {"labels":["2021-07-17","2021-07-18","2021-07-19","2021-07-20","2021-07-21","2021-07-22","2021-07-23","2021-07-24","2021-07-25","2021-07-26","2021-07-27","2021-07-28","2021-07-29","2021-07-30","2021-07-31","2021-08-01","2021-08-02","2021-08-03","2021-08-04","2021-08-05","2021-08-06","2021-08-07","2021-08-08","2021-08-09","2021-08-10","2021-08-11","2021-08-12","2021-08-13","2021-08-14","2021-08-15","2021-08-16","2021-08-17"],"datasets":[{"label":"Jobs Created","fillColor":"rgba(26, 188, 156,0.2)","strokeColor":"rgba(26, 188, 156,1)","pointColor":"rgba(26, 188, 156,1)","pointStrokeColor":"#fff","pointHighlightFill":"#fff","pointHighlightStroke":"rgba(26, 188, 156,1)","data":{"2021-08-17":"2"}}]};

我知道代码没有问题,因为我已在 https://playcode.io/ 中尝试过它

我只是无法理解错误消息,而且我在 Google 上找不到任何内容,非常感谢任何指点。

2个回答

您的数据中只有 1 个项目。由于项目之间没有线可画,因此在图表上很难看到。

除此之外, 数据集 中的 数据 应采用 这样的格式。

看起来您正在尝试组合 PrimitiveObject 样式,而实际上您应该使用其中一种样式。因为您已经给出了标签,所以您只需要给出如下数据: [10, 20, 30]

您也可以提供您当前提供的数据。您只是不需要标签。

Jesper
2021-08-17

找到罪魁祸首 -prototype-1.7.js

删除它,一切正常

djcamo
2021-08-19