graphs.js 5.5 KB

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