问题描述
我正在使用miloschuman的模块来显示高图https://github.com/miloschuman/yii2-highcharts
这里我附上截图 Pass PHP variable to High Chart's tool tip > formatted option
这里是代码示例
<?PHP
use miloschuman\highcharts\Highcharts;
use yii\web\JsExpression;
$a = "Here its the variable";
echo Highcharts::widget([
'options' => [
'tooltip' => [
'valueSuffix' => '','backgroundColor'=> null,'borderWidth'=> 0,'shadow'=> false,'shared'=> false,'useHTML'=> true,'pointFormat'=> $this->render('tooltip2'),'formatter'=>new JsExpression('function(){return <?PHP echo $a ?>}'),'style'=> [ 'opacity'=> 1,'background'=> "rgba(246,246,1)" ],],]
]);?>
但它向我显示未捕获的语法错误:控制台和高图中未显示意外标记“
谢谢!
解决方法
您传递的 JsExpression
构造函数是一个 php 字符串。因此,您可以以与在任何其他 php 字符串中完全相同的方式包含变量。你只需要记住,如果你想包含的变量是字符串,你必须在它周围加上引号。
$a = "Here is the variable";
new JsExpression("function(){ return '$a'; }")
如果你这样做,传递给 JsExpression
类的字符串参数将是:
function() { return 'Here is the variable'; }