Graph.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. this.box = new Box2(new Vector2(-50, -35), new Vector2(50, 35));
  20. /**
  21. * Color of the box border line.
  22. */
  23. this.strokeStyle = new ColorStyle("rgb(0, 153, 255)");
  24. /**
  25. * Line width used to stroke the graph data.
  26. */
  27. this.lineWidth = 1.0;
  28. /**
  29. * Background color of the box.
  30. */
  31. this.fillStyle = new ColorStyle("rgba(0, 153, 255, 0.3)");
  32. /**
  33. * Minimum value of the graph.
  34. */
  35. this.min = 0;
  36. /**
  37. * Maximum value of the graph.
  38. */
  39. this.max = 10;
  40. /**
  41. * Data to be presented in the graph.
  42. *
  43. * The array should store numeric values.
  44. */
  45. this.data = [];
  46. }
  47. Graph.prototype = Object.create(Object2D.prototype);
  48. Graph.prototype.constructor = Graph;
  49. Graph.prototype.type = "Graph";
  50. Object2D.register(Graph, "Graph");
  51. Graph.prototype.isInside = function(point)
  52. {
  53. return this.box.containsPoint(point);
  54. };
  55. Graph.prototype.draw = function(context, viewport, canvas)
  56. {
  57. if(this.data.length === 0)
  58. {
  59. return;
  60. }
  61. var width = this.box.max.x - this.box.min.x;
  62. var height = this.box.max.y - this.box.min.y;
  63. context.lineWidth = this.lineWidth;
  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. if(this.strokeStyle !== null)
  73. {
  74. context.strokeStyle = this.strokeStyle.get(context);
  75. context.stroke();
  76. }
  77. if(this.fillStyle !== null)
  78. {
  79. context.fillStyle = this.fillStyle.get(context);
  80. context.lineTo(this.box.max.x, this.box.max.y);
  81. context.lineTo(this.box.min.x, this.box.max.y);
  82. context.fill();
  83. }
  84. };
  85. Graph.prototype.serialize = function(recursive)
  86. {
  87. var data = Object2D.prototype.serialize.call(this, recursive);
  88. data.box = this.box.toArray();
  89. data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
  90. data.lineWidth = this.lineWidth;
  91. data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
  92. data.min = this.min;
  93. data.max = this.max;
  94. data.data = this.data;
  95. return data;
  96. };
  97. Graph.prototype.parse = function(data, root)
  98. {
  99. Object2D.prototype.parse.call(this, data, root);
  100. this.box.fromArray(data.box);
  101. this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
  102. this.lineWidth = data.lineWidth;
  103. this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
  104. this.min = data.min;
  105. this.max = data.max;
  106. this.data = data.data;
  107. };
  108. export {Graph};