0

I have a jquery graph which takes in data in sets for example

data: [
            [ vara,varb,varc,vard ],

I have an array of numbers such as

n = [1,2,3,4,5,6,7,8]

I want to graph to take the first four numbers, then if theres any left over make a new data set. The end result graph data should represent

data: [
          [1,2,3,4],
          [5,6,7,8],
      ]

The whole point of doing so is that i wont have to manually enter data into the graph it would be read from the array.

jumpman8947
  • 249
  • 3
  • 6
  • 13
  • The actual thing you are trying to do is not specific to jQuery. It is "how do I split an array into chunks in Javascript?" - see [this question](http://stackoverflow.com/questions/8495687/split-array-into-chunks) and duplicates of it. – GregL Jan 25 '16 at 04:11
  • Possible duplicate of [Split array into chunks](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – GregL Jan 25 '16 at 04:12
  • The issue isn't splitting it into chunks, the issue is getting the chart to automatically fill in data. – jumpman8947 Jan 25 '16 at 04:13
  • Can you explain more what you mean by "automatically fill in data"? If you pass it `[1,2,3,4,5,6,7,8]`, what data do you expect will be automatically filled in? – GregL Jan 25 '16 at 04:18
  • The chart data takes in variables as four at a time, i cannot pass in the whole array, it will not automatically carry over anything. Splitting up this is array is not an issue. I just need sets to be made without actually typing `[9,10,11,12]` – jumpman8947 Jan 25 '16 at 04:23
  • I need the chart, or a function to read the array, and generate its own data sets. – jumpman8947 Jan 25 '16 at 04:25
  • I can split the initial array, but after that how will it get passed to the chart data sets? – jumpman8947 Jan 25 '16 at 04:25
  • What jQuery graph lib you are using? You need to make changes in that lib as per your requirement. Changes like the one answered below by Jeremy Jackson. – Savaratkar Jan 25 '16 at 05:02

1 Answers1

0

You can build out your array 4 at a time like this:

var data = [[]];
var n = [1,2,3,4,5,6,7,8];

var counter = 0;
var dataIndex = 0;
for(var i = 0; i < n.length; i++){
    if(counter > 0 && counter % 4 == 0){
        dataIndex++;
        data[dataIndex] = [];
    }
    data[dataIndex].push(n[i]);
    counter++;
}

Opening the console and running it yields the results you expect.

Jeremy Jackson
  • 2,247
  • 15
  • 24
  • how will i get the result to the chart? – jumpman8947 Jan 25 '16 at 04:44
  • The chart just needs the array to look like `[[1,2,3,4],[5,6,7,8]]` right? – Jeremy Jackson Jan 25 '16 at 04:48
  • Yes, so in order to get the information to the data set, all i would pass is `data[dataIndex], data[dataIndex]. The information needs to get sent to the chart data set, which is in another function. – jumpman8947 Jan 25 '16 at 04:58
  • So you could use the code I gave you and set `data: getDataSetFromArray([1,2,3,4....n])`, where getDataSetFromArray is a function that can return the values formatted how you need them. – Jeremy Jackson Jan 25 '16 at 05:02