L
L
Lander2015-09-22 12:43:32
Yii
Lander, 2015-09-22 12:43:32

How to bind a click handler on a point in Highcharts?

Good afternoon.
In Yii2 I use the widget www.yiiframework.com/extension/yii2-highcharts-widget

<?php

use miloschuman\highcharts\Highcharts;

$this->registerAssetBundle('yii\web\YiiAsset');

echo Highcharts::widget([
   'options' => [
    'title' => ['text' => 'Статистика за месяц'],
    'xAxis' => [
       'categories' => array_keys($statistic)
    ],
    'yAxis' => [
       'title' => ['text' => 'Достижения цели']
    ],
    'series' => [
       ['name' => 'Jane', 'data' => array_values($statistic), 'class'=>'test-class'],
    ],
    'plotOptions' => [
      'series' => [
        'cursor' => 'pointer',
        'point' => [
          'events' => [
            'click' => 'function(e){ alert(this.x); }'
          ]
        ]
      ]
    ]
   ]
]);

Everything works, everything is fine, but when you try to click on a dot in the browser console, it pops up:
Uncaught TypeError: ((jQuery.event.special[handleObj.origType] || (intermediate value)).handle || handleObj.handler).apply is not a function

I'm not strong in the front, so I decided to look at the code that generates the widget. As a result, I see (only the key point):
...],"plotOptions":{"series":{"cursor":"pointer","point":{"events":{"click":"function(e){ alert(this.x); }"}}}}});

As I understand it, the error is related precisely to the fact that function is perceived as a string. In this regard, the question is, how to cure and NPT?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-09-22
@usdglander

use yii\web\JsExpression;
upd. More

<?php

use miloschuman\highcharts\Highcharts;
use yii\web\JsExpression;

$this->registerAssetBundle('yii\web\YiiAsset');

echo Highcharts::widget([
   'options' => [
    'title' => ['text' => 'Статистика за месяц'],
    'xAxis' => [
       'categories' => array_keys($statistic)
    ],
    'yAxis' => [
       'title' => ['text' => 'Достижения цели']
    ],
    'series' => [
       ['name' => 'Jane', 'data' => array_values($statistic), 'class'=>'test-class'],
    ],
    'plotOptions' => [
      'series' => [
        'cursor' => 'pointer',
        'point' => [
          'events' => [
            'click' => new JsExpression('function(e){ alert(this.x); }')
          ]
        ]
      ]
    ]
   ]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question