vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
11 KiB

10 years ago
  1. /**
  2. * Created by Alex on 10/27/2014.
  3. */
  4. var GETTING_EVENTS = false;
  5. var agentList = {};
  6. var jobList = {};
  7. var eventGen = new EventGenerator("eventGenerator");
  8. var agentGen = new AgentGenerator("agentGenerator");
  9. var jobGen = new JobAgentGenerator("jobAgentGenerator");
  10. // eventGen.start();
  11. function getNewEvent() {
  12. agentGen.getEvents(1);
  13. }
  14. function getTenNewEvents() {
  15. agentGen.getEvents(10);
  16. }
  17. function getAllEvents() {
  18. if (GETTING_EVENTS == false) {
  19. GETTING_EVENTS = true;
  20. agentGen.getEvents(agentGen.amountOfEvents);
  21. }
  22. }
  23. function getAllEventsQuickly() {
  24. INCREASE_SPEED = false;
  25. EVENT_DELAY = 0;
  26. if (GETTING_EVENTS == false) {
  27. GETTING_EVENTS = true;
  28. agentGen.getEvents(agentGen.amountOfEvents);
  29. }
  30. else {
  31. }
  32. }
  33. function refreshJobs() {
  34. var multiSelect = document.getElementById("multiselect");
  35. while (multiSelect.firstChild) {
  36. multiSelect.removeChild(multiSelect.firstChild);
  37. }
  38. var jobNames = jobGen.getAllJobNames();
  39. for (var i = 0; i < jobNames.length; i++) {
  40. var jobOption = new Option(jobNames[i], jobNames[i]);
  41. multiSelect.appendChild(jobOption);
  42. }
  43. }
  44. function updateGraph() {
  45. var multiSelect = document.getElementById("multiselect");
  46. var selection = [];
  47. for (var i = 0; i < multiSelect.children.length; i++) {
  48. if (multiSelect.children[i].selected) {
  49. selection.push(multiSelect.children[i].value);
  50. }
  51. }
  52. var filteredValues = [];
  53. var originalPredictionValues = [];
  54. var durationValues = [];
  55. var predictionValues = [];
  56. var diffValues = [];
  57. var graphGroup = null;
  58. var stdGroup = null;
  59. var predictionGroup = null;
  60. var originalPredictionGroup = null;
  61. for (var i = 0; i < graph2dDataset.length; i++) {
  62. if (selection.indexOf(graph2dDataset[i].type) != -1) {
  63. if (showDuration == true) {
  64. if (graph2dDataset[i].group == graph2dDataset[i].type + "_" + selectedGroup) {
  65. filteredValues.push(graph2dDataset[i]);
  66. durationValues.push(graph2dDataset[i]);
  67. graphGroup = graph2dDataset[i].group;
  68. }
  69. }
  70. if (showPrediction == true) {
  71. if (graph2dDataset[i].group == graph2dDataset[i].type + "_pred_" + selectedGroup) {
  72. filteredValues.push(graph2dDataset[i]);
  73. predictionValues.push(graph2dDataset[i]);
  74. predictionGroup = graph2dDataset[i].group;
  75. }
  76. if (graph2dDataset[i].group == graph2dDataset[i].type + "_pred_" + selectedGroup + "_std_higher") {
  77. filteredValues.push(graph2dDataset[i]);
  78. stdGroup = graph2dDataset[i].group;
  79. }
  80. if (graph2dDataset[i].group == graph2dDataset[i].type + "_pred_" + selectedGroup + "_std_lower") {
  81. filteredValues.push(graph2dDataset[i]);
  82. stdGroup = graph2dDataset[i].group;
  83. }
  84. if (graph2dDataset[i].group == graph2dDataset[i].type + "_pred_" + selectedGroup + "_original") {
  85. filteredValues.push(graph2dDataset[i]);
  86. originalPredictionValues.push(graph2dDataset[i]);
  87. originalPredictionGroup = graph2dDataset[i].group;
  88. }
  89. }
  90. }
  91. }
  92. graph2DItems.clear();
  93. if (differenceWithPrediction == true) {
  94. for (var i = 0; i < durationValues.length; i++) {
  95. var item = {};
  96. item.x = i < 10 ? '2014-09-0' + i : '2014-09-' + i;
  97. item.y = durationValues[i].y;
  98. item.type = durationValues[i].type;
  99. item.y -= originalPredictionValues[i].y;
  100. var group = 'differenceNegative';
  101. if (item.y < 0) {
  102. item.y *= -1;
  103. group = 'differencePositive'
  104. }
  105. item.group = group;
  106. diffValues.push(item);
  107. }
  108. var legendDiv = document.getElementById("Legend");
  109. legendDiv.innerHTML = "";
  110. populateExternalLegend('differencePositive', "Faster than predicted (hours)");
  111. populateExternalLegend('differenceNegative', "Slower than predicted (hours)");
  112. graph2DItems.add(diffValues);
  113. }
  114. else {
  115. var filteredValues = [];
  116. for (var i = 0; i < durationValues.length; i++) {
  117. var durationItem = {};
  118. durationItem.x = i < 10 ? '2014-09-0' + i : '2014-09-' + i;
  119. durationItem.y = durationValues[i].y;
  120. durationItem.type = durationValues[i].type;
  121. durationItem.group = durationValues[i].group;
  122. filteredValues.push(durationItem);
  123. if (showPrediction == true) {
  124. var predItem = {};
  125. predItem.x = i < 10 ? '2014-09-0' + i : '2014-09-' + i;
  126. predItem.y = predictionValues[i].y;
  127. predItem.type = predictionValues[i].type;
  128. predItem.group = predictionValues[i].group;
  129. filteredValues.push(predItem);
  130. var originalPred = {};
  131. originalPred.x = i < 10 ? '2014-09-0' + i : '2014-09-' + i;
  132. originalPred.y = originalPredictionValues[i].y;
  133. originalPred.type = originalPredictionValues[i].type;
  134. originalPred.group = originalPredictionValues[i].group;
  135. filteredValues.push(originalPred);
  136. }
  137. }
  138. var legendDiv = document.getElementById("Legend");
  139. legendDiv.innerHTML = "";
  140. if (graphGroup != null) {
  141. populateExternalLegend(graphGroup, "duration (hours)");
  142. }
  143. // if (stdGroup != null) {populateExternalLegend(stdGroup, "standard deviation");}
  144. if (predictionGroup != null) {
  145. populateExternalLegend(predictionGroup, "updated prediction (hours)");
  146. }
  147. if (originalPredictionGroup != null) {
  148. populateExternalLegend(originalPredictionGroup, "initial prediction (hours)");
  149. }
  150. graph2DItems.add(filteredValues);
  151. }
  152. graph2d.fit();
  153. }
  154. function turnOff(type) {
  155. var btn = document.getElementById(type);
  156. btn.className = btn.className.replace(" selected", "");
  157. }
  158. function turnOffAll() {
  159. var types = ['duration', 'durationWithPause', 'durationWithStartup', 'durationWithBoth'];
  160. for (var i = 0; i < types.length; i++) {
  161. turnOff(types[i]);
  162. }
  163. }
  164. function turnOn(type) {
  165. turnOffAll();
  166. var btn = document.getElementById(type);
  167. selectedGroup = type;
  168. btn.className += " selected";
  169. updateGraph();
  170. }
  171. function togglePrediction() {
  172. if (differenceWithPrediction != true) {
  173. var btn = document.getElementById('togglePrediction');
  174. if (showPrediction == true) {
  175. btn.className = btn.className.replace("selected", "");
  176. showPrediction = false;
  177. }
  178. else {
  179. showPrediction = true;
  180. btn.className += " selected";
  181. }
  182. updateGraph();
  183. }
  184. }
  185. function toggleDuration() {
  186. if (differenceWithPrediction != true) {
  187. var btn = document.getElementById('toggleDuration');
  188. if (showDuration == true) {
  189. btn.className = btn.className.replace("selected", "");
  190. showDuration = false;
  191. }
  192. else {
  193. showDuration = true;
  194. btn.className += " selected";
  195. }
  196. updateGraph();
  197. }
  198. }
  199. function toggleDifference() {
  200. differenceWithPrediction = !differenceWithPrediction;
  201. var btn = document.getElementById('togglePrediction');
  202. if (showPrediction == false && differenceWithPrediction == true) {
  203. showPrediction = true;
  204. btn.className += " selected";
  205. }
  206. btn = document.getElementById('toggleDuration');
  207. if (showDuration == false && differenceWithPrediction == true) {
  208. showDuration = true;
  209. btn.className += " selected";
  210. }
  211. var btn2 = document.getElementById('difference');
  212. if (differenceWithPrediction == false) {
  213. btn2.className = btn2.className.replace("selected", "");
  214. }
  215. else {
  216. btn2.className += " selected";
  217. }
  218. updateGraph();
  219. }
  220. function showTimelineBtn() {
  221. var timelineBtn = document.getElementById('showTimeline');
  222. var graphBtn = document.getElementById('showGraph');
  223. if (showTimeline == false) {
  224. graphBtn.className = graphBtn.className.replace("selected", "");
  225. timelineBtn.className += " selected";
  226. var timelinewrapper = document.getElementById("timelineWrapper");
  227. var graphwrapper = document.getElementById("graphWrapper");
  228. graphwrapper.style.display = "none";
  229. timelinewrapper.style.display = "block";
  230. showTimeline = true;
  231. showGraph = false;
  232. }
  233. }
  234. function showGraphBtn() {
  235. var timelineBtn = document.getElementById('showTimeline');
  236. var graphBtn = document.getElementById('showGraph');
  237. if (showGraph == false) {
  238. timelineBtn.className = graphBtn.className.replace("selected", "");
  239. graphBtn.className += " selected";
  240. var timelinewrapper = document.getElementById("timelineWrapper");
  241. var graphwrapper = document.getElementById("graphWrapper");
  242. timelinewrapper.style.display = "none";
  243. graphwrapper.style.display = "block";
  244. showTimeline = false;
  245. showGraph = true;
  246. }
  247. }
  248. /**
  249. * this function fills the external legend with content using the getLegend() function.
  250. */
  251. function populateExternalLegend(groupDataItem, description) {
  252. var legendDiv = document.getElementById("Legend");
  253. // create divs
  254. var containerDiv = document.createElement("div");
  255. var iconDiv = document.createElement("div");
  256. var descriptionDiv = document.createElement("div");
  257. // give divs classes and Ids where necessary
  258. containerDiv.className = 'legendElementContainer';
  259. containerDiv.id = groupDataItem + "_legendContainer";
  260. iconDiv.className = "iconContainer";
  261. descriptionDiv.className = "descriptionContainer";
  262. // get the legend for this group.
  263. var legend = graph2d.getLegend(groupDataItem, 30, 30);
  264. // append class to icon. All styling classes from the vis.css have been copied over into the head here to be able to style the
  265. // icons with the same classes if they are using the default ones.
  266. legend.icon.setAttributeNS(null, "class", "legendIcon");
  267. // append the legend to the corresponding divs
  268. iconDiv.appendChild(legend.icon);
  269. descriptionDiv.innerHTML = description;
  270. // determine the order for left and right orientation
  271. if (legend.orientation == 'left') {
  272. descriptionDiv.style.textAlign = "left";
  273. containerDiv.appendChild(iconDiv);
  274. containerDiv.appendChild(descriptionDiv);
  275. }
  276. else {
  277. descriptionDiv.style.textAlign = "right";
  278. containerDiv.appendChild(descriptionDiv);
  279. containerDiv.appendChild(iconDiv);
  280. }
  281. // append to the legend container div
  282. legendDiv.appendChild(containerDiv);
  283. }
  284. function printEvents(events) {
  285. var str = "";
  286. str += "[";
  287. for (var i = 0; i < events.length; i++) {
  288. str += "{";
  289. var first = true;
  290. for (var eventField in events[i]) {
  291. if (events[i].hasOwnProperty(eventField)) {
  292. if (first == false) {
  293. str += ","
  294. }
  295. first = false;
  296. if (eventField == "time") {
  297. str += eventField + ": '" + new Date(events[i][eventField]).valueOf() + "'";
  298. }
  299. else if(events[i][eventField] instanceof Array) {
  300. str += eventField + ": [";
  301. for (var j = 0; j < events[i][eventField].length; j++) {
  302. str += "'" + events[i][eventField][j] + "'";
  303. if (j != events[i][eventField].length - 1) {
  304. str += ",";
  305. }
  306. }
  307. }
  308. else {
  309. str += eventField + ": '" + events[i][eventField] + "'";
  310. }
  311. }
  312. }
  313. str += "}";
  314. if (i < events.length -1) {
  315. str += ",";
  316. }
  317. }
  318. str += "]";
  319. console.log(str);
  320. }