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

1 回答 1

2

您需要有两个变量,parameterCount 和 pointCount。
那么你的数据数组的大小是 2*parameterCount*PointCount (在 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;
}

稍微解释一下:
parameterCount是...参数计数,是多维数组的第一维。
pointCount 是第二维
,第三维是 2D,点的 x 和 y。

这些点应该在数组中这样组织:
[ param1x1, param1y1, param1x2, param1y2, param1x3, param1y3, ... param1xpointCount, param1ypointCount, param2x1, param2y1, param2x2, param2y2, param2x3, .... , param2xpointCount, param2ypointCount, ..., paramparameterCountx1, paramparameterCounty1, ... paramparameterCountxpointCount, paramparameterCountypointCount]

(对不起,我不能在这里使用数学符号,这会使这一切看起来不那么凌乱。)

编辑:如果它是一组 2D 点,并且您知道参数计数,那么您有

 var pointCount = data.length / ( 2 * parameterCount );

我更新了代码。

于 2013-09-13T11:27:15.167 回答