|
|
@@ -6,9 +6,6 @@
|
|
|
// xName = a string for the name of the X axis
|
|
|
class LineGraph{
|
|
|
constructor(canvas, yName, xName){
|
|
|
- canvas.height = canvas.clientHeight;
|
|
|
- canvas.width = canvas.clientWidth;
|
|
|
-
|
|
|
this.canvas = canvas;
|
|
|
this.context = canvas.getContext("2d");
|
|
|
this.left = canvas.clientWidth - (canvas.clientWidth * 0.9);
|
|
|
@@ -220,4 +217,63 @@ class LineGraph{
|
|
|
|
|
|
this.context.fillStyle = "black";
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+class HorizontalBarGraph{
|
|
|
+ constructor(canvas){
|
|
|
+ this.canvas = canvas;
|
|
|
+ this.context = canvas.getContext("2d");
|
|
|
+ this.left = 0;
|
|
|
+ this.right = canvas.clientWidth;
|
|
|
+ this.top = canvas.clientHeight - (canvas.clientHeight * 0.99);
|
|
|
+ this.bottom = canvas.clientHeight;
|
|
|
+ this.data = [];
|
|
|
+ this.max = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Adds an array of data points to the chart
|
|
|
+ //All data is removed and redrawn when called
|
|
|
+ //Must pass in all data points
|
|
|
+ //Inputs:
|
|
|
+ // dataArray: array of objects
|
|
|
+ // num: number for the actual data
|
|
|
+ // label: text to display on bar
|
|
|
+ addData(dataArray){
|
|
|
+ let data = [];
|
|
|
+ this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
+
|
|
|
+ for(let point of dataArray){
|
|
|
+ if(point.num > this.max){
|
|
|
+ this.max = point.num;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.data.push(point);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.drawGraph();
|
|
|
+ }
|
|
|
+
|
|
|
+ drawGraph(){
|
|
|
+ let barHeight = (this.bottom - this.top) / this.data.length;
|
|
|
+
|
|
|
+ for(let i = 0; i < this.data.length; i++){
|
|
|
+ let topLocation = this.top + (i * barHeight) + 5;
|
|
|
+ let width = (this.right - this.left) * (this.data[i].num / this.max);
|
|
|
+
|
|
|
+ if(this.data[i].num >= this.max){
|
|
|
+ this.context.fillStyle = "rgb(255, 99, 107)";
|
|
|
+ }else{
|
|
|
+ this.context.fillStyle = "rgb(179, 191, 209)";
|
|
|
+ }
|
|
|
+
|
|
|
+ this.context.beginPath();
|
|
|
+ this.context.fillRect(this.left, topLocation, width, barHeight - 5);
|
|
|
+ this.context.stroke();
|
|
|
+
|
|
|
+ let textLocation = 15;
|
|
|
+ this.context.font = "12px Saira";
|
|
|
+ this.context.fillStyle = "black";
|
|
|
+ this.context.fillText(this.data[i].label, textLocation, (this.top) + (i * barHeight) + (barHeight / 1.5));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|