StatisticGraph.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.geom.GeneralPath;
  11. import java.util.ArrayList;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15. import classes.HolonElement;
  16. import classes.HolonObject;
  17. import classes.HolonSwitch;
  18. import classes.TrackedDataSet;
  19. import ui.controller.Control;
  20. import ui.model.Model;
  21. public class StatisticGraph extends JPanel {
  22. /**
  23. *
  24. */
  25. private static final long serialVersionUID = 1L;
  26. // Maximum y Value
  27. double maximum = 0;
  28. // is the Simulation running?
  29. private boolean isSimRunning;
  30. // model and controller
  31. private Model model;
  32. private Control controller;
  33. // Graphics2D
  34. private Graphics2D g2;
  35. GeneralPath path = new GeneralPath();
  36. // Data
  37. public ArrayList<TrackedDataSet> objects = new ArrayList<>();
  38. /**
  39. * Constructor.
  40. *
  41. * @param model
  42. * the Model
  43. * @param control
  44. * the Controller
  45. */
  46. public StatisticGraph(final Model model, Control control) {
  47. this.controller = control;
  48. this.model = model;
  49. this.setBackground(Color.WHITE);
  50. }
  51. /**
  52. * Paints all Components on the Canvas.
  53. *
  54. * @param g
  55. * Graphics
  56. *
  57. */
  58. public void paintComponent(Graphics g) {
  59. super.paintComponent(g);
  60. // Graphics2D init
  61. g2 = (Graphics2D) g;
  62. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  63. g2.setRenderingHints(rh);
  64. // Paint the Grid
  65. g2.setStroke(new BasicStroke(0));
  66. g2.setColor(Color.BLACK);
  67. for (int i = 0; i <= this.getWidth(); i += 10) {
  68. g2.drawLine(i, 0, i, this.getHeight());
  69. }
  70. for (int i = 0; i <= this.getHeight(); i += 5) {
  71. g2.drawLine(0, i, this.getWidth(), i);
  72. }
  73. isSimRunning = model.getIsSimulation();
  74. // if sim is on
  75. if (isSimRunning) {
  76. g2.setStroke(new BasicStroke(3));
  77. // Calculate the Maximum
  78. calcMaximum();
  79. // Calculate values for each set and add them
  80. addValues();
  81. // Create Paths and draw them
  82. for (TrackedDataSet set : objects) {
  83. path.reset();
  84. switch (set.getProperty()) {
  85. case TrackedDataSet.CONSUMPTION:
  86. case TrackedDataSet.PRODUCTION:
  87. case TrackedDataSet.ACTIVATED_ELEMENTS:
  88. case TrackedDataSet.TOTAL_PRODUCTION:
  89. case TrackedDataSet.TOTAL_CONSUMPTION:
  90. createPathFloats(set);
  91. break;
  92. case TrackedDataSet.ON_OFF:
  93. createPathBooleans(set);
  94. break;
  95. case TrackedDataSet.PERCENT_SUPPLIED:
  96. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  97. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  98. //TODO Percentage Method
  99. break;
  100. default:
  101. break;
  102. }
  103. g2.setColor(set.getColor());
  104. g2.draw(path);
  105. }
  106. }
  107. }
  108. /**
  109. * Add an Object to the Graph if the maximum has not reached yet.
  110. *
  111. * @param obj
  112. * the Object to add
  113. */
  114. public void addObject(TrackedDataSet set) {
  115. objects.add(set);
  116. }
  117. /**
  118. * Removes an Object from the Graph.
  119. *
  120. * @param id
  121. * the id of the Object to remove
  122. */
  123. public void removeObject(int id) {
  124. objects.remove(id);
  125. }
  126. /**
  127. * converts the number to fit the canvas.
  128. *
  129. * @param d
  130. * the number to convert
  131. * @return the converted number
  132. */
  133. public double convertToCanvasY(float d) {
  134. return Math.abs((this.getHeight() - (d * (this.getHeight() / maximum))));
  135. }
  136. /**
  137. * Does take into account which timestep is watched, calculates the max
  138. * values.
  139. *
  140. * @return the currentEnergy
  141. */
  142. public float getEnergyAtCurrentTimeStep(HolonObject obj) {
  143. float temp = 0;
  144. for (HolonElement e : obj.getElements()) {
  145. if (e.getActive()) {
  146. temp = temp + e.getEnergyAt()[model.getCurIteration()];
  147. }
  148. }
  149. return temp;
  150. }
  151. /**
  152. * Calculate the Max Value of the Graph
  153. */
  154. public void calcMaximum() {
  155. maximum = 0;
  156. for (TrackedDataSet set : objects) {
  157. int val = 0;
  158. switch (set.getProperty()) {
  159. case TrackedDataSet.CONSUMPTION:
  160. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  161. if (h.getEnergy() < 0) {
  162. val += h.getEnergy() * h.getAmount();
  163. }
  164. }
  165. val *= -1;
  166. break;
  167. case TrackedDataSet.PRODUCTION:
  168. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  169. if (h.getEnergy() > 0) {
  170. val += h.getEnergy() * h.getAmount();
  171. }
  172. }
  173. break;
  174. case TrackedDataSet.ACTIVATED_ELEMENTS:
  175. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  176. val += h.getAmount();
  177. }
  178. break;
  179. case TrackedDataSet.ON_OFF:
  180. val = 1;
  181. break;
  182. case TrackedDataSet.TOTAL_PRODUCTION:
  183. //TODO
  184. break;
  185. case TrackedDataSet.TOTAL_CONSUMPTION:
  186. //TODO
  187. break;
  188. case TrackedDataSet.PERCENT_SUPPLIED:
  189. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  190. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  191. val = 1;
  192. break;
  193. default:
  194. break;
  195. }
  196. if (val > maximum) {
  197. maximum = val;
  198. }
  199. }
  200. ((StatisticGraphPanel) this.getParent()).setMaximumLabel(maximum);
  201. }
  202. /**
  203. * Add the Current Values to each set
  204. */
  205. private void addValues() {
  206. for (TrackedDataSet set : objects) {
  207. int val = 0;
  208. switch (set.getProperty()) {
  209. case TrackedDataSet.CONSUMPTION:
  210. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  211. if (h.getEnergy() < 0 && h.getActive()) {
  212. val += Math.abs(h.getEnergyAt()[model.getCurIteration()]) * h.getAmount();
  213. }
  214. set.setValAt(val, model.getCurIteration());
  215. }
  216. break;
  217. case TrackedDataSet.PRODUCTION:
  218. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  219. if (h.getEnergy() > 0 && h.getActive()) {
  220. val += Math.abs(h.getEnergyAt()[model.getCurIteration()]) * h.getAmount();
  221. }
  222. set.setValAt(val, model.getCurIteration());
  223. }
  224. break;
  225. case TrackedDataSet.ACTIVATED_ELEMENTS:
  226. for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
  227. if (h.getActive()) {
  228. val += h.getAmount();
  229. }
  230. set.setValAt(val, model.getCurIteration());
  231. }
  232. break;
  233. case TrackedDataSet.ON_OFF:
  234. if (((HolonSwitch) set.getCpsObject()).getManualMode()) {
  235. if (((HolonSwitch) set.getCpsObject()).getActiveManual()) {
  236. set.setValAt(1, model.getCurIteration());
  237. } else {
  238. set.setValAt(0, model.getCurIteration());
  239. }
  240. } else {
  241. if (((HolonSwitch) set.getCpsObject()).getActiveAt()[model.getCurIteration()]) {
  242. set.setValAt(1, model.getCurIteration());
  243. } else {
  244. set.setValAt(0, model.getCurIteration());
  245. }
  246. }
  247. break;
  248. case TrackedDataSet.TOTAL_PRODUCTION:
  249. break;
  250. case TrackedDataSet.TOTAL_CONSUMPTION:
  251. break;
  252. case TrackedDataSet.PERCENT_SUPPLIED:
  253. break;
  254. case TrackedDataSet.PERCENT_NOT_SUPPLIED:
  255. break;
  256. case TrackedDataSet.PERCENT_PARTIAL_SUPPLIED:
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. }
  263. /**
  264. * create Path with floats
  265. *
  266. * @param set
  267. */
  268. private void createPathFloats(TrackedDataSet set) {
  269. boolean init = true;
  270. path.moveTo(0, 0);
  271. for (int i = 0; i < model.getCurIteration(); i++) {
  272. if (init && set.getValues()[i] != -1) {
  273. path.moveTo(i * this.getWidth() / model.getIterations() - 1, convertToCanvasY(set.getValues()[i]));
  274. init = false;
  275. }
  276. if (!init) {
  277. if (set.getValues()[i + 1] != -1) {
  278. path.lineTo((i + 1) * this.getWidth() / model.getIterations(),
  279. convertToCanvasY(set.getValues()[i + 1]));
  280. } else {
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * create Path with booleans(0 and 1)
  288. *
  289. * @param set
  290. */
  291. private void createPathBooleans(TrackedDataSet set) {
  292. boolean init = true;
  293. for (int i = 0; i < model.getCurIteration(); i++) {
  294. if (init && set.getValues()[i] != -1) {
  295. path.moveTo(i * this.getWidth() / model.getIterations() - 1,
  296. convertToCanvasY((float) (set.getValues()[i] * maximum)));
  297. init = false;
  298. }
  299. if (!init) {
  300. if (set.getValues()[i + 1] != -1) {
  301. path.lineTo((i + 1) * this.getWidth() / model.getIterations(),
  302. convertToCanvasY((float) (set.getValues()[i + 1] * maximum)));
  303. } else {
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. }