简体   繁体   English

从JSON数组中提取具有2个y轴的Highcharts图表的数据系列

[英]Extract data series from a JSON array for a Highcharts chart with 2 y-axis

I'm using Highcharts to create a line chart with 2 y-axis. 我正在使用Highcharts创建具有2个y轴的折线图。 Below is how I build my JSON array from a MySQL query: 下面是如何从MySQL查询中构建JSON数组的方法:

$series1 = array();
$series1['name'] = 'V';
$series2 = array();
$series2['name'] = 'Speed';
while($row = $selectQueryResult1->fetch())
{
    $dateTimer1 = ($row['dateTimer']+$timeAdd)*1000;

    $series1['data'][] = array($dateTimer1,$row['v1']);
    $series2['data'][] = array($dateTimer1,$row['speed']);
}
$result = array();
array_push($result,$series1);
array_push($result,$series2);
echo json_encode($result, JSON_NUMERIC_CHECK);

Here is how I call the PHP script in jQuery: 这是我在jQuery中调用PHP脚本的方式:

$.post('getData.php', {b: begin,e: end}, function(data){

}

My issue now is how to extract the V data and Speed data into two variables because I need to pass them to the chart as two series of data, eg I have tried data[0] , etc. Nothing seems to work. 我现在的问题是如何将V数据和Speed数据提取到两个变量中,因为我需要将它们作为两个系列的数据传递给图表,例如,我尝试过data[0]等。似乎没有任何效果。

///chart.series[0].setData( eval(vData) );
///chart.series[0].setData( eval(speedData) );

You might want to add json as the 4th argument of your $.post to get the JSON parsed as an object, more info here: jQuery.post() . 您可能想要将json添加为$.post的第四个参数,以将JSON解析为对象,更多信息请jQuery.post()

$.post('getData.php', {b: begin,e: end}, function(data){

}, 'json');

Reading your code tells us the structure of your data is as follows: 阅读您的代码可以告诉我们您的数据结构如下:

Array (
    [0] => Array
        (
            [name] => V
            [data] => Array
                (
                    [0] => Array
                        (
                            [0] => value of $dateTimer1
                            [1] => value of $row['v1']

Therefore the data series are: 因此,数据系列为:

  • data[0][data] for V . V data[0][data]
  • data[1][data] for Speed . data[1][data]用于Speed

Therefore: 因此:

chart.series[0].setData( data[0][data] );
chart.series[1].setData( data[1][data] );

When in doubt about a structure, simply print it out: 对结构有疑问时,只需将其打印出来即可:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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