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


Highstock - Two data series from mysql query

我创建了一个mysql查询,它为"Visits"和"Visitors"输出两组数据

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

这给了我以下输出:

[{"name":"Visits","data":[293319287]},
{"名称":"访问者","数据":[15767157]}]

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

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

我真的在学习,但我无法做到这一点——我知道我输入的信息不正确。有人能给我一个指针吗?

这是不正确的,因为在数据中,您得到的是序列结构(很清楚),而不是数据。但是你运行

 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