0

I'm using jqPlot to build some dynamic graphs.

jqPlot takes a 2D array for its data, thus:

$.jqplot('chartdiv',  [[[1, 2],[2,5.12],[3,13.1],[7,33.6],[9,85.9],[21,219.9]]],
{ title:'progress',
  axes:{yaxis:{min:0, max:240}},
  series:[{color:'#5FAB78'}]
});

The first number is the x axis (in this case 1,2,3,7,9 and 21), the second number is the y axis. chartdiv is the html element where the graph is displayed, the rest is self explanatory.

I want to take data from my localStorage and push it to an array that will allow me to display it as a graph, and I can't work out how to do this.

If my array is declared with

var graphdata = [];

how to I push the data into it? I'm using a loop like this to get to the content:

var dt          = new Date();
var today       = dt.getDateKey();
var df          = getStartDate(period);
var datefrom    = df.getDateKey();  
var graphdata = [];
alert("Today is " + today + " and the graph will be calculated from " + datefrom);
while (df<=dt) {                
    dataObject = JSON.parse(localStorage.getItem(df.getDateKey()));     
    if(dataObject){
        var dateposition = df.getDateKey();
        console.log("Date is " + dateposition);
        console.log("The Weight is " + dataObject.savedobs1);
        graphdata.push([dateposition,dataObject.savedobs1]);            
    }
    df.setDate(df.getDate() + 1);
};
console.log("Graph data " + graphdata);     

I need to get the dateposition and the data into the chart and I can't work out how to get it into the right format. The console.log of the graph data just gives:

Graph data ,26/04/2014,118,27/04/2014,116,28/04/2014,116,29/04/2014,105 

1 Answer 1

3

Your current setup is appending your data after the first empty array in your top-level array.

If the question is really about how to push data into that nested array, just wrap it when you're done.

// create an empty array
var list = [];

// push data
list.push([1,2]);
list.push([3,4]);
list.push([5,6]);

// wrap the result before you return it
console.log([list]);
    // returns [[[1,2],[3,4],[5,6]]]

DEMO

output: enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

I've changed the array declaration and I am using the same method as you. I don't get your output though, and your fiddle demo does nothing for me - i press Run and nothing happens at all.
Acutally, I checked in the console (thought itd show up in fiddle) and I dont get your result, I get 1,2,3,4,5,6 which is what I was as getting before.
@SubjectiveEffect what browser are you using? See my edit; output in Chrome.
Chrome. I'll try with others tomorrow.
I've tried it in Chrome, Safari, Firefox and Opera. I get the same thing - without the square brackets. Now that doesn't mean the data is necessarily in the wrong format but if it isn't how are you getting the output you listed? You code works fine. Look at mine (now I've edited it) what is different?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.