Question

I'm using this PHP/CodeIgniter library for jQuery Highcharts: http://www.crustiz.com/php-jquery/highcharts-library-for-codeigniter/

The way that library does things is that it builds a PHP array of options, then converts it to json using json_encode (see line 273 of that library) which is then used by the jQuery Highcharts plugin. This is fine, except the option I'm trying to use is the tooltip formatter, which needs to be a javascript function, not a string (see http://www.highcharts.com/ref/#tooltip).

Since the library doesn't have a tooltip function, I created one as a test:

  function set_tooltip() {
    $this->a_options['tooltip']['formatter'] = 'function() { return this.series.name + "<br>" + this.x + ": " + this.y }';
    return $this;
  }

But this doesn't work as the JS function is output as a string, not a function. Anyone know if there is a way to have it be a function after passing through json_encode without rewriting that part of the library?

Was it helpful?

Solution

JSON isn't meant to convey functions. If you take a look at the documentation, there are only specifications for four types - object, array, string and number - plus the values true, false and null.

If you insist on doing this, perhaps an article like Sending Javascript Functions Over JSON will give you a hand.

OTHER TIPS

JSON doesn't have a "function callback" type so I think this is not possible. It knows only the basic types: string / number / object / array

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top