简体   繁体   中英

Problem with FLOT Charts not drawing the graph

var dataprofit = [];
var databalance = [];
var datastops = [];
var aline = [];
var profit = [];
var balance = [];
var stop = [];


var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var jresp=JSON.parse(this.responseText);
        for (i=0; i<jresp.length; i++) {
            aline = jresp[i]
            profit = [aline['ticket'], aline['profit']];
            balance = [aline['ticket'], aline['balance']];
            stop = [aline['ticket'], aline['stop']];
            dataprofit.push(profit);
            databalance.push(balance);
            datastops.push(stop);
         }

        // EquityCounter.update(jresp["equity"]);
    }
};
xhttp.open("GET", "https://dashboard.luxtradingfirm.com/getvalues/1234", true);
xhttp.send(null);

var optionsSeries = {
    yaxis: { min: 14000, max: 16000 },
    xaxis: { show: false },
    grid: {
        tickColor: '#dddddd',
        borderWidth: 0,
    },
    legend: {position: 'sw'},
    colors: ['#008000','rgba(65,139,202,0.5)', '#FF0000']
};
var dataset = [
    {
        label: "Succeed",
        data: dataprofit,
        series: { shadowSize: 0 },
        lines: { fill: false },
    } , {
        label: "Balance",
        data: databalance,
        series: { shadowSize: 1 },
        lines: { fill: true, fillColor: { colors: [{ opacity: 1 }, { opacity: 0.1 }] } },
    }, {
        label: "Fail",
        data: datastops,
    }
];


$.plot($('#realtimechart'), dataset, optionsSeries);

This code reads a json object from the link, then puts it in 3 different arrays, reassembles the 3 arrays in 1 with the options for the Flot chart. However, the Flotchart does not draw. If I change the array for a handcoded array (in the dataset array), it works perfectly. What is wrong?

The assembling of the arrays and the plot function has to be inside the xhttp.onreadystatechange = function() {}

else it will assemble empty arrays since the API call hasn't finished yet.

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