Highstock - 来自mysql查询的两个数据系列

时间:2013-06-21 15:32:07

标签: php json highcharts

我创建了一个mysql查询,为“访问”和访问者“

输出两组数字
<?php
$con = mysql_connect("localhost","name","pass");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("fandang_gvmaster", $con);

$sth = mysql_query("SELECT Unixdate, visits FROM WebWeekly where (`Unixdate` >         
1299456000000) and (`Unixdate` < 1000 * UNIX_TIMESTAMP( NOW()));");
$rows = array();
$rows['name'] = 'Visits';
while($r = mysql_fetch_array($sth)) {
$rows['data'][] = $r['visits'];
}

$sth = mysql_query("SELECT Unixdate, visitors FROM WebWeekly where (`Unixdate` >     
1299456000000) and (`Unixdate` < 1000 * UNIX_TIMESTAMP( NOW()));");
$rows1 = array();
$rows1['name'] = 'Visitors';
while($rr = mysql_fetch_assoc($sth)) {
$rows1['data'][] = $rr['visitors'];
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);

print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con);
?>

这给了我以下输出:

  

[{ “名称”: “访问”, “数据”:[293319287]},
  { “名称”: “访问者”, “数据”:[157167157]}]

然后我设置了一个高库图来读取数据:

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

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

    $.getJSON('calc_tables/Allyears_calc.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() {

    $('#graphcontainerslide').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
    });
}

});

我正在学习,因为我继续学习,但我不能让它工作 - 我知道我正在错误地提供信息。有人可以给我一个指针吗?

1 个答案:

答案 0 :(得分:0)

这是不正确的,因为在您的数据中,您获得了系列结构(很明显),而不是数据。但你运行

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

因此您尝试将系列结构设置为您定义的系列对象。结果你实现了:

     seriesOptions[i] = {
            name: name,
            data: [{"name":"Visits","data":[293,319,287]},
{"name":"Visitors","data":[157,167,157]}]
        };

尝试使用系列对象中的数据,如:

series: data