Graph.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {Object2D} from "../Object2D.js";
  2. import {Vector2} from "../math/Vector2.js";
  3. import {Box2} from "../math/Box2.js";
  4. /**
  5. * Graph object is used to draw simple graph data into the canvas.
  6. *
  7. * Graph data is composed of X, Y values.
  8. *
  9. * @class
  10. * @extends {Object2D}
  11. */
  12. function Graph()
  13. {
  14. Object2D.call(this);
  15. /**
  16. * Graph object containing the size of the object.
  17. */
  18. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  19. /**
  20. * Color of the box border line.
  21. */
  22. this.strokeStyle = "rgb(0, 153, 255)";
  23. /**
  24. * Line width.
  25. */
  26. this.lineWidth = 1;
  27. /**
  28. * Background color of the box.
  29. */
  30. this.fillStyle = "rgba(0, 153, 255, 0.3)";
  31. /**
  32. * Minimum value of the graph.
  33. */
  34. this.min = 0;
  35. /**
  36. * Maximum value of the graph.
  37. */
  38. this.max = 10;
  39. /**
  40. * Data to be presented in the graph.
  41. *
  42. * The array should store numeric values.
  43. */
  44. this.data = [];
  45. }
  46. Graph.prototype = Object.create(Object2D.prototype);
  47. Graph.prototype.constructor = Graph;
  48. Graph.prototype.type = "Graph";
  49. Graph.prototype.isInside = function(point)
  50. {
  51. return this.box.containsPoint(point);
  52. };
  53. Graph.prototype.draw = function(context, viewport, canvas)
  54. {
  55. if(this.data.length === 0)
  56. {
  57. return;
  58. }
  59. var width = this.box.max.x - this.box.min.x;
  60. var height = this.box.max.y - this.box.min.y;
  61. context.lineWidth = this.lineWidth;
  62. context.strokeStyle = this.strokeStyle;
  63. context.beginPath();
  64. var step = width / (this.data.length - 1);
  65. var gamma = this.max - this.min;
  66. context.moveTo(this.box.min.x, this.box.max.y - ((this.data[0] - this.min) / gamma) * height);
  67. for(var i = 1, s = step; i < this.data.length; s += step, i++)
  68. {
  69. context.lineTo(this.box.min.x + s, this.box.max.y - ((this.data[i] - this.min) / gamma) * height);
  70. }
  71. context.stroke();
  72. if(this.fillStyle !== null)
  73. {
  74. context.fillStyle = this.fillStyle;
  75. context.lineTo(this.box.max.x, this.box.max.y);
  76. context.lineTo(this.box.min.x, this.box.max.y);
  77. context.fill();
  78. }
  79. };
  80. Graph.prototype.serialize = function(recursive)
  81. {
  82. var data = Object2D.prototype.serialize.call(this, recursive);
  83. data.box = this.box.toArray();
  84. data.strokeStyle = this.strokeStyle;
  85. data.lineWidth = this.lineWidth;
  86. data.fillStyle = this.fillStyle;
  87. data.min = this.min;
  88. data.max = this.max;
  89. data.data = this.data;
  90. return data;
  91. };
  92. Graph.prototype.parse = function(data)
  93. {
  94. Object2D.prototype.parse.call(this, data);
  95. this.box.fromArray(data.box);
  96. this.strokeStyle = data.strokeStyle;
  97. this.lineWidth = data.lineWidth;
  98. this.fillStyle = data.fillStyle;
  99. this.min = data.min;
  100. this.max = data.max;
  101. this.data = data.data;
  102. };
  103. export {Graph};