graphs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. console.log(data);
  17. xData.dataLength = data.length;
  18. this.drawYAxis(yName, max);
  19. this.drawXAxis(xName, xData);
  20. this.addLine(data);
  21. }
  22. addLine(data){
  23. let redRand = Math.floor(Math.random() * 200);
  24. let greenRand = Math.floor(Math.random() * 200);
  25. let blueRand = Math.floor(Math.random() * 200);
  26. for(let i = 0; i < data.length - 1; i++){
  27. this.context.beginPath();
  28. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * data[i]));
  29. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * data[i + 1]));
  30. this.context.lineWidth = 1;
  31. this.context.strokeStyle = `rgb(${redRand}, ${greenRand}, ${blueRand})`;
  32. this.context.stroke();
  33. }
  34. }
  35. drawXAxis(xName, xData){
  36. this.context.beginPath();
  37. this.context.moveTo(this.left, this.bottom);
  38. this.context.lineTo(this.right, this.bottom);
  39. this.context.lineWidth = 2;
  40. this.context.stroke();
  41. this.context.font = "25px Arial";
  42. this.context.fillText(xName, this.right / 2, this.bottom + 50);
  43. this.context.setLineDash([5, 10]);
  44. this.context.font = "10px Arial";
  45. this.context.lineWidth = 1;
  46. if(xData.type = "date"){
  47. 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)));
  48. // let horizontalIncrement = (this.right - this.left) /10
  49. // let horizontalOffset = 0
  50. let showDate = xData.start;
  51. for(let i = 0; i < xData.dataLength; i += Math.floor(xData.dataLength / 10)){
  52. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  53. this.context.beginPath()
  54. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  55. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  56. this.context.strokeStyle = "#a5a5a5";
  57. this.context.stroke();
  58. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  59. console.log(i);
  60. }
  61. // do{
  62. // this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + horizontalOffset - 20, this.bottom + 10);
  63. // this.context.beginPath();
  64. // this.context.moveTo(this.left + horizontalOffset, this.bottom);
  65. // this.context.lineTo(this.left + horizontalOffset, this.top);
  66. // this.context.strokeStyle = "#a5a5a5";
  67. // this.context.stroke();
  68. // showDate.setDate(showDate.getDate() + (Math.abs(diff / 10)));
  69. // horizontalOffset += horizontalIncrement;
  70. // }while(horizontalOffset < (this.right - this.left));
  71. }
  72. this.context.strokeStyle = "black";
  73. this.context.setLineDash([]);
  74. }
  75. drawYAxis(yName, max){
  76. this.context.beginPath();
  77. this.context.moveTo(this.left, this.top);
  78. this.context.lineTo(this.left, this.bottom);
  79. this.context.lineWidth = 2;
  80. this.context.stroke();
  81. this.context.font = "25px Arial";
  82. this.context.fillText(yName, 0, this.bottom / 2);
  83. this.context.setLineDash([5, 10]);
  84. this.context.font = "10px Arial";
  85. this.context.lineWidth = 1;
  86. let axisNum = 0;
  87. let verticalIncrement = (this.bottom - this.top) / 10;
  88. let verticalOffset = 0;
  89. do{
  90. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  91. this.context.beginPath();
  92. this.context.moveTo(this.left, this.bottom - verticalOffset);
  93. this.context.lineTo(this.right, this.bottom - verticalOffset);
  94. this.context.strokeStyle = "#a5a5a5";
  95. this.context.stroke();
  96. verticalOffset += verticalIncrement;
  97. axisNum += max / 10;
  98. }while(verticalOffset <= (this.bottom - this.top));
  99. this.context.strokeStyle = "black";
  100. this.context.setLineDash([]);
  101. }
  102. }