0

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

4
  • How is the original array structured? Commented Sep 13, 2013 at 11:16
  • It isn't. it's something like arr[0] = 'x coordinate', arr[1]= 'y coordinate' and so on. Commented Sep 13, 2013 at 11:18
  • How "so on"? arr[2] = 'z coordinate', or arr[2] = 'second x coordinate', or what is it? Show us a sample, maybe by JSON.stringify(arr, null, 4) Commented Sep 13, 2013 at 11:25
  • Sorry i led you to ambiguous interpretation. :/ yes, arr[2]='2nd x coordinate', arr[3]='2nd y coordinate', arr[4]='3rd x coordinate'. arr is already parsed from a JSON string, by using jQuery.parseJSON and spliting commas. could i parse it any other way that would make this simpler? Commented Sep 13, 2013 at 13:37

1 Answer 1

2

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.

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

4 Comments

I won't know how many parameters i'm gonna plot data for. I need to get that from arr[]'s index.
newPoint ou thisPoint?
Well, i may have a way to know how many parameters i'll have, but no PointCount, for sure... :/
though your answer is correct, my question is now irrelevant. I went around all this parsing logic and managed to use jQuery's $.getJSON(), which returns me what i want. Nonetheless, i'll mark your answer as the answer.

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.