【问题标题】:Highstock - Get two data from database using JSON and display into a Chart using addseriesHighstock - 使用 JSON 从数据库中获取两个数据并使用添加序列显示到图表中
【发布时间】:2013-04-04 20:11:33
【问题描述】:

我正在尝试使用 Highstock 来显示此示例中的图表。 JS fiddle

我需要使用两个JSON 代码,因为我想在我的数据库中显示来自不同表的两个数据。 第一个数据是 OHLC 和体积。 OHLC 和音量显示在不同的yAxis 中,就像上面的示例一样。

第二个数据是高低预测,我想将它与 OHLC 一起显示在同一个 yAxis 中。虽然音量保持不变(显示在不同的yAxis 与 OHLC 和预测)。

这是迄今为止我尝试过的代码,但它只显示 OHLC 和 Volume。不知何故,未添加预测数据。我认为addSeries() 是问题所在。谁能帮帮我?

谢谢。

$.getJSON(url, function(data) {
    var ohlc = [];
    var volume = [];
    var dataLength = data.length;

    for (i = 0; i < dataLength; i++) {
        ohlc.push([
            data[i][0], // the date
            data[i][1],
            data[i][2],
            data[i][3],
            data[i][4] // open
        ]);

        volume.push([
            data[i][0], // the date
            data[i][5] // the volume
        ])
    }

    $('#container').highcharts('StockChart', {

        rangeSelector: {
            selected: 0,
        },

        title: {
            text: 'AAPL Historical'
        },

        yAxis: [{
            title: {
                text: 'OHLC'
            },
            height: 200,
            lineWidth: 2
        }, {
            title: {
                text: 'Volume'
            },
            top: 300,
            height: 100,
            offset: 0,
            lineWidth: 2
        }],

        series: [{
            type: 'candlestick',
            name: 'AAPL',
            data: ohlc,
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
        }]
    });
});

$.getJSON(urlprediction, function(data) {
    var forecast = [];
    var dataLength = data.length;

    for (i = 0; i < dataLength; i++) {
        forecast.push([
            data[i][0], // the date
            data[i][1],
            data[i][2], // open
        ])
    }

    var chart = $('#container').highcharts();
    chart.addSeries({
        type: 'arearange',
        name: 'ADBE',
        data: forecast,
    });
});

【问题讨论】:

    标签: getjson highstock


    【解决方案1】:

    如果你第二次通过 JSON 获取数据,你不应该使用 var chart = $('#container').highcharts(); .我建议使用这种结构:

    $.getJSON(url, function(data) {
    var ohlc = [];
    var volume = [];
    var dataLength = data.length;
    
    for (i = 0; i < dataLength; i++) {
        ohlc.push([
            data[i][0], // the date
            data[i][1],
            data[i][2],
            data[i][3],
            data[i][4] // open
        ]);
    
        volume.push([
            data[i][0], // the date
            data[i][5] // the volume
        ])
    }
    
    var chart = $('#container').highcharts('StockChart', {
    
        rangeSelector: {
            selected: 0,
        },
    
        title: {
            text: 'AAPL Historical'
        },
    
        yAxis: [{
            title: {
                text: 'OHLC'
            },
            height: 200,
            lineWidth: 2
        }, {
            title: {
                text: 'Volume'
            },
            top: 300,
            height: 100,
            offset: 0,
            lineWidth: 2
        }],
    
        series: [{
            type: 'candlestick',
            name: 'AAPL',
            data: ohlc,
        }, {
            type: 'column',
            name: 'Volume',
            data: volume,
            yAxis: 1,
        }]
    },function(chart){
    $.getJSON(urlprediction, function(data) {
        var forecast = [];
        var dataLength = data.length;
    
        for (i = 0; i < dataLength; i++) {
            forecast.push([
            data[i][0], // the date
            data[i][1],
            data[i][2], // open
            ])
        }
    
        chart.addSeries({
            type: 'arearange',
            name: 'ADBE',
            data: forecast,
        });
        });
    
    });
    

    });

    【讨论】:

    • 它不起作用。它给了我空白页。图表未显示。
    • 嘿 Sebastian Bochan,我找到了答案。现在我还有另一个问题here。请问你能帮帮我吗?谢谢。
    猜你喜欢
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2019-04-12
    相关资源
    最近更新 更多