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?

5
  • Why do you use double quotes there and single quotes everywhere else ? Commented May 22, 2013 at 18:31
  • So you are trying to pass a Javascript function to a PHP script? or vice-versa? Commented May 22, 2013 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. Commented May 22, 2013 at 18:38
  • @thatidiotguy Its a javascript function in a string that is passed to javascript using AJAX ... Commented May 22, 2013 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 and this question. Commented May 22, 2013 at 18:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.