Highstock多系列

时间:2013-07-27 00:16:54

标签: php jquery mysql json highstock

请帮我解决多个系列的问题。

我只用一个系列做了这个,结果很好。但我需要另一个系列进行比较。

我已经使用Highstock基本编码来完成这项工作,我从互联网上找到了这些。

  $(function() {
        var seriesOptions = [],
            yAxisOptions = [],
            seriesCounter = 0,
            names = ['Freight', 'DBNchaus'],
            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() {

            $('#container').highcharts('StockChart', {
                chart: {
                },

                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
            });
        }

    });

这是制作json数据的data.php

如果我删除第二个查询,我只能看到一个系列。

$host     = "localhost";
$username = "root";
$password = "";
$db_name  = "northwind";
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");

$sql    = "select unix_timestamp(OrderDate) as datetime, Freight from orders";
$result = mysql_query($sql);
$data   = array();
while ($row = mysql_fetch_array($result)) {
    extract($row);
    $datetime *= 1000; // convert from Unix timestamp to JavaScript time
    $datetime += 19800000;
    $data[] = array(
        $datetime,
        floatval($Freight)
    );
}


$sql    = "select unix_timestamp(OrderDate) as datetime, DBNchaus from orders";
$result = mysql_query($sql);
$data1  = array();
while ($row1 = mysql_fetch_assoc($result)) {
    extract($row1);
    $datetime *= 1000; // convert from Unix timestamp to JavaScript time
    $datetime += 19800000;
    $data1[] = array(
        $datetime,
        floatval($DBNchaus)
    );
}


echo json_encode($data), "\n";
echo json_encode($data1);

非常感谢您的帮助。我被卡住了。 :(

0 个答案:

没有答案