ScatterGraph.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {Object2D} from "../../Object2D.js";
  2. import {Graph} from "./Graph.js";
  3. /**
  4. * Scatter graph can be used to draw numeric data as points.
  5. *
  6. * @class
  7. * @extends {Object2D}
  8. */
  9. function ScatterGraph()
  10. {
  11. Graph.call(this);
  12. /**
  13. * Radius of each point represented in the scatter plot.
  14. *
  15. * @type {number}
  16. */
  17. this.radius = 5.0;
  18. /**
  19. * Draw lines betwen the points of the scatter graph.
  20. *
  21. * @type {boolean}
  22. */
  23. this.drawLine = false;
  24. }
  25. ScatterGraph.prototype = Object.create(Graph.prototype);
  26. ScatterGraph.prototype.constructor = ScatterGraph;
  27. ScatterGraph.prototype.type = "BarGraph";
  28. Object2D.register(ScatterGraph, "BarGraph");
  29. ScatterGraph.prototype.draw = function(context, viewport, canvas)
  30. {
  31. if(this.data.length === 0)
  32. {
  33. return;
  34. }
  35. var width = this.box.max.x - this.box.min.x;
  36. var height = this.box.max.y - this.box.min.y;
  37. var step = width / (this.data.length - 1);
  38. var gamma = this.max - this.min;
  39. context.lineWidth = this.lineWidth;
  40. // Draw line
  41. if(this.drawLine)
  42. {
  43. context.beginPath();
  44. context.moveTo(this.box.min.x, this.box.max.y - ((this.data[0] - this.min) / gamma) * height);
  45. for(var i = 1, s = step; i < this.data.length; s += step, i++)
  46. {
  47. context.lineTo(this.box.min.x + s, this.box.max.y - ((this.data[i] - this.min) / gamma) * height);
  48. }
  49. if(this.strokeStyle !== null)
  50. {
  51. context.strokeStyle = this.strokeStyle.get(context);
  52. context.stroke();
  53. }
  54. }
  55. // Draw circles
  56. context.beginPath();
  57. for(var i = 0, s = 0; i < this.data.length; s += step, i++)
  58. {
  59. var y = this.box.max.y - ((this.data[i] - this.min) / gamma) * height;
  60. context.moveTo(this.box.min.x + s + this.radius, y);
  61. context.arc(this.box.min.x + s, y, this.radius, 0, Math.PI * 2, true);
  62. }
  63. if(this.strokeStyle !== null)
  64. {
  65. context.strokeStyle = this.strokeStyle.get(context);
  66. context.stroke();
  67. }
  68. if(this.fillStyle !== null)
  69. {
  70. context.fillStyle = this.fillStyle.get(context);
  71. context.fill();
  72. }
  73. };
  74. ScatterGraph.prototype.serialize = function(recursive)
  75. {
  76. var data = Graph.prototype.serialize.call(this, recursive);
  77. data.radius = this.radius;
  78. return data;
  79. };
  80. ScatterGraph.prototype.parse = function(data, root)
  81. {
  82. Graph.prototype.parse.call(this, data, root);
  83. this.radius = data.radius;
  84. };
  85. export {ScatterGraph};