2

Given myData is JSON var as follow:

({0:{qID:"3", id:"1", shortText:"Leisure", OpiCount:"0"}, 
  1:{qID:"3", id:"2", shortText:"Business", OpiCount:"3"}, 
  2:{qID:"3", id:"3", shortText:"University visit", OpiCount:"1"}, length:3})

To transform into float data array:

var data = [];
myData.each(function(i,v) {  
    data[i] = { label: v.shortText, data: v.OpiCount } ; 
})

The above code break because of the white space in v.shortText. How can I overcome this?

Seanny123
  • 6,594
  • 11
  • 56
  • 106
Gian
  • 636
  • 4
  • 11
  • 20
  • 1
    Those parantheses `( )` should make that JSON unparseable. Unless you mean it's an object and you left out the `new Object` part. – Jan Oct 22 '12 at 04:33
  • 2
    there is no `each` method in `javascript`. are you looking for `jQuery` – diEcho Oct 22 '12 at 04:35
  • I don't think flot cares if the label contains whitespace. The first example in the API documentation has the label "y = 3", which contains spaces. – Barmar Oct 22 '12 at 04:37

4 Answers4

1

Thanks for #Jan and #diEcho, I have figured out.

my problem is flot needs numeric data. Just add parseFloat(v.OpiCount)

var data = [];
myData.each(function(i,v) {  
    data[i] = { label: v.shortText, data: parseFloat(v.OpiCount) } ; 
})

Regards.

Gian
  • 636
  • 4
  • 11
  • 20
0

i ran into this problem translating text for image processing...what i did was make a base_64 encode on the server end and did a javascript Base64 decode on the client end

Community
  • 1
  • 1
we.mamat
  • 667
  • 9
  • 20
0

.each method is in jQuery and its working perfect, try

var myData = ({0:{qID:"3", id:"1", shortText:"Leisure", OpiCount:"0"}, 
  1:{qID:"3", id:"2", shortText:"Business", OpiCount:"3"}, 
  2:{qID:"3", id:"3", shortText:"University visit", OpiCount:"1"}, length:3});
var data = [];
$.each(myData, function(i,v) {
    console.log(v);
    data[i] = { label: v.shortText, data: v.OpiCount } ; 

})
    console.log(data);
})
diEcho
  • 50,018
  • 37
  • 156
  • 230
0

Firstly, that isn't valid JSON. The attribute names should have quotes around them like "qID":"3". Also there shouldn't be any brackets. The round brackets are used in JSONP.

Then the next issue is how is the JSON stored? Normally json is transported as a string so it is serialised before sending and then unpacked on the other end. So you will need to work out where it is coming from. The 'json' you provided looks like it is hard coded but as a json object which you can see running here:

http://jsfiddle.net/VYGY5/

So you'll need to work out where its coming from and in what format and make sure valid JSON. If its a string you could use something like jQuery.parseJSON to turn a JSON string into javascript objects and then do your conversion to flot array. Otherwise just use the object.

Next a flot array is an array of array(s). So it should be like this:

[ [0,0], [0,2] ].

So your code should be:

data[i] = [pointX, pointY];

Make sure you read the doc page here in relation to the flot format:

https://github.com/flot/flot/blob/master/API.md

Matt
  • 448
  • 6
  • 13