A Bar class is used to create a bar chart. In the constructor, we initialize most values, such as data.
The data is an array of length 10, each entry being an array of length 2, with state name and state population in millions, which will be the x and y values. There are logging of values, and setting debug to true or false will lead to text rectangles being shown or not. The xAxis()
and yAxis() as well as bars() function can be combined into a draws function. However, if you should comment out the axis function calls, axis will not be shown.
// 5. Bar Chart
let canvas = document.querySelector("#myCanvas");
let ctx = canvas.getContext("2d");
class Bar {
constructor(params) {
this.data = data;
this.offx = params.off.x;
this.offy = params.off.y;
this.width = params.width;
this.ticks = params.ticks;
this.tickWidth = params.tickWidth;
this.max = params.max;
this.font = params.font;
this.style = params.style;
this.x = this.offx + this.width;
this.y = this.offy + this.ticks * this.width
this.totWidth = this.width * (3/2 * this.data.length + 2);
this.debug = params.debug;
console.log("Width of bar graph: " + this.totWidth);
console.log("From: " + this.offx);
console.log("To: " + (this.offx + this.totWidth));
}
yAxis() {
ctx.font = this.font;
ctx.textAlign = "center";
ctx.fillStyle = this.style;
ctx.strokeStyle = this.style;
ctx.lineWidth = 2;
for(let i = 0; i<this.ticks; i++) {
let textVal = this.max - i/(this.ticks-1) * this.max;
console.log(textVal);
if (this.debug) {
ctx.strokeRect(this.offx, i*this.width + this.offy,
this.width, this.width);
}
let posY = (i+1)*this.width + this.offy;
console.log("i = " + i + ", posY = " + posY);
ctx.fillText(textVal+"",this.offx + this.width/2, posY );
ctx.beginPath();
ctx.moveTo(this.x, posY);
console.log(this.x);
ctx.lineTo(this.x + this.tickWidth, posY);
console.log(this.x + this.tickWidth);
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(this.x,this.offy);
ctx.lineTo(this.x,this.offy + this.ticks * this.width);
ctx.stroke();
}
xAxis() {
ctx.beginPath();
ctx.moveTo(this.offx + this.width, this.y);
ctx.lineTo(this.offx + this.totWidth, this.y);
ctx.stroke();
ctx.textAlign = "center";
for(let i=0; i<this.data.length; i++) {
let posX = this.x + this.width/2 * (3 * i + 1);
console.log("i = " + i + ", posX = " + posX);
if (this.debug === true) {
ctx.strokeRect(posX, this.y, this.width, this.width);
}
ctx.fillText(this.data[i][0], posX + this.width/2,
this.y + this.width/2, this.width);
}
}
bars(style) {
ctx.fillStyle = style;
for(let i=0; i<data.length; i++) {
let posX = this.x + this.width/2 * (3 * i + 1);
let val = data[i][1];
let height = val/this.max*(this.ticks-1)*this.width;
ctx.fillRect(posX, this.offy + this.width*this.ticks - height,
this.width, height);
}
}
}
let data = [ ["California",39.250017],
["Texas",27.862596],
["Florida",20.612439],
["New York",19.745289],
["Illinois",12.801539],
["Pennsylvania",12.784227],
["Ohio",11.646273],
["Georgia",10.310371],
["North Carolina",10.146788],
["Michigan",9.928301] ];
bar = new Bar({
data: data,
font: "14px Comic",
style: "purple",
off: {x:30, y:40},
width: 70,
ticks: 5,
tickWidth: 10,
max: 50,
debug: false
});
bar.yAxis();
bar.xAxis();
bar.bars("blue");
Output (debug is true):
Output (debug is false):