简体   繁体   中英

Add two json data series in one highchart

I'm using highcharts time series , I need to add another data series populated from a different json file/link, is that possible?

Here is my code:

$(function () {
    $.getJSON('myfirstjson', function (data) {
        mydata = [];
        $('#tvmtgm').highcharts({
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'TVM/TGM'
            },
            xAxis: {
                type: 'datetime',

                dateTimeLabelFormats : {
                    hour: '%I %p',
                    minute: '%I:%M %p'
                }
            },
            yAxis: {
                title: {
                    text: 'Moves'
                }
            },
            legend: {
                enabled: false
            },
            plotOptions: {
                area: {
                    fillColor: {
                        linearGradient: {
                            x1: 0,
                            y1: 0,
                            x2: 0,
                            y2: 1
                        },
                        stops: [
                            [0, Highcharts.getOptions().colors[0]],
                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                        ]
                    },
                    marker: {
                        radius: 2
                    },
                    lineWidth: 1,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    },
                    threshold: null
                }
            },

            series: [{
                type: 'area',
                name: 'Total vessel moves',
                data: data
            }
            ]
        });
    });
});

There are examples on the highcharts website, but they are all static data series, I need to have both data series populated from json requests

Yes you can. Of course, I am hoping I understood your problem.

This link shows you how you can add multiple y-axes. As for getting the data. $.when should help you.

var data1, data2;
$.when(
    $.getJSON(onurl1, function(data) {
        data1 = data;
    }),
    $.getJSON(onurl2, function(data) {
        data2= data;
    })
).then(function() {
    //use data1 and data2
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM