Bhavi Tanwani
Bhavi Tanwani

Reputation: 41

How to pass php value in the "formatter" of Highcharts in Yii2

I want to pass the php value in the high chart's tool tip for-matter option in yii2

I am using the miloschuman's module to display the high-chart https://github.com/miloschuman/yii2-highcharts

Here I am attaching the screenshot Pass PHP variable to High Chart's tool tip > formatted option

Here is the example of code

<?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, 246, 1)" ],
    ],
]
]);?>

But its showing me Uncaught Syntax Error: Unexpected token '<' in console and High-chart is not displaying

Thanks!

Upvotes: 1

Views: 279

Answers (1)

Michal Hynčica
Michal Hynčica

Reputation: 6144

What you are passing JsExpression constructor is a php string. So you can include variable in exactly same way you would do it in any other php string. You just need to remember that if the variable you want to include is the string, you have to put the quotes around it.

$a = "Here is the variable";
new JsExpression("function(){ return '$a'; }")

If you do it that way the string parameter passed to JsExpression class will be:

function() { return 'Here is the variable'; }

Upvotes: 1

Related Questions