Reputation: 3
I have a unidimensional array filled up with graphic point coordinates that i get from a JSON encoded string, on which i
jsdata = $.parseJSON('[' + strdata + ']');
arr = jsdata[0].toString().split(',');
and i don't seem to quite think up the algorigthm to parse it to a 3 dimensional array, which needs to have the following structure:
data['parameter to plot'][point][coordinate]
As an example, let's say i'm gonna draw a line graph with data from 2 parameters. It will draw 74 points with 2 coordinates for each of the 2 lines which means the array's index will go as far as:
arr[296];
So, the "data" array's indexes will need to go as far as:
data[1][74][1];
I have no problem creating any of the arrays, but developing an algorithm for filling up the "data" array according to "arr's" length is really baking my brain. :/
Thanks for any help bros.
EDIT:
Since i don't know how many parameters i'm gonna need to draw graphs for but i'll know which type of graph i'm going to use, i came up with this answer.
I create a multi dimensional array using this function
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while(i--) arr[i] = createArray.apply(this, args);
}
return arr;
}
And then i can do
function toMulti(arr, plotType)
{
if(plotType =='line_plot')
{
var lineaux = createArray((arr.length/74/2),arr.length/2,2);
var cont = 0;
for(i= 0; i<lineaux.length; i++)
for(j=0; j< lineaux[i].length; j++)
for(k=0; k< lineaux[i][j].length;k++)
{
lineaux[i][j][k] = arr[cont];
cont +=1;
}
return lineaux;
}
}
But a line graph will not always have 74 points, nor will i always plot line graph..
Tell me what you think about it and if you can think up any way to make this non-plotType dependent i'd be eternally grateful. Or just buy you a beer to make it even. :D
Upvotes: 0
Views: 332
Reputation: 19294
You need to have two vars, parameterCount, and pointCount.
Then the size of your data array is 2*parameterCount*PointCount (in 2d).
function toMulti(data, parameterCount) {
var pointCount = data.length / ( 2 * parameterCount );
var res = [];
for (var pr=0 ; pr<parameterCount ; pr ++) {
var newLine = [];
for (var pt=0; pt<pointCount; pt++ ) {
var newPoint = [data[2*pr*pointCount+2*pt],data[2*pr*pointCount+2*pt+1]]
newLine.push(newPoint);
}
res.push(newLine);
}
return res;
}
Just to explain a little :
parameterCount is the... parameter count, and is the first dimension of the multi-dimensionnal array.
pointCount is the second dimension
the third dimension is 2D, the x and the y of the point.
The points are supposed to be organized like this within the array :
[ param1x1, param1y1, param1x2, param1y2, param1x3, param1y3, ... param1xpointCount, param1ypointCount, param2x1, param2y1, param2x2, param2y2, param2x3, .... , param2xpointCount, param2ypointCount,
..., paramparameterCountx1, paramparameterCounty1, ... paramparameterCountxpointCount, paramparameterCountypointCount]
(sorry i can't use here the math notation that would make all this look less messy.)
Edit : provided it is a 2D set of points, and you know the parameter count, you have
var pointCount = data.length / ( 2 * parameterCount );
i updated the code.
Upvotes: 2