graphs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. class LineGraph{
  2. constructor(canvas, yName, xName, xData){
  3. canvas.height = canvas.clientHeight;
  4. canvas.width = canvas.clientWidth;
  5. this.canvas = canvas;
  6. this.context = canvas.getContext("2d");
  7. this.left = canvas.clientWidth - (canvas.clientWidth * 0.9);
  8. this.right = canvas.clientWidth * 0.9;
  9. this.top = canvas.clientHeight - (canvas.clientHeight * 0.9);
  10. this.bottom = canvas.clientHeight * 0.9;
  11. this.data = [];
  12. this.max = 0;
  13. this.xName = xName;
  14. this.yName = yName;
  15. this.xData = xData;
  16. }
  17. drawGraph(){
  18. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  19. this.drawYAxis();
  20. this.drawXAxis();
  21. for(let dataSet of this.data){
  22. this.drawData(dataSet.set);
  23. }
  24. }
  25. addData(data){
  26. this.data.push(data);
  27. let isNewMax = false;
  28. for(let point of data.set){
  29. if(point > this.max){
  30. this.max = point;
  31. isNewMax = true;
  32. }
  33. }
  34. if(isNewMax){
  35. this.verticalMultiplier = (this.bottom - this.top) / this.max;
  36. this.horizontalMultiplier = (this.right - this.left) / data.set.length;
  37. this.xData.dataLength = data.set.length;
  38. this.drawGraph();
  39. }else{
  40. this.drawData(data.set);
  41. }
  42. }
  43. removeData(id){
  44. for(let i = 0; i < this.data.length; i++){
  45. if(this.data[i].id === id){
  46. this.data.splice(i, 1);
  47. break;
  48. }
  49. }
  50. this.drawGraph();
  51. }
  52. drawData(dataSet){
  53. let redRand = Math.floor(Math.random() * 200);
  54. let greenRand = Math.floor(Math.random() * 200);
  55. let blueRand = Math.floor(Math.random() * 200);
  56. for(let i = 0; i < dataSet.length - 1; i++){
  57. this.context.beginPath();
  58. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom - (this.verticalMultiplier * dataSet[i]));
  59. this.context.lineTo(this.left + (this.horizontalMultiplier * (i + 1)), this.bottom - (this.verticalMultiplier * dataSet[i + 1]));
  60. this.context.lineWidth = 1;
  61. this.context.strokeStyle = `rgb(${redRand}, ${greenRand}, ${blueRand})`;
  62. this.context.stroke();
  63. }
  64. }
  65. drawXAxis(){
  66. this.context.beginPath();
  67. this.context.moveTo(this.left, this.bottom);
  68. this.context.lineTo(this.right, this.bottom);
  69. this.context.lineWidth = 2;
  70. this.context.stroke();
  71. this.context.font = "25px Arial";
  72. this.context.fillText(this.xName, this.right / 2, this.bottom + 50);
  73. this.context.setLineDash([5, 10]);
  74. this.context.font = "10px Arial";
  75. this.context.lineWidth = 1;
  76. if(this.xData.type = "date"){
  77. let diff = Math.abs(Math.floor((Date.UTC(this.xData.start.getFullYear(), this.xData.start.getMonth(), this.xData.start.getDate()) - Date.UTC(this.xData.end.getFullYear(), this.xData.end.getMonth(), this.xData.end.getDate())) / (1000 * 60 * 60 * 24)));
  78. let showDate = this.xData.start;
  79. for(let i = 0; i < this.xData.dataLength; i += Math.floor(this.xData.dataLength / 10)){
  80. this.context.fillText(showDate.toLocaleDateString("en-US", {month: "short", day: "numeric", year: "2-digit"}), this.left + (this.horizontalMultiplier * i) - 20, this.bottom + 15);
  81. this.context.beginPath()
  82. this.context.moveTo(this.left + (this.horizontalMultiplier * i), this.bottom);
  83. this.context.lineTo(this.left + (this.horizontalMultiplier * i), this.top);
  84. this.context.strokeStyle = "#a5a5a5";
  85. this.context.stroke();
  86. showDate.setDate(showDate.getDate() + Math.abs(diff / 10));
  87. }
  88. }
  89. this.context.strokeStyle = "black";
  90. this.context.setLineDash([]);
  91. }
  92. drawYAxis(){
  93. this.context.beginPath();
  94. this.context.moveTo(this.left, this.top);
  95. this.context.lineTo(this.left, this.bottom);
  96. this.context.lineWidth = 2;
  97. this.context.stroke();
  98. this.context.font = "25px Arial";
  99. this.context.fillText(this.yName, 0, this.bottom / 2);
  100. this.context.setLineDash([5, 10]);
  101. this.context.font = "10px Arial";
  102. this.context.lineWidth = 1;
  103. let axisNum = 0;
  104. let verticalIncrement = (this.bottom - this.top) / 10;
  105. let verticalOffset = 0;
  106. do{
  107. this.context.fillText(Math.round(axisNum).toString(), this.left - 20, this.bottom - verticalOffset + 3);
  108. this.context.beginPath();
  109. this.context.moveTo(this.left, this.bottom - verticalOffset);
  110. this.context.lineTo(this.right, this.bottom - verticalOffset);
  111. this.context.strokeStyle = "#a5a5a5";
  112. this.context.stroke();
  113. verticalOffset += verticalIncrement;
  114. axisNum += this.max / 10;
  115. }while(verticalOffset <= (this.bottom - this.top));
  116. this.context.strokeStyle = "black";
  117. this.context.setLineDash([]);
  118. }
  119. }