I have the following arrays that a want to display as a stacked bar chart with p5js (javascript).
Below you can see an arrayOfMonths (month1, month2,...) that is going to be displayed on the X Axis of the Stacked Bar Chart. Now, Each month has a number of other (0-to many) elements. Those are the elements that I want to display on Y Axis so for example month1 has 2 elements i want to create two rectangles on the first element on X Axis and so on as shown below:
4| ▭
3| ▭ ▭
2| ▭ ▭ ▭
1| ▭ ▭ ▭ ▭
▬▬▬▬▬▬▬▬▬▬▬▬▬▬
m1 m2 m3 m4
let me know if you know how to handle 2Dimensional Arrays :) Thanks...
/* Needed Variables Declaration */
var month1 = ["M01V01","M01V02"];
var month2 = ["M02V01","M02V02","M02V03","M02V04"];
var month3 = ["M03V01"];
var month4 = ["M04V01","M04V02","M04V03"];
var arrayOfMonths = [month1,month2,month3,month4];
var arrayOfResponses = ["1", "2", "3","4","5"];
function addValues(){
var locX = canvasWidth-(canvasWidth-125);
var locY = canvasHeight-25;
var barWidth = 50;
var barData = canvasHeight/(numOfResponses+2);
for(var x=0; x<arrayOfMonths.length; x++){
/* Draw Stack Bars (rectangles) */
for(var y=0; y<arrayOfMonths[x].length; y++){
/* ADD TEXT TO X AXIS */
if(y<=x){
locX = canvasWidth-(canvasWidth-125);
locY -= 125;
text(arrayOfMonths[x][y], locX, locY);
fill(random(255),random(255),random(255));
rect(locX, locY, barWidth, barData);
}
// locX+=canvasWidth/(numOfMonths+1);
locX+=100;
//locY -= 150;
}
//locX = canvasWidth-(canvasWidth-125);
//locY = canvasHeight-210;
}
}