graphs.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class LineGraph{
  2. constructor(canvas, data, yName, xName, xData){
  3. canvas.height = canvas.clientHeight;
  4. canvas.width = canvas.clientWidth;
  5. this.context = canvas.getContext("2d");
  6. this.left = canvas.clientWidth - (canvas.clientWidth * 0.9);
  7. this.right = canvas.clientWidth * 0.9;
  8. this.top = canvas.clientHeight - (canvas.clientHeight * 0.9);
  9. this.bottom = canvas.clientHeight * 0.9;
  10. let max = data[0];
  11. for(let point of data){
  12. if(point > max){max = point;}
  13. }
  14. this.verticalMultiplier = (this.bottom - this.top) / max;
  15. this.horizontalMultiplier = (this.right - this.left) / data.length;
  16. xData.dataLength = data.length;
  17. this.drawYAxis(yName, max);
  18. this.drawXAxis(xName, xData);
  19. this.addLine(data);
  20. }
  21. addLine(data){
  22. let redRand = Math.floor(Math.random() * 200);
  23. let greenRand = Math.floor(Math.random() * 200);
  24. let blueRand = Math.floor(Math.random() * 200);
  25. for(let i = 0; i < data.length - 1; i++){
  26. this.context.beginPath();
  27. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * data[i]));
  28. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * data[i + 1]));
  29. this.context.lineWidth = 1;
  30. this.context.strokeStyle = `rgb(${redRand}, ${greenRand}, ${blueRand})`;
  31. this.context.stroke();
  32. }
  33. }
  34. drawXAxis(xName, xData){
  35. this.context.beginPath();
  36. this.context.moveTo(this.left, this.bottom);
  37. this.context.lineTo(this.right, this.bottom);
  38. this.context.lineWidth = 2;
  39. this.context.stroke();
  40. this.context.font = "25px Arial";
  41. this.context.fillText(xName, this.right / 2, this.bottom + 50);
  42. this.context.setLineDash([5, 10]);
  43. this.context.font = "10px Arial";
  44. this.context.lineWidth = 1;
  45. if(xData.type = "date"){
  46. let diff = Math.abs(Math.floor((Date.UTC(xData.start.getFullYear(), xData.start.getMonth(), xData.start.getDate()) - Date.UTC(xData.end.getFullYear(), xData.end.getMonth(), xData.end.getDate())) / (1000 * 60 * 60 * 24)));
  47. let showDate = xData.start;
  48. for(let i = 0; i < xData.dataLength; i += Math.floor(xData.dataLength / 10)){
  49. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  50. this.context.beginPath()
  51. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  52. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  53. this.context.strokeStyle = "#a5a5a5";
  54. this.context.stroke();
  55. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  56. }
  57. }
  58. this.context.strokeStyle = "black";
  59. this.context.setLineDash([]);
  60. }
  61. drawYAxis(yName, max){
  62. this.context.beginPath();
  63. this.context.moveTo(this.left, this.top);
  64. this.context.lineTo(this.left, this.bottom);
  65. this.context.lineWidth = 2;
  66. this.context.stroke();
  67. this.context.font = "25px Arial";
  68. this.context.fillText(yName, 0, this.bottom / 2);
  69. this.context.setLineDash([5, 10]);
  70. this.context.font = "10px Arial";
  71. this.context.lineWidth = 1;
  72. let axisNum = 0;
  73. let verticalIncrement = (this.bottom - this.top) / 10;
  74. let verticalOffset = 0;
  75. do{
  76. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  77. this.context.beginPath();
  78. this.context.moveTo(this.left, this.bottom - verticalOffset);
  79. this.context.lineTo(this.right, this.bottom - verticalOffset);
  80. this.context.strokeStyle = "#a5a5a5";
  81. this.context.stroke();
  82. verticalOffset += verticalIncrement;
  83. axisNum += max / 10;
  84. }while(verticalOffset <= (this.bottom - this.top));
  85. this.context.strokeStyle = "black";
  86. this.context.setLineDash([]);
  87. }
  88. }