3

PHP - This is part of a Highchart configuration array that is returned by a PHP function (which is json_encoded)

'plotOptions' => array(
    'pie' => array(
        'allowPointSelect'  => TRUE,
        'cursor'            => 'pointer',
        'dataLabels'        => array(
            'enabled'           => TRUE,
            'color'             => '#000000',
            'connectorColor'    => '#000000',
            'formatter'         => "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %' }"
        )
    )
)

JavaScript - Accessing this information from the encoded data

container.highcharts(r.hc);
// r.hc is the array that contains the highchart information above

The problem I'm having is:

Uncaught TypeError: Object function() { return '<b>'+ this.point.name +'</b>: '+
this.percentage +' %' } has no method 'call'

How do I change this so it recognizes it as a function? Or is this even possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • Why do you use double quotes there and single quotes everywhere else ? – Nicolai Fröhlich May 22 '13 at 18:31
  • So you are trying to pass a Javascript function to a PHP script? or vice-versa? – thatidiotguy May 22 '13 at 18:31
  • @nifr Notice how there are single quotes in the function? I would have to escape the single quotes inside in order to use single quotes to wrap it. – Mr. Meeseeks May 22 '13 at 18:38
  • @thatidiotguy Its a javascript function in a string that is passed to javascript using AJAX ... – Mr. Meeseeks May 22 '13 at 18:39
  • 1
    Your function is a string object when it is decoded into your JavaScript from JSON, not a function object. You're going to have to do something like `eval` on it. See [this question](http://stackoverflow.com/questions/3946958/pass-function-in-json-and-execute) and [this question](http://stackoverflow.com/questions/5494127/how-to-call-a-javascript-function-from-a-json-string). – ajp15243 May 22 '13 at 18:40

2 Answers2

2

Executing javascript from a remote source is a bad idea as you can't trust it. However, if you must:

The function included in the json is a string therefore you need to evaluate it:

 pie.dataLabels.formatter = eval('('+pie.dataLabels.formatter+')');

for each method of the object that needs to be converted.

However, as said at the start, there is almost certainly a solution that does not require you to fetch javascript in this way.

Rob Johnstone
  • 1,704
  • 9
  • 14
  • Using eval caused the following error: "Uncaught SyntaxError: Unexpected token (" ... which makes me think that it can't eval unnamed functions. Either way, I'm not gonna pass a string for the function ... I'll just pass other data and use javascript to set up the configuration – Mr. Meeseeks May 22 '13 at 18:47
  • I forgot to surround the string in parentheses - these are needed so that eval knows it is evaluating an expression. I've updated my answer accordingly – Rob Johnstone May 22 '13 at 18:53
0

I've decided to only return series data for the pie highchart ... instead of the whole highchart configuration array ... plotOptions are now defined in javascript and not returned by a json_encoded PHP array ..

I'm not too sure why I was doing that but thanks for all the help with eval!

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37