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.
<?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
精彩评论