如何使用highcharts将多个变量绘制为折线图

时间:2013-07-01 19:40:06

标签: highcharts highstock

我需要将多个变量作为折线图放到highcharts中。由于变量的数量非常多,我需要能够抓取数据并动态绘制图表。

我有多个服务器需要将所有服务器的CPU利用率放在一个不同行的图表中。我的数据在sql查询中看起来像这样:

ServerName  DateTime           CPU
ServerA     1/2/2013 12:00:00  30
ServerA     1/2/2013 01:00:00  20
ServerA     1/2/2013 02:00:00  50
ServerB     1/2/2013 12:00:00  30
ServerB     1/2/2013 01:00:00  30
ServerB     1/2/2013 02:00:00  5
ServerC     1/2/2013 01:00:00  10
serverC     1/2/2013 02:00:00  50
ServerC     1/2/2013 03:00:00  70

我正在查看下面的代码,但由于我看不到数据,我无法真正做出任何改变。如何使用highstock图表将上述sql查询的结果添加到多行系列图表中?

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

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

        $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?',   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
        });
    }

});

1 个答案:

答案 0 :(得分:0)

您需要将日期“1/2/2013 12:00:00”解析为JS时间戳(以毫秒为单位的时间)。您可以使用ie Date.UTC()函数。其次,您必须使用数字来显示CPU值的图表(而不是字符串)。