简体   繁体   中英

send javascript function in PHP to be formatted as JSON (highcharts)

Hi I am setting some variables for highcharts in a Zend Report controller script like so:

    $chartOptions = array(
        "chart"=> array(
            //chart type
            "type" => "bar",
                    "spacingBottom" => 25
        ),
        "height"=>1,
        "title" => array(
            "text" => $spm->getFullName()
        ),

etc.

the json I want to end up with also has this in:

exporting: {
    buttons: {
        exportButton: {
            menuItems: [{
                text: 'Export to PNG (with text)',
                onclick: function() {
                    this.exportChart({},{
                        labels: {
                            items:[{
                                html: "System: x.x.x, Date/Time: 30:05:2012 14:32:00",
                                style: {
                                    left: '50%',
                                    top: '250px'
                                }                                            
                            }]
                        }
                    });
                }
            }, {
                text: 'Export to PNG (default)',
                onclick: function() {
                    this.exportChart();
                }
            },
            null,
            null
            ]
        }
    }
}

So I need to setup the PHP array like so:

$chartOptions = array(
    "chart"=> array(
        //chart type
        "type" => "bar",
        "spacingBottom" => 25
     ),
    "height"=>1,
    "title" => array(
        "text" => $spm->getFullName()
    ),
    "exporting" => "buttons" =>

and so on. But I'm not sure how to do it because of the javascript function. The Zend report controller copies all the variables to the view script and I have no idea how it converts the php to json, but it does and it works :/ Sorry I can't be more helpful.

All I want is the outputted json to contain the javascript above, thanks!

Here's a solution that may help you: http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/

Effectively the idea is to encode the function as a string and then decode it once it has been passed to javascript and before passing it into highcharts.

It seems it is possible to encode javascript call backs in to json data with Zend. More information here: http://framework.zend.com/manual/en/zend.json.advanced.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM