0

我创建了一个 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);
?>

这给了我以下输出:

[{“名称”:“访问”,“数据”:[293,319,287]},
{“名称”:“访问者”,“数据”:[157,167,157]}]

然后我设置了一个 highstock 图表来读取数据:

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

});

我真的在学习,但我无法让它发挥作用——我知道我输入的信息不正确。有人可以给我指点吗?

4

1 回答 1

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
于 2013-06-24T10:50:24.537 回答