0
votes

I try to get series and data through ajax Function to retrieve data, in Document.ready:

   function requestData() {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: xxxxx,
                async: false,
                // you can use an object here
                success: function(data) {
                    $("html").css('cursor','auto');
                    //remove previous data
                    while(chart.series.length > 0)
                        chart.series[0].remove(true);
                    $.each(data, function(val, text) {
                        counter = 0;
                        $.each(text, function() {
                            if (counter==0) {
                                datoscol=  "[" + this + "]";
                            }
                            else{
                                datoscol= datoscol + "," + "[" + this + "]";
                            }
                            counter=1
                        });

                        datoscol = "["+datoscol + "]";
                        alert (datoscol);
                        chart.addSeries({
                            name: val,
                            data: datoscol
                        });

                    });   
                    chart.redraw();
                },
                cache: false
            });
        }    

I also initialize Highcharts in document.ready:

chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container2',
            type: 'line',
            pointInterval: 30*24 * 3600 * 1000,
            events: {
                load: function(){
                    chart = this;
                    requestData();
                }
            }

        },

        title: {
            text: 'Facturación clínica'
        },
        credits: {
            enabled: false
        },
        xAxis: {
            type: 'datetime',
            maxZoom: 14 * 24 * 3600000, // fourteen days
            title: {
                text: null
            }
        },
        yAxis: {
            title: {
                text: 'Facturación (€)'
            }

        },
        tooltip: {
            formatter: function() {
                var s;
                if (this.point.name) { // the pie chart
                    s = ''+
                        this.point.name +': '+ this.y +' fruits';
                } else {
                    s = ''+
                        this.y + " Euros";
                }
                return s;
            }
        },
        labels: {
            items: [{
                    html: 'Facturación clínica',
                    style: {
                        left: '40px',
                        top: '8px',
                        color: 'black'
                    }
                }]
        }
    });

Data I get from the server is something like: {"Employee1":[["1356908400000","10.00"],["1359586800000","11.00"], ["1362006000000","12.00"],["1364684400000","13.45"]],"Employee2":[["1356908400000","10.00"],["1359586800000","11.00"],["1362006000000","12.00"],["1364684400000","13.45"]]}

So when I add the serie to highcharts, the values are, for example: chart.addSeries({ name: val, --------->Employee1 data: datoscol ------->[["1356908400000","10.00"],["1359586800000","11.00"], ["1362006000000","12.00"],["1364684400000","13.45"]]

I get no error through firebug console but nothing appear, except the axis and legends. I get all the series names but nothing about the data, no markers, no lines.

1

1 Answers

0
votes

The data you are trying to supply to Highcharts (variable datoscol) is a string. In your case, you must supply an object and the contents of each array must also be numbers.

The best way, I believe, is to fix your server response.

If you can't, the easy, dirty and potentially dangerous way to do it is to eval the datoscol variable as you supply it to Highcharts:

chart.addSeries({
    name: val,
    data: eval(datoscol)
});

See it running here.

Another way is to convert each array element to an integer (the datetime) and to a float (the amount). So, instead of your current $.each, you could write this:

for (var seriesName in data) {
    var curData = data[seriesName];
    var curDataLen = curData.length;
    for (var i = 0; i < curDataLen; i++) {
        curData[i][0] = parseInt(curData[i][0], 10);
        curData[i][1] = parseFloat(curData[i][1]);
    }
    chart.addSeries({ name: seriesName, data: curData });
}

See it running here.