duaedf6509 2011-08-10 08:07
浏览 67
已采纳

将返回的数据从PHP脚本传递到Highcharts data []块?

I have a Highchart scatter chart that plots data calculated using a PHP script. Values are passed to the PHP script via the jQuery .get function and returned to an ExpressionEngine template. The PHP code I'm using is like so:

    printf("{x:'%3.2f',y:'%3.2f',fillColor:'#058DC7'},",$annualReturns[2] * 100.0,$annualSemiStDev[2]);
    printf("{x:'%3.2f',y:'%3.2f',fillColor:'#50B432'},",$annualReturns[3] * 100.0,$annualSemiStDev[3]);
    printf("{x:'%3.2f',y:'%3.2f',fillColor:'#ED561B'},",$annualReturns[4] * 100.0,$annualSemiStDev[4]);

Once back in the ExpressionEngine template, the data is immediately shuffled off to the Highcharts library through a JavaScript function (some of the data going into the PHP script is compiled by another JavaScript function) which creates the Highcharts scatter plot.

    updateScatterChart(data);

This is where I'm running into my issue. The data needs to render, as expected here:

  series: [{
          name: 'Custom',
          data:[ DATA NEEDS TO LIVE HERE ]
          }]

To confirm I'm getting the right data, immediately inside the updateScatterChart function, I'm displaying the results in an alert window -- using alert(data) -- and I get this:

  {x:'12.48',y:'3.52',fillColor:'#058DC7'}
  {x:'34.82',y:'16.11',fillColor:'#50B432'}
  {x:'8.32',y:'9.35',fillColor:'#ED561B'}

which, when pasted inside my data[] block my scatter chart renders as expected. So, my data is good. That pasted code block looks like this:

  series: [{
          name: 'Custom',
          data:[ 
               {x:'12.48',y:'3.52',fillColor:'#058DC7'}
               {x:'34.82',y:'16.11',fillColor:'#50B432'}
               {x:'8.32',y:'9.35',fillColor:'#ED561B'}
              ]
          }]

The connection point I'm missing is getting that returned data to output directly into the data:[] block.

I'm fairly new to PHP and less so to JavaScript, but switching languages like this (moving data from PHP to JavaScript) is still a bit beyond my knowledge base.

  • 写回答

1条回答 默认 最新

  • duandi8544 2011-08-10 08:52
    关注
    <?php
        $data = array(
            (object)array('x'=>12.48, 'y'=>3.52, 'fillOClor'=>'#058DC7'),
            (object)array('x'=>34.82, 'y'=>16.11, 'fillOClor'=>'#50B432'),
            //more to come
        );
    ?>
    series: [{
          name: 'Custom',
          data: <?php echo json_encode($data)?>
          }]
    

    Note square brackets are removed from data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题