Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yagran

macrumors 6502a
Original poster
Jan 8, 2007
718
2
Brighton, East Sussex, UK
Hi, i've got myself really confuse with this all now and i could do with an outsider having a look...

I am plotting graphs using Flot for jQuery, it likes its data set in the following way:
Code:
var d1 = [[1224690646000, 4], [1224690663000, 4], [1224690672000, 1], [1224693751000, 4], [1224693166000, 4]]
where each item of the aray has an x any y coordinate.

I have a data set which is created by php and written to a text file like this:
Code:
1224690646000, 4+1224690663000, 4+1224690672000, 1+1224693751000, 4+1224693166000, 4

And then im using a xmlhttprequest to grab the textfile and put it into the dataText variable, then im trying to split that into an array ready to plot:
Code:
dataText = oXML.responseText;
dataTextSplit = dataText.split("+");

Its not working right, and im pretty sure its because im not splitting the text file into the array properly but i cant get my head around how it should be done?

any help would be truly fantastic!
 
That code will only give you a single dimension array rather than a 2D array like you want. If I get a chance later I'll give you some code for it, but don't have time at the moment. Hopefully that hint will get you in the right direction though.
 
Well guess no one else chimed in. Here's a solution:

PHP:
var dataText = '1224690646000, 4+1224690663000, 4+1224690672000, 1+1224693751000, 4+1224693166000, 4';
var tmp = dataText.split('+');
var dataTextSplit = new Array();
for (var x=0, y=tmp.length; x<y; x++) {
  dataTextSplit[x] = tmp[x].split(', ');
}

You of course would need to swap in your oXML.responseText part at the top instead of the raw data. I just used it for testing purposes. Enjoy.
 
Well guess no one else chimed in. Here's a solution:

PHP:
var dataText = '1224690646000, 4+1224690663000, 4+1224690672000, 1+1224693751000, 4+1224693166000, 4';
var tmp = dataText.split('+');
var dataTextSplit = new Array();
for (var x=0, y=tmp.length; x<y; x++) {
  dataTextSplit[x] = tmp[x].split(', ');
}

You of course would need to swap in your oXML.responseText part at the top instead of the raw data. I just used it for testing purposes. Enjoy.

Thank you, you're a godsend!
Arrays do my head in!
had to make a quick change (global variables were needed), but other than that absolutely perfect!

thanks to everyone else that chipped in too!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.