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

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

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制