【问题标题】:Array to string conversion while using canvas js in yii2在yii2中使用canvas js时数组到字符串的转换
【发布时间】:2020-04-17 20:34:29
【问题描述】:

我正在尝试在yii2 project 的索引页面上加载图表。下面是我的代码

<?PHP

$dataPoints1 = array(
array("label"=> "2010", "y"=> 36.12),
array("label"=> "2011", "y"=> 34.87),
array("label"=> "2012", "y"=> 40.30),
array("label"=> "2013", "y"=> 35.30),
array("label"=> "2014", "y"=> 39.50),
array("label"=> "2015", "y"=> 50.82),
array("label"=> "2016", "y"=> 74.70)
);
$dataPoints2 = array(
array("label"=> "2010", "y"=> 64.61),
array("label"=> "2011", "y"=> 70.55),
array("label"=> "2012", "y"=> 72.50),
array("label"=> "2013", "y"=> 81.30),
array("label"=> "2014", "y"=> 63.60),
array("label"=> "2015", "y"=> 69.38),
array("label"=> "2016", "y"=> 98.70)
);
?>

我的JS

<?PHP
$script = <<< JS
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer1", {
animationEnabled: true,
theme: "light2",
title:{
    text: "Average Amount Spent on Real and Artificial X-Mas Trees in U.S."
},
legend:{
    cursor: "pointer",
    verticalAlign: "center",
    horizontalAlign: "right",
    itemclick: toggleDataSeries
},
data: [{
    type: "column",
    name: "Real Trees",
    indexLabel: "{y}",
    yValueFormatString: "$#0.##",
    showInLegend: true
    dataPoints: <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>
},{
    type: "column",
    name: "Artificial Trees",
    indexLabel: "{y}",
    yValueFormatString: "$#0.##",
    showInLegend: true,
    dataPoints: <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>
}]
});
chart.render();

function toggleDataSeries(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
    e.dataSeries.visible = false;
}
else{
    e.dataSeries.visible = true;
}
chart.render();
}
}

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

当我运行我的代码时,我遇到了错误

数组到字符串的转换

这个错误来自dataPoints: &lt;?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?&gt;

我怎样才能摆脱这个错误?任何帮助将不胜感激

【问题讨论】:

  • 第一个 dataPoints:.. 之前缺少的 , 是错字吗?
  • @AnuragSrivastava 添加, 不会有任何区别
  • 如果它对你有用,你能标记答案吗

标签: javascript yii2 yii2-advanced-app canvasjs jsonencoder


【解决方案1】:

您应该将php数组编码为heredoc外部的json并将输出提供给javascript部分,并且您不使用php标签而是使用花括号{}从heredoc内部的变量中解析值。

请看下面它应该可以正常工作

<?PHP
$dataPoints1 = json_encode($dataPoints1, JSON_NUMERIC_CHECK);
$dataPoints2 = json_encode($dataPoints1, JSON_NUMERIC_CHECK);

$script = <<< JS
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer1", {
animationEnabled: true,
theme: "light2",
title:{
    text: "Average Amount Spent on Real and Artificial X-Mas Trees in U.S."
},
legend:{
    cursor: "pointer",
    verticalAlign: "center",
    horizontalAlign: "right",
    itemclick: toggleDataSeries
},
data: [{
    type: "column",
    name: "Real Trees",
    indexLabel: "{y}",
    yValueFormatString: "$#0.##",
    showInLegend: true
    dataPoints: {$dataPoints1}
},{
    type: "column",
    name: "Artificial Trees",
    indexLabel: "{y}",
    yValueFormatString: "$#0.##",
    showInLegend: true,
    dataPoints: {$dataPoints2}
}]
});
chart.render();

function toggleDataSeries(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
    e.dataSeries.visible = false;
}
else{
    e.dataSeries.visible = true;
}
chart.render();
}
}

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

【讨论】:

    【解决方案2】:

    @Faisal,您正在“dataPoints”中传递一个字符串(回显将任何内容打印为字符串),但它接受 JSON 数组。

    您需要解析 json 字符串以将其转换为有效的 JSON。使用 JSON.parse() 函数并在 Javascript 中修改您的代码,如下所示。

    数据点:JSON.parse()

    更新:

    在初始化图表之前,首先在变量中从 PHP 中获取数据点。然后在图表配置中传递这个变量。也尝试使用 JSON.parse()。

    如果它仍然不起作用,请在控制台中打印这个新变量并检查您的输出并将其发布在这里

    【讨论】:

    • 请看这个答案。 stackoverflow.com/a/21153859/7867822
    • 在初始化图表之前,首先从 PHP 中获取数据点到一个变量中。然后在图表配置中传递这个变量。也尝试使用 JSON.parse()。如果它仍然不起作用,请在控制台中打印这个新变量并检查您的输出并将其发布在这里
    • 我不是提问者 :) 只是指出 OP 的方法是可能的,你的回答表明信息不正确
    • var dp1 = &lt;?php echo json_encode( $dataPoints1 ) ?&gt;; console.log(dp1); 仍然给我同样的错误
    • JSON.parse 也是如此
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 2020-10-20
    • 1970-01-01
    • 2021-05-03
    • 2018-02-18
    • 2022-07-21
    • 2011-03-25
    • 2014-11-13
    相关资源
    最近更新 更多