PieChart.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {Object2D} from "../../Object2D.js";
  2. import { ColorStyle } from "../style/ColorStyle.js";
  3. /**
  4. * Pie chart represents a set of data in a pie like chart graph.
  5. *
  6. * The values are drawn in porportion relative to their sum.
  7. *
  8. * @class
  9. * @extends {Object2D}
  10. */
  11. function PieChart()
  12. {
  13. Object2D.call(this);
  14. /**
  15. * Data to be displayed on the pie chart. Each element should store a value and a stroke/fill styles.
  16. *
  17. * Each element should use the following structure {value: 0.0, fillStyle: ..., strokestyle: ...}.
  18. */
  19. this.data = [
  20. {value: 10, fillStyle: new ColorStyle("#FD5748"), strokeStyle: new ColorStyle("#AAAAAA")},
  21. {value: 15, fillStyle: new ColorStyle("#23AB48"), strokeStyle: new ColorStyle("#AAAAAA")},
  22. {value: 5, fillStyle: new ColorStyle("#6285F8"), strokeStyle: new ColorStyle("#AAAAAA")}
  23. ];
  24. /**
  25. * Radius of the pie chart object.
  26. *
  27. * @type {number}
  28. */
  29. this.radius = 50;
  30. /**
  31. * The line width of each pie chart section.
  32. *
  33. * @type {number}
  34. */
  35. this.lineWidth = 1.0;
  36. /**
  37. * Start angle of the pie chart.
  38. *
  39. * @type {number}
  40. */
  41. this.startAngle = 0;
  42. /**
  43. * End angle of the pie chart.
  44. *
  45. * @type {number}
  46. */
  47. this.endAngle = 2 * Math.PI;
  48. }
  49. PieChart.prototype = Object.create(Object2D.prototype);
  50. PieChart.prototype.constructor = PieChart;
  51. PieChart.prototype.type = "PieChart";
  52. Object2D.register(PieChart, "PieChart");
  53. PieChart.prototype.isInside = function(point)
  54. {
  55. return point.length() <= this.radius;
  56. };
  57. PieChart.prototype.draw = function(context)
  58. {
  59. var sum = 0;
  60. for(var i = 0; i < this.data.length; i++)
  61. {
  62. sum += this.data[i].value;
  63. }
  64. context.lineWidth = this.lineWidth;
  65. var angleRange = this.endAngle - this.startAngle;
  66. var angle = this.startAngle;
  67. for(var i = 0; i < this.data.length; i++)
  68. {
  69. var section = angleRange * (this.data[i].value / sum);
  70. context.beginPath();
  71. context.arc(0, 0, this.radius, angle, section);
  72. angle += section;
  73. if(this.data[i].strokeStyle)
  74. {
  75. context.strokeStyle = this.data[i].strokeStyle.get(context);
  76. context.stroke();
  77. }
  78. if(this.data[i].fillStyle)
  79. {
  80. context.fillStyle = this.data[i].fillStyle.get(context);
  81. context.fill();
  82. }
  83. }
  84. };
  85. PieChart.prototype.serialize = function(recursive)
  86. {
  87. var data = Object2D.prototype.serialize.call(this, recursive);
  88. data.radius = this.radius;
  89. data.lineWidth = this.lineWidth;
  90. data.startAngle = this.startAngle;
  91. data.endAngle = this.endAngle;
  92. return data;
  93. };
  94. PieChart.prototype.parse = function(data, root)
  95. {
  96. Object2D.prototype.parse.call(this, data, root);
  97. this.radius = data.radius;
  98. this.lineWidth = data.lineWidth;
  99. this.startAngle = data.startAngle;
  100. this.endAngle = data.endAngle;
  101. };
  102. export {PieChart};