graphs.js 5.2 KB

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