plot.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { mix, isObject } from '../util/common';
  2. class Plot {
  3. constructor(cfg) {
  4. mix(this, cfg);
  5. this._init();
  6. }
  7. _init() {
  8. var self = this;
  9. var start = self.start;
  10. var end = self.end;
  11. var xMin = Math.min(start.x, end.x);
  12. var xMax = Math.max(start.x, end.x);
  13. var yMin = Math.min(start.y, end.y);
  14. var yMax = Math.max(start.y, end.y);
  15. this.tl = {
  16. x: xMin,
  17. y: yMin
  18. };
  19. this.tr = {
  20. x: xMax,
  21. y: yMin
  22. };
  23. this.bl = {
  24. x: xMin,
  25. y: yMax
  26. };
  27. this.br = {
  28. x: xMax,
  29. y: yMax
  30. };
  31. this.width = xMax - xMin;
  32. this.height = yMax - yMin;
  33. }
  34. /**
  35. * reset
  36. * @param {Object} start start point
  37. * @param {Object} end end point
  38. */
  39. reset(start, end) {
  40. this.start = start;
  41. this.end = end;
  42. this._init();
  43. }
  44. /**
  45. * check the point is in the range of plot
  46. * @param {Number} x x value
  47. * @param {[type]} y y value
  48. * @return {Boolean} return the result
  49. */
  50. isInRange(x, y) {
  51. if (isObject(x)) {
  52. y = x.y;
  53. x = x.x;
  54. }
  55. var tl = this.tl;
  56. var br = this.br;
  57. return tl.x <= x && x <= br.x && tl.y <= y && y <= br.y;
  58. }
  59. }
  60. export default Plot;