graphs.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. for(let i = 0; i < data.length - 1; i++){
  26. context.beginPath();
  27. context.moveTo(left + (horizontalMultiplier * i), bottom - (verticalMultiplier * data[i]));
  28. context.lineTo(left + (horizontalMultiplier * (i + 1)), bottom - (verticalMultiplier * data[i + 1]));
  29. context.stroke();
  30. }
  31. }
  32. }