graphs.js 4.4 KB

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