1

I have the following dataset

Input:

dataset[0]=[{data:29, color:"y"},{data:44, color:"g"}]
dataset[1]=[{data:16, color:"r"},{data:23, color:"m"},{data:23, color:"b"}]

I am showing this information on bar chart, however bar chart attempting to group them. And it does not give me what I expect. http://jsfiddle.net/7dhb4jh0/1

Therefore,I need to have the following output before I feed my bar chart

The logic behind the desired output to match the length of two dataset by adding {data:0, color:null} There are two things involved unshift and push operations

Desired Output:

dataset[0]=[{data:29, color:"y"},{data:44, color:"g"},{data:0, color:null},{data:0, color:null},{data:0, color:null}]
dataset[1]=[{data:0, color:null},{data:0, color:null},{data:16, color:"r"},{data:23, color:"m"},{data:23, color:"b"}]

Initial Attempt

I have did it as follows, https://jsfiddle.net/s8mywm33/1/

dataset=[];
dataset[0]=[{data:29, color:"y"},{data:44, color:"g"}]
dataset[1]=[{data:16, color:"r"},{data:23, color:"m"},{data:23, color:"b"}]

sum1=dataset[0].length
sum2=dataset[1].length

for(i=0;i<sum1;i++)
{
    dataset[1].unshift({data:0, color:null})
}

for(i=0;i<sum2;i++)
{
   dataset[0].splice(2, 0, {data:0, color:null});
}

console.log(dataset[0]);
console.log(dataset[1]);

However is there a better way to do it?

casillas
  • 16,351
  • 19
  • 115
  • 215
  • 1
    Inserting at front is called an "array unshift" operation, and can be performed as `dataset[1].unshift({data:0, color:null},{data:0, color:null})`. See here: http://stackoverflow.com/a/8073687/410342. That's the big part of the issue; everything else is just `push()` (illustrated at the above link) or `splice()`, which is illustrated here: http://stackoverflow.com/a/586189/410342 – Shotgun Ninja May 29 '15 at 17:11
  • possible duplicate of [How can I add new array elements at the beginning of an array in JavaScript?](http://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript) – Shotgun Ninja May 29 '15 at 17:12
  • Please check my solution, is there a better way to do it? – casillas May 29 '15 at 17:29
  • I don't know if there is a better way to do this; because the intended location of the data isn't uniform, you can't really say where you should be inserting dummy values into your data without giving it some sort of form. Maybe making an object with specific data fields for each column in your graph, or making a dummy entry that can be placed in-place? – Shotgun Ninja May 29 '15 at 17:37

1 Answers1

1

As per my recommendations in the comments, there are a number of ways you could be doing this. The one I'd recommend is just using a dummy value (as I mentioned here):

var blank = {data:0, color:null};
var dataset = [];
dataset[0] = [{data:29, color:"y"}, {data:44, color:"g"}, blank, blank, blank];
dataset[1] = [blank, blank, {data:16, color:"r"}, {data:23, color:"m"}, {data:23, color:"b"}];

This makes your intentions visibly clear for what data should be where, and doesn't require that much extra code.

Community
  • 1
  • 1
Shotgun Ninja
  • 2,540
  • 3
  • 26
  • 32
  • However, I am looking for modular code. To handle various scenerios. I could able to add dummy data but there should be logic behind. I have upvoted your effort. – casillas May 29 '15 at 17:45
  • Remember, you can also embed flag values and functions into your objects; this might be an approach to adding logic into the system, where you either check the flag or call the function when processing the set of data, and the object itself determines what happens next. – Shotgun Ninja Feb 24 '16 at 19:38