A B
A B

Reputation: 597

highcharts chart showing only last element in series

I am plotting multiple series in highcharts by referencing json data. the json data is configured as follows:

{"data": {"words with friends": [[1365034819.212261, 107], [1366071619.212278, 224]], "snacksss": [[1364775619.212285, 786], [1366849219.212291, 445]], "ticktackloot": [[1365207619.212297, 85], [1367799619.212303, 803]], "bike race": [[1364343619.212309, 293], [1367886019.212315, 572]]}}

where the first and only element in each dictionary is a list of list. each list of lists contains a list with a timestamp and an arbitrary value. The key for each dictionary represents a series. The problem is that the series is only plotting the last list in each list of lists. The timestamp is on the x axis and arbitrary value on the y. I end up with y points that are 224, 445, 803, and 572. I want them all to show up. The code is below:

var chart;  // global
function requestData() 
{
    $.ajax(
    {
        url: 'test.json',
        success: function(response) 
        {
            // SINCE response IS TURNED INTO AN OBJECT LITERAL
            // ASSOCIATIVE ARRAY, WE CAN ACCESS data
            for (var key in response.data)
            {
                //extract every timestamp and multiply it by 1000
                //because JS processes timestamps in microseconds.
                //Reinsert in array
                for(var counter=0;counter<response.data[key].length;counter++)
                {
                    insert_array=[]
                    //insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
                    insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])

                    if(counter==response.data[key].length-1)
                    {
                        var seriesData=insert_array;

                        var seriesName = key;

                        console.log("   processing " + seriesName + "=" + seriesData);
                        var series = 
                        {
                            name: seriesName,
                            data: seriesData,
                            lineWidth: 15
                        };

                        chart.addSeries(series);
                    }
                }
            }
        },
        cache: false
    });
}

$(document).ready(function()
{
    console.log("preparing document...");
    chart = new Highcharts.Chart(
    {
        chart: 
        {
            renderTo: 'container1',
            type: 'bar',
            events: 
            {
                load: requestData
            }
        },
        title: 
        {
            text: 'Top Offers'
        },
        xAxis: 
        {
            type: 'datetime',
            title:
            {
                text: 'Offers'
            }
        },
        yAxis: 
        {
            title:
            {
                text: 'Rewards'
            }    
        },
        series: []
    });   
 });

Upvotes: 2

Views: 674

Answers (1)

Irvin Dominin
Irvin Dominin

Reputation: 30993

Try to change the cody inside loop like this:

//extract every timestamp and multiply it by 1000 because JS processes timestamps in microseconds. Reinsert in array
insert_array=[]

for(var counter=0;counter<response.data[key].length;counter++)
    {                      
        //insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
        insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
    ...

In this way the insert_array variable will be defined once per list and filled correctly; with you actual code the variable is cleaned at every loop so it will carry only the last value.

Upvotes: 1

Related Questions