Graph.js 3.2 KB

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