未定义Yii2图表

问题描述:

我正在尝试使用 Yii2 在图表上显示一些数据。我已经通过作曲家安装了 Yii2 Chart JS 。下面的代码摘自文档

I am trying to show some data on the chart while using Yii2. I have installed Yii2 Chart JS via the composer. Below is the code is taken from the documentation

use practically\chartjs\Chart;

Chart::widget([
    'type' => Chart::TYPE_BAR,
    'datasets' => [
        [
            'data' => [
                'Label 1' => 10,
                'Label 2' => 20,
                'Label 3' => 30
            ]
        ]
    ]
]);

现在,当我刷新此页面时,没有看到任何图表。但是在 inspect->控制台中出现错误

Now when I refreshed this page I not seeing any chart. But in inspect->console I am getting an error


Uncaught ReferenceError:图表未定义

Uncaught ReferenceError: Chart is not defined

更新1

这是我的 composer.lock 文件

我调查了此问题,找到了一些解决方案,但仍然无法执行任务。

I have looked into this issue and found some solutions but still unable to perform the task.

任何帮助将不胜感激。

Any help would be highly appreciated.

要显示图表,您必须 echo 小部件结果

To display chart you must echo result of widget

echo Chart::widget([
    'type' => Chart::TYPE_BAR,
    'datasets' => [
        [
            'data' => [
                'Label 1' => 10,
                'Label 2' => 20,
                'Label 3' => 30
            ]
        ]
    ]
]);