Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm totally new to objects,

I need to push objects to get this structure:

var data = [
            {x: 0, y: 91},
            {x: 1, y: 90},
            {x: 2, y: 89.7},
            {x: 3, y: 89.5},
            {x: 4, y: 89.2},
            {x: 5, y: 88.2},
            {x: 6, y: 88.4},
            {x: 7, y: 87.9},
            {x: 8, y: 87.2},
            {x: 9, y: 87.0},
            {x: 10, y: 87.2},

        ];

trying this way

for (var i=0; i<result.rows.length; i++) {

var row = result.rows.item(i);

data.push({x: i, y: row.weight});console.log(data[i]);

}

here is the jsfiddle example, not the whole code with SQLite database, just the part I am not able to manage, what I am trying to do is to feed the chart dynamically, but looks like I am not able to push the 'data' array in the right way (if the problem is there, because the push code looks good).

http://jsfiddle.net/zeroshadow/WDEUz/3/

that's what I get from the console when I push the data in:

Object {x: 1, y: 65} 
Object {x: 2, y: 64.9}
Object {x: 3, y: 64.8}
Object {x: 4, y: 64.2}
Object {x: 5, y: 64.3}
Object {x: 6, y: 64.5}
Object {x: 7, y: 64.3} 
Object {x: 8, y: 64} 
Object {x: 9, y: 63.8}
Object {x: 10, y: 63.1}

looks OK but then the chart does not like it and all is vanished. As I have no knowledge of objects, I thought the array after the push has something wrong, because the data array in its initial state works.

share|improve this question

closed as off-topic by Felix Kling, Oriol, Davin Tryon, EdChum, Reuben Mallaby Mar 4 at 11:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself." – Felix Kling, Oriol, Davin Tryon, EdChum, Reuben Mallaby
If this question can be reworded to fit the rules in the help center, please edit the question.

4  
What doesn't work? What happens? Do you see any errors in the console? What is result? What is result.rows? Even though you have included the code, you haven't provided any other useful information that is necessary to solve whatever problem you have. –  Felix Kling Mar 1 at 19:48
2  
@deemeetree That shouldn't have any effect. –  Oriol Mar 1 at 19:55
    
Just to illustrate my point, I can take the code you posted without any modifications and "make it work": jsfiddle.net/E4TLf. So if what you posted doesn't work for you, it's because result or result.rows isn't what you think it is, or because of any other problem with code you didn't post. –  Felix Kling Mar 1 at 19:55
    
In the for loop you are checking against the length of result.rows, You want to know how many rows you have in the result, so you check the length of result.rows. You then assign the row variable to result.rows.item(i). I don't know what item() is, but I would think you want to do var row = result.rows[i] to get the correct data object. –  alexK85 Mar 1 at 19:56
    
The code is right, nothing wrong with it, it was me overlooking the range settings of the chart, so I was simply pushing data out of the range and the chard couldn't display. Anyway thanks because knowing the code was good, I was able to look around and discover what was wrong. –  sasha Mar 2 at 8:49

2 Answers 2

up vote 1 down vote accepted

You have used: console.log(data[i]); in the for loop and probably you've saw how objects got pushed into the array. Make sure you console.log(data) outside your for loop (just for debugging). Your data.push({x: i, y: row.weight}) is correct. Maybe row.weight it's not what you think. Also make sure that result.rows.length is 10. You should give us a fiddle or more informations if you need better support.

share|improve this answer
    
Radu thanks for the comment, knowing I was on the right way helped me focusing elsewhere, and I found the bug! –  sasha Mar 2 at 8:46
    
I'm glad it helped. Good luck and thank you for accepting my comment –  radubogdan Mar 2 at 9:31

Sorry guys, like often happen I overlooked the data range settings in the chart. My fault my misunderstanding. Sorry again. The code works.

in the Jsfiddle example you can see:

y = pv.Scale.linear(82, 92).range(0, h);

and I am just pushing data out of the range from 65 to 60. I adjusted the range:

y = pv.Scale.linear(59, 66).range(0, h);

and all is like expected.

Thank you to all and sorry for the waste of time.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.