Graph.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = new ColorStyle("rgb(0, 153, 255)");
  23. /**
  24. * Line width.
  25. */
  26. this.lineWidth = 1;
  27. /**
  28. * Background color of the box.
  29. */
  30. this.fillStyle = new ColorStyle("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. Object2D.register(Graph, "Graph");
  50. Graph.prototype.isInside = function(point)
  51. {
  52. return this.box.containsPoint(point);
  53. };
  54. Graph.prototype.draw = function(context, viewport, canvas)
  55. {
  56. if(this.data.length === 0)
  57. {
  58. return;
  59. }
  60. var width = this.box.max.x - this.box.min.x;
  61. var height = this.box.max.y - this.box.min.y;
  62. context.lineWidth = this.lineWidth;
  63. context.strokeStyle = this.strokeStyle.get(context);
  64. context.beginPath();
  65. var step = width / (this.data.length - 1);
  66. var gamma = this.max - this.min;
  67. context.moveTo(this.box.min.x, this.box.max.y - ((this.data[0] - this.min) / gamma) * height);
  68. for(var i = 1, s = step; i < this.data.length; s += step, i++)
  69. {
  70. context.lineTo(this.box.min.x + s, this.box.max.y - ((this.data[i] - this.min) / gamma) * height);
  71. }
  72. context.stroke();
  73. if(this.fillStyle !== null)
  74. {
  75. context.fillStyle = this.fillStyle.get(context);
  76. context.lineTo(this.box.max.x, this.box.max.y);
  77. context.lineTo(this.box.min.x, this.box.max.y);
  78. context.fill();
  79. }
  80. };
  81. Graph.prototype.serialize = function(recursive)
  82. {
  83. var data = Object2D.prototype.serialize.call(this, recursive);
  84. data.box = this.box.toArray();
  85. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  86. data.lineWidth = this.lineWidth;
  87. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  88. data.min = this.min;
  89. data.max = this.max;
  90. data.data = this.data;
  91. return data;
  92. };
  93. Graph.prototype.parse = function(data, root)
  94. {
  95. Object2D.prototype.parse.call(this, data, root);
  96. this.box.fromArray(data.box);
  97. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  98. this.lineWidth = data.lineWidth;
  99. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  100. this.min = data.min;
  101. this.max = data.max;
  102. this.data = data.data;
  103. };
  104. export {Graph};