如何使用array_merge在PHP / Highcharts中显示非引用的命令?

时间:2015-02-24 09:06:41

标签: javascript php highcharts array-merge

我将带有多个Highcharts参数的JSON数组传递给客户端。在少数情况下,我使用array_merge在分配代码之后插入一些额外的东西。只要传递的参数将以引号显示,例如:

,这样就可以正常工作
$data_['xAxis'] = array_merge($data_['xAxis'], array("type" => "category"));

然后,在客户端,这将转化为:

type: "category"

但有时我想传递一个函数,它应该像这样显示:

chart: {
    events: {
        load: function () {
            var label = ....
        }
    }
},    

当我在这里使用它时,它显示为文本:

$data_['chart'] = array_merge($data_['chart'], array("events" => "{load: function(event) {var label = ... }}");

chart: {
    events: "{
        load: function () {
            var label = ....
        }
    }"
},    

编辑:客户端的Javascript如下所示:

$(document).ready(function() {
    var options = {};               

    var url =  "http://geodev.grid.unep.ch/mod_test/jsonp_response.php?callback=?";

    $.getJSON(url, {selectedCountries: selectedCountries , selectedID: selectedID, selectedYears: selectedYears, per_capita: per_capita, graphBorder: graphBorder, graphSource: graphSource, graphStyle: graphStyle, graphXAxis: graphXAxis, type: "jsonp"})
    .done(function(data)
    {
        //console.log(data);

        options.chart       = data["chart"];
        options.tooltip     = data["tooltip"];
        options.series      = data["series"];
        options.title       = data["title"];
        options.subtitle    = data["subtitle"];
        options.yAxis       = data["yAxis"];
        options.xAxis       = data["xAxis"];
        options.legend      = data["legend"];
        options.exporting   = data["exporting"];
        options.plotOptions = data["plotOptions"];
        options.credits     = data["credits"];

        var chart = new Highcharts.Chart(options);
    })

那么,为了不将其显示为文本,我可以做些什么来传递它?任何人都可以给我一个提示如何以正确的方式做到这一点?非常感谢!

1 个答案:

答案 0 :(得分:1)

两种方式

  1. 在javascript本身中实现事件处理程序,如

    options.credits     = data["credits"];
    
    options.chart.events = {};
    options.chart.events.load = function(event){
        // implementations go here
    }
    
  2. Not recommending though,使用eval

    options.credits     = data["credits"];
    
    options.chart.events = eval(data.chart.events);
    
  3. 希望这有帮助