2

我在尝试通过 Json 将多个数据传递到 Highstock 图表时遇到问题,这是我目前的代码:

    $(function() {
    var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        names = ['MSFT', 'AAPL'],
        colors = Highcharts.getOptions().colors;

    $.each(names, function(i, name) {

        $.getJSON('data.php',   function(data) {

            seriesOptions[i] = {
                name: name,
                data: data
            };

            // As we're loading the data asynchronously, we don't know what order it will arrive. So
            // we keep a counter and create the chart when all the data is loaded.
            seriesCounter++;

            if (seriesCounter == names.length) {
                createChart();
            }
        });

    });



    // create the chart when all data is loaded
    function createChart() {

        chart = new Highcharts.StockChart({
            chart: {
                renderTo: 'container'
            },

            rangeSelector: {
                selected: 4
            },

            yAxis: {
                labels: {
                    formatter: function() {
                        return (this.value > 0 ? '+' : '') + this.value + '%';
                    }
                },
                plotLines: [{
                    value: 0,
                    width: 2,
                    color: 'silver'
                }]
            },

            plotOptions: {
                series: {
                    compare: 'percent'
                }
            },

            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                valueDecimals: 2
            },

            series: seriesOptions

        });
    }
});

这是我的 data.php 文件:

$query = "SELECT tiempo,Fp1Ref,F7Ref
            FROM GraficaEEG limit 10";
    $data = mysqli_query($dbc, $query);
    $i=0;
    while($row = mysqli_fetch_array($data)) {

        $rows[$i]=array((float)$row['tiempo'],(float)$row['Fp1Ref']); 
        $rows1[$i]=array((float)$row['tiempo'],(float)$row['F7Ref']); 


        $i++;
    }

    echo json_encode($rows), "\n";
    echo json_encode($rows1);

我以这种形式接收数据:

[[0,3001],[0.005,4937.22],[0.01,4130.55],[0.015,4213.15],[0.02,4010.61],[0.025,3914.34],[0.03,3785.33],[0.035,3666.13],[0.04,3555.25],[0.045,3447.77]]

[[0,2995.12],[0.005,4931.59],[0.01,4125.99],[0.015,4203.38],[0.02,4005.27],[0.025,3911.41],[0.03,3777.35],[0.035,3659.15],[0.04,3552.75],[0.045,3442.12]]

我不知道我做错了什么以及如何将两组数据传递给 Highstock 图表,如果我只传递一个 $rows 它可以工作,但它在三行中给了我相同的数据,我会将非常感谢任何帮助。

4

2 回答 2

1

有 3 种方法可以将数据传递到 Highstock。

  1. 数值列表。例如data: [0, 5, 3, 5]

  2. 具有两个值的数组列表,例如data: [[5, 2], [6, 3], [8, 2]]

  3. 具有命名值的对象列表。

例如

data: [{
    name: 'Point 1',
    color: '#00FF00',
    y: 0
}, {
    name: 'Point 2',
    color: '#FF00FF',
    y: 5
}]

您可以为每一行创建 Json 对象并将它们存储在 Json 数组中。然后在另一个 Json 对象中使用命名属性(也称为键)传递 Json 数组,该对象将保存多个 Json 数组,每个数组具有不同的键。

在返回的 Response 中,使用其 Key 检索每个数组。

查看文档以获取更多详细信息: http: //api.highcharts.com/highstock#series.data

我从来没有在 PHP 上工作过所以,我不能帮助直接,但希望这会有所帮助。

于 2012-10-01T09:42:54.173 回答
0

我没有异步加载数据,而是将保存最终数据字符串的变量转储到 Javascript 中。

例如,

chart = new Highcharts.StockChart({
          chart: { renderTo: 'container' },

          ...all those chart options...

          series: <?php echo $formattedDataString; ?>

        });


因此,当 Highcharts 加载时,所有数据都已经到位。当然,这假设数据格式正确,正如 Hardik 所提到的。
如果您需要动态更新数据,那么您仍然可以进行 AJAX 调用和 .update() 图表。

于 2014-12-08T18:22:23.687 回答