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.

297 lines
8.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var Emitter = require('emitter-component');
  2. var Hammer = require('../module/hammer');
  3. var util = require('../util');
  4. var DataSet = require('../DataSet');
  5. var DataView = require('../DataView');
  6. var Range = require('./Range');
  7. var Core = require('./Core');
  8. var TimeAxis = require('./component/TimeAxis');
  9. var CurrentTime = require('./component/CurrentTime');
  10. var CustomTime = require('./component/CustomTime');
  11. var LineGraph = require('./component/LineGraph');
  12. /**
  13. * Create a timeline visualization
  14. * @param {HTMLElement} container
  15. * @param {vis.DataSet | Array} [items]
  16. * @param {Object} [options] See Graph2d.setOptions for the available options.
  17. * @constructor
  18. * @extends Core
  19. */
  20. function Graph2d (container, items, groups, options) {
  21. // if the third element is options, the forth is groups (optionally);
  22. if (!(Array.isArray(groups) || groups instanceof DataSet) && groups instanceof Object) {
  23. var forthArgument = options;
  24. options = groups;
  25. groups = forthArgument;
  26. }
  27. var me = this;
  28. this.defaultOptions = {
  29. start: null,
  30. end: null,
  31. autoResize: true,
  32. orientation: 'bottom',
  33. width: null,
  34. height: null,
  35. maxHeight: null,
  36. minHeight: null
  37. };
  38. this.options = util.deepExtend({}, this.defaultOptions);
  39. // Create the DOM, props, and emitter
  40. this._create(container);
  41. // all components listed here will be repainted automatically
  42. this.components = [];
  43. this.body = {
  44. dom: this.dom,
  45. domProps: this.props,
  46. emitter: {
  47. on: this.on.bind(this),
  48. off: this.off.bind(this),
  49. emit: this.emit.bind(this)
  50. },
  51. hiddenDates: [],
  52. util: {
  53. toScreen: me._toScreen.bind(me),
  54. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  55. toTime: me._toTime.bind(me),
  56. toGlobalTime : me._toGlobalTime.bind(me)
  57. }
  58. };
  59. // range
  60. this.range = new Range(this.body);
  61. this.components.push(this.range);
  62. this.body.range = this.range;
  63. // time axis
  64. this.timeAxis = new TimeAxis(this.body);
  65. this.components.push(this.timeAxis);
  66. //this.body.util.snap = this.timeAxis.snap.bind(this.timeAxis);
  67. // current time bar
  68. this.currentTime = new CurrentTime(this.body);
  69. this.components.push(this.currentTime);
  70. // item set
  71. this.linegraph = new LineGraph(this.body);
  72. this.components.push(this.linegraph);
  73. this.itemsData = null; // DataSet
  74. this.groupsData = null; // DataSet
  75. this.on('tap', function (event) {
  76. me.emit('click', me.getEventProperties(event))
  77. });
  78. this.on('doubletap', function (event) {
  79. me.emit('doubleClick', me.getEventProperties(event))
  80. });
  81. this.dom.root.oncontextmenu = function (event) {
  82. me.emit('contextmenu', me.getEventProperties(event))
  83. };
  84. // apply options
  85. if (options) {
  86. this.setOptions(options);
  87. }
  88. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  89. if (groups) {
  90. this.setGroups(groups);
  91. }
  92. // create itemset
  93. if (items) {
  94. this.setItems(items);
  95. }
  96. else {
  97. this._redraw();
  98. }
  99. }
  100. // Extend the functionality from Core
  101. Graph2d.prototype = new Core();
  102. /**
  103. * Set items
  104. * @param {vis.DataSet | Array | null} items
  105. */
  106. Graph2d.prototype.setItems = function(items) {
  107. var initialLoad = (this.itemsData == null);
  108. // convert to type DataSet when needed
  109. var newDataSet;
  110. if (!items) {
  111. newDataSet = null;
  112. }
  113. else if (items instanceof DataSet || items instanceof DataView) {
  114. newDataSet = items;
  115. }
  116. else {
  117. // turn an array into a dataset
  118. newDataSet = new DataSet(items, {
  119. type: {
  120. start: 'Date',
  121. end: 'Date'
  122. }
  123. });
  124. }
  125. // set items
  126. this.itemsData = newDataSet;
  127. this.linegraph && this.linegraph.setItems(newDataSet);
  128. if (initialLoad) {
  129. if (this.options.start != undefined || this.options.end != undefined) {
  130. var start = this.options.start != undefined ? this.options.start : null;
  131. var end = this.options.end != undefined ? this.options.end : null;
  132. this.setWindow(start, end, {animation: false});
  133. }
  134. else {
  135. this.fit({animation: false});
  136. }
  137. }
  138. };
  139. /**
  140. * Set groups
  141. * @param {vis.DataSet | Array} groups
  142. */
  143. Graph2d.prototype.setGroups = function(groups) {
  144. // convert to type DataSet when needed
  145. var newDataSet;
  146. if (!groups) {
  147. newDataSet = null;
  148. }
  149. else if (groups instanceof DataSet || groups instanceof DataView) {
  150. newDataSet = groups;
  151. }
  152. else {
  153. // turn an array into a dataset
  154. newDataSet = new DataSet(groups);
  155. }
  156. this.groupsData = newDataSet;
  157. this.linegraph.setGroups(newDataSet);
  158. };
  159. /**
  160. * Returns an object containing an SVG element with the icon of the group (size determined by iconWidth and iconHeight), the label of the group (content) and the yAxisOrientation of the group (left or right).
  161. * @param groupId
  162. * @param width
  163. * @param height
  164. */
  165. Graph2d.prototype.getLegend = function(groupId, width, height) {
  166. if (width === undefined) {width = 15;}
  167. if (height === undefined) {height = 15;}
  168. if (this.linegraph.groups[groupId] !== undefined) {
  169. return this.linegraph.groups[groupId].getLegend(width,height);
  170. }
  171. else {
  172. return "cannot find group:" + groupId;
  173. }
  174. };
  175. /**
  176. * This checks if the visible option of the supplied group (by ID) is true or false.
  177. * @param groupId
  178. * @returns {*}
  179. */
  180. Graph2d.prototype.isGroupVisible = function(groupId) {
  181. if (this.linegraph.groups[groupId] !== undefined) {
  182. return (this.linegraph.groups[groupId].visible && (this.linegraph.options.groups.visibility[groupId] === undefined || this.linegraph.options.groups.visibility[groupId] == true));
  183. }
  184. else {
  185. return false;
  186. }
  187. };
  188. /**
  189. * Get the data range of the item set.
  190. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  191. * When no minimum is found, min==null
  192. * When no maximum is found, max==null
  193. */
  194. Graph2d.prototype.getItemRange = function() {
  195. var min = null;
  196. var max = null;
  197. // calculate min from start filed
  198. for (var groupId in this.linegraph.groups) {
  199. if (this.linegraph.groups.hasOwnProperty(groupId)) {
  200. if (this.linegraph.groups[groupId].visible == true) {
  201. for (var i = 0; i < this.linegraph.groups[groupId].itemsData.length; i++) {
  202. var item = this.linegraph.groups[groupId].itemsData[i];
  203. var value = util.convert(item.x, 'Date').valueOf();
  204. min = min == null ? value : min > value ? value : min;
  205. max = max == null ? value : max < value ? value : max;
  206. }
  207. }
  208. }
  209. }
  210. return {
  211. min: (min != null) ? new Date(min) : null,
  212. max: (max != null) ? new Date(max) : null
  213. };
  214. };
  215. /**
  216. * Generate Timeline related information from an event
  217. * @param {Event} event
  218. * @return {Object} An object with related information, like on which area
  219. * The event happened, whether clicked on an item, etc.
  220. */
  221. Graph2d.prototype.getEventProperties = function (event) {
  222. var pageX = event.center ? event.center.x : event.pageX;
  223. var pageY = event.center ? event.center.y : event.pageY;
  224. var x = pageX - util.getAbsoluteLeft(this.dom.centerContainer);
  225. var y = pageY - util.getAbsoluteTop(this.dom.centerContainer);
  226. var time = this._toTime(x);
  227. var customTime = CustomTime.customTimeFromTarget(event);
  228. var element = util.getTarget(event);
  229. var what = null;
  230. if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  231. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  232. else if (util.hasParent(element, this.linegraph.yAxisLeft.dom.frame)) {what = 'data-axis';}
  233. else if (util.hasParent(element, this.linegraph.yAxisRight.dom.frame)) {what = 'data-axis';}
  234. else if (util.hasParent(element, this.linegraph.legendLeft.dom.frame)) {what = 'legend';}
  235. else if (util.hasParent(element, this.linegraph.legendRight.dom.frame)) {what = 'legend';}
  236. else if (customTime != null) {what = 'custom-time';}
  237. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  238. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  239. var value = [];
  240. var yAxisLeft = this.linegraph.yAxisLeft;
  241. var yAxisRight = this.linegraph.yAxisRight;
  242. if (!yAxisLeft.hidden) {
  243. value.push(yAxisLeft.screenToValue(y));
  244. }
  245. if (!yAxisRight.hidden) {
  246. value.push(yAxisRight.screenToValue(y));
  247. }
  248. return {
  249. event: event,
  250. what: what,
  251. pageX: pageX,
  252. pageY: pageY,
  253. x: x,
  254. y: y,
  255. time: time,
  256. value: value
  257. }
  258. };
  259. module.exports = Graph2d;