【问题标题】:Yii2 unable to view chartYii2 无法查看图表
【发布时间】:2020-04-17 07:54:07
【问题描述】:

我正在使用ChartJs Widget For Yii2 by 2amigOS。下面是我的代码

use dosamigos\chartjs\ChartJs;

ChartJs::widget([
    'type' => 'pie',
    'id' => 'structurePie',
    'options' => [
        'height' => 200,
        'width' => 400,
    ],
    'data' => [
        'radius' =>  "90%",
        'labels' => ['Label 1', 'Label 2', 'Label 3'], // Your labels
        'datasets' => [
            [
                'data' => ['35.6', '17.5', '46.9'], // Your dataset
                'label' => '',
                'backgroundColor' => [
                        '#ADC3FF',
                        '#FF9A9A',
                    'rgba(190, 124, 145, 0.8)'
                ],
                'borderColor' =>  [
                        '#fff',
                        '#fff',
                        '#fff'
                ],
                'borderWidth' => 1,
                'hoverBorderColor'=>["#999","#999","#999"],
            ]
        ]
    ],
    'clientOptions' => [
        'legend' => [
            'display' => false,
            'position' => 'bottom',
            'labels' => [
                'fontSize' => 14,
                'fontColor' => "#425062",
            ]
        ],
        'tooltips' => [
            'enabled' => true,
            'intersect' => true
        ],
        'hover' => [
            'mode' => false
        ],
        'maintainAspectRatio' => false,

    ],
    'plugins' =>
        new \yii\web\JsExpression('
        [{
            afterDatasetsDraw: function(chart, easing) {
                var ctx = chart.ctx;
                chart.data.datasets.forEach(function (dataset, i) {
                    var meta = chart.getDatasetMeta(i);
                    if (!meta.hidden) {
                        meta.data.forEach(function(element, index) {
                            // Draw the text in black, with the specified font
                            ctx.fillStyle = \'rgb(0, 0, 0)\';

                            var fontSize = 16;
                            var fontStyle = \'normal\';
                            var fontFamily = \'Helvetica\';
                            ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);

                            // Just naively convert to string for now
                            var dataString = dataset.data[index].toString()+\'%\';

                            // Make sure alignment settings are correct
                            ctx.textAlign = \'center\';
                            ctx.textBaseline = \'middle\';

                            var padding = 5;
                            var position = element.tooltipPosition();
                            ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
                        });
                    }
                });
            }
        }]')
]);

当我刷新我的代码时,我得到了

创建图表失败:无法从给定项目获取上下文

在我的inspect->console。我已经研究了一种解决方案并尝试了它

<canvas id="myChart" width="400" height="400"></canvas>
<?PHP
$script = <<< JS
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)',
            'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
            'rgba(255, 99, 132, 1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)',
            'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
    }]
},
options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
    }
}
});

JS;
$this->registerJs($script);
?>

当我尝试刷新此页面时,我得到了

Uncaught ReferenceError: Chart is not defined

在我的inspect-&gt;console

我怎样才能摆脱这些问题?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript yii2 chart.js yii2-advanced-app


    【解决方案1】:

    在第一个代码示例中,您必须 echo 您的小部件

    echo ChartJs::widget([...]);
    

    <?= ChartJs::widget([...]); ?>
    

    对于第二个代码,您必须先添加图表资产(小部件自行添加)

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多