graphs.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. let graph = {
  2. line: function(canvas, data){
  3. canvas.height = canvas.clientHeight;
  4. canvas.width = canvas.clientWidth;
  5. let context = canvas.getContext("2d");
  6. let left = canvas.clientWidth - (canvas.clientWidth * 0.75);
  7. let right = canvas.clientWidth * 0.75;
  8. let bottom = canvas.clientHeight * 0.9;
  9. let top = canvas.clientHeight - (canvas.clientHeight * 0.9);
  10. let max = data[0];
  11. for(let point of data){
  12. if(point > max){max = point;}
  13. }
  14. let verticalMultiplier = (bottom - top) / max;
  15. let horizontalMultiplier = (right - left) / data.length;
  16. //Draw axes
  17. context.beginPath();
  18. context.moveTo(left, top);
  19. context.lineTo(left, bottom);
  20. context.stroke();
  21. context.beginPath();
  22. context.moveTo(left, bottom);
  23. context.lineTo(right, bottom);
  24. context.stroke();
  25. context.beginPath();
  26. context.moveTo(right, bottom);
  27. context.lineTo(right, top);
  28. context.stroke();
  29. context.beginPath();
  30. context.moveTo(left, top);
  31. context.lineTo(right, top);
  32. context.stroke();
  33. for(let i = 0; i < data.length - 1; i++){
  34. context.beginPath();
  35. context.moveTo(left + (horizontalMultiplier * i), bottom - (verticalMultiplier * data[i]));
  36. context.lineTo(left + (horizontalMultiplier * (i + 1)), bottom - (verticalMultiplier * data[i + 1]));
  37. context.stroke();
  38. }
  39. }
  40. }