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.

399 lines
12 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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 ItemSet = require('./component/ItemSet');
  12. /**
  13. * Create a timeline visualization
  14. * @param {HTMLElement} container
  15. * @param {vis.DataSet | vis.DataView | Array} [items]
  16. * @param {vis.DataSet | vis.DataView | Array} [groups]
  17. * @param {Object} [options] See Timeline.setOptions for the available options.
  18. * @constructor
  19. * @extends Core
  20. */
  21. function Timeline (container, items, groups, options) {
  22. if (!(this instanceof Timeline)) {
  23. throw new SyntaxError('Constructor must be called with the new operator');
  24. }
  25. // if the third element is options, the forth is groups (optionally);
  26. if (!(Array.isArray(groups) || groups instanceof DataSet || groups instanceof DataView) && groups instanceof Object) {
  27. var forthArgument = options;
  28. options = groups;
  29. groups = forthArgument;
  30. }
  31. var me = this;
  32. this.defaultOptions = {
  33. start: null,
  34. end: null,
  35. autoResize: true,
  36. orientation: {
  37. axis: 'bottom', // axis orientation: 'bottom', 'top', or 'both'
  38. item: 'bottom' // not relevant
  39. },
  40. width: null,
  41. height: null,
  42. maxHeight: null,
  43. minHeight: null
  44. };
  45. this.options = util.deepExtend({}, this.defaultOptions);
  46. // Create the DOM, props, and emitter
  47. this._create(container);
  48. // all components listed here will be repainted automatically
  49. this.components = [];
  50. this.body = {
  51. dom: this.dom,
  52. domProps: this.props,
  53. emitter: {
  54. on: this.on.bind(this),
  55. off: this.off.bind(this),
  56. emit: this.emit.bind(this)
  57. },
  58. hiddenDates: [],
  59. util: {
  60. getScale: function () {
  61. return me.timeAxis.step.scale;
  62. },
  63. getStep: function () {
  64. return me.timeAxis.step.step;
  65. },
  66. toScreen: me._toScreen.bind(me),
  67. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  68. toTime: me._toTime.bind(me),
  69. toGlobalTime : me._toGlobalTime.bind(me)
  70. }
  71. };
  72. // range
  73. this.range = new Range(this.body);
  74. this.components.push(this.range);
  75. this.body.range = this.range;
  76. // time axis
  77. this.timeAxis = new TimeAxis(this.body);
  78. this.timeAxis2 = null; // used in case of orientation option 'both'
  79. this.components.push(this.timeAxis);
  80. // current time bar
  81. this.currentTime = new CurrentTime(this.body);
  82. this.components.push(this.currentTime);
  83. // item set
  84. this.itemSet = new ItemSet(this.body);
  85. this.components.push(this.itemSet);
  86. this.itemsData = null; // DataSet
  87. this.groupsData = null; // DataSet
  88. this.on('tap', function (event) {
  89. me.emit('click', me.getEventProperties(event))
  90. });
  91. this.on('doubletap', function (event) {
  92. me.emit('doubleClick', me.getEventProperties(event))
  93. });
  94. this.dom.root.oncontextmenu = function (event) {
  95. me.emit('contextmenu', me.getEventProperties(event))
  96. };
  97. // apply options
  98. if (options) {
  99. this.setOptions(options);
  100. }
  101. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  102. if (groups) {
  103. this.setGroups(groups);
  104. }
  105. // create itemset
  106. if (items) {
  107. this.setItems(items);
  108. }
  109. else {
  110. this._redraw();
  111. }
  112. }
  113. // Extend the functionality from Core
  114. Timeline.prototype = new Core();
  115. /**
  116. * Force a redraw. The size of all items will be recalculated.
  117. * Can be useful to manually redraw when option autoResize=false and the window
  118. * has been resized, or when the items CSS has been changed.
  119. */
  120. Timeline.prototype.redraw = function() {
  121. this.itemSet && this.itemSet.markDirty({refreshItems: true});
  122. this._redraw();
  123. };
  124. /**
  125. * Set items
  126. * @param {vis.DataSet | Array | null} items
  127. */
  128. Timeline.prototype.setItems = function(items) {
  129. var initialLoad = (this.itemsData == null);
  130. // convert to type DataSet when needed
  131. var newDataSet;
  132. if (!items) {
  133. newDataSet = null;
  134. }
  135. else if (items instanceof DataSet || items instanceof DataView) {
  136. newDataSet = items;
  137. }
  138. else {
  139. // turn an array into a dataset
  140. newDataSet = new DataSet(items, {
  141. type: {
  142. start: 'Date',
  143. end: 'Date'
  144. }
  145. });
  146. }
  147. // set items
  148. this.itemsData = newDataSet;
  149. this.itemSet && this.itemSet.setItems(newDataSet);
  150. if (initialLoad) {
  151. if (this.options.start != undefined || this.options.end != undefined) {
  152. if (this.options.start == undefined || this.options.end == undefined) {
  153. var dataRange = this._getDataRange();
  154. }
  155. var start = this.options.start != undefined ? this.options.start : dataRange.start;
  156. var end = this.options.end != undefined ? this.options.end : dataRange.end;
  157. this.setWindow(start, end, {animation: false});
  158. }
  159. else {
  160. this.fit({animation: false});
  161. }
  162. }
  163. };
  164. /**
  165. * Set groups
  166. * @param {vis.DataSet | Array} groups
  167. */
  168. Timeline.prototype.setGroups = function(groups) {
  169. // convert to type DataSet when needed
  170. var newDataSet;
  171. if (!groups) {
  172. newDataSet = null;
  173. }
  174. else if (groups instanceof DataSet || groups instanceof DataView) {
  175. newDataSet = groups;
  176. }
  177. else {
  178. // turn an array into a dataset
  179. newDataSet = new DataSet(groups);
  180. }
  181. this.groupsData = newDataSet;
  182. this.itemSet.setGroups(newDataSet);
  183. };
  184. /**
  185. * Set both items and groups in one go
  186. * @param {{items: Array | vis.DataSet, groups: Array | vis.DataSet}} data
  187. */
  188. Timeline.prototype.setData = function (data) {
  189. if (data && data.groups) {
  190. this.setGroups(data.groups);
  191. }
  192. if (data && data.items) {
  193. this.setItems(data.items);
  194. }
  195. };
  196. /**
  197. * Set selected items by their id. Replaces the current selection
  198. * Unknown id's are silently ignored.
  199. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  200. * selected. If ids is an empty array, all items will be
  201. * unselected.
  202. * @param {Object} [options] Available options:
  203. * `focus: boolean`
  204. * If true, focus will be set to the selected item(s)
  205. * `animation: boolean | {duration: number, easingFunction: string}`
  206. * If true (default), the range is animated
  207. * smoothly to the new window. An object can be
  208. * provided to specify duration and easing function.
  209. * Default duration is 500 ms, and default easing
  210. * function is 'easeInOutQuad'.
  211. * Only applicable when option focus is true.
  212. */
  213. Timeline.prototype.setSelection = function(ids, options) {
  214. this.itemSet && this.itemSet.setSelection(ids);
  215. if (options && options.focus) {
  216. this.focus(ids, options);
  217. }
  218. };
  219. /**
  220. * Get the selected items by their id
  221. * @return {Array} ids The ids of the selected items
  222. */
  223. Timeline.prototype.getSelection = function() {
  224. return this.itemSet && this.itemSet.getSelection() || [];
  225. };
  226. /**
  227. * Adjust the visible window such that the selected item (or multiple items)
  228. * are centered on screen.
  229. * @param {String | String[]} id An item id or array with item ids
  230. * @param {Object} [options] Available options:
  231. * `animation: boolean | {duration: number, easingFunction: string}`
  232. * If true (default), the range is animated
  233. * smoothly to the new window. An object can be
  234. * provided to specify duration and easing function.
  235. * Default duration is 500 ms, and default easing
  236. * function is 'easeInOutQuad'.
  237. */
  238. Timeline.prototype.focus = function(id, options) {
  239. if (!this.itemsData || id == undefined) return;
  240. var ids = Array.isArray(id) ? id : [id];
  241. // get the specified item(s)
  242. var itemsData = this.itemsData.getDataSet().get(ids, {
  243. type: {
  244. start: 'Date',
  245. end: 'Date'
  246. }
  247. });
  248. // calculate minimum start and maximum end of specified items
  249. var start = null;
  250. var end = null;
  251. itemsData.forEach(function (itemData) {
  252. var s = itemData.start.valueOf();
  253. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  254. if (start === null || s < start) {
  255. start = s;
  256. }
  257. if (end === null || e > end) {
  258. end = e;
  259. }
  260. });
  261. if (start !== null && end !== null) {
  262. // calculate the new middle and interval for the window
  263. var middle = (start + end) / 2;
  264. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  265. var animation = (options && options.animation !== undefined) ? options.animation : true;
  266. this.range.setRange(middle - interval / 2, middle + interval / 2, animation);
  267. }
  268. };
  269. /**
  270. * Get the data range of the item set.
  271. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  272. * When no minimum is found, min==null
  273. * When no maximum is found, max==null
  274. */
  275. Timeline.prototype.getItemRange = function() {
  276. // calculate min from start filed
  277. var dataset = this.itemsData.getDataSet(),
  278. min = null,
  279. max = null;
  280. if (dataset) {
  281. // calculate the minimum value of the field 'start'
  282. var minItem = dataset.min('start');
  283. min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null;
  284. // Note: we convert first to Date and then to number because else
  285. // a conversion from ISODate to Number will fail
  286. // calculate maximum value of fields 'start' and 'end'
  287. var maxStartItem = dataset.max('start');
  288. if (maxStartItem) {
  289. max = util.convert(maxStartItem.start, 'Date').valueOf();
  290. }
  291. var maxEndItem = dataset.max('end');
  292. if (maxEndItem) {
  293. if (max == null) {
  294. max = util.convert(maxEndItem.end, 'Date').valueOf();
  295. }
  296. else {
  297. max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf());
  298. }
  299. }
  300. }
  301. return {
  302. min: (min != null) ? new Date(min) : null,
  303. max: (max != null) ? new Date(max) : null
  304. };
  305. };
  306. /**
  307. * Generate Timeline related information from an event
  308. * @param {Event} event
  309. * @return {Object} An object with related information, like on which area
  310. * The event happened, whether clicked on an item, etc.
  311. */
  312. Timeline.prototype.getEventProperties = function (event) {
  313. var pageX = event.center ? event.center.x : event.pageX;
  314. var pageY = event.center ? event.center.y : event.pageY;
  315. var x = pageX - util.getAbsoluteLeft(this.dom.centerContainer);
  316. var y = pageY - util.getAbsoluteTop(this.dom.centerContainer);
  317. var item = this.itemSet.itemFromTarget(event);
  318. var group = this.itemSet.groupFromTarget(event);
  319. var customTime = CustomTime.customTimeFromTarget(event);
  320. var snap = this.itemSet.options.snap || null;
  321. var scale = this.body.util.getScale();
  322. var step = this.body.util.getStep();
  323. var time = this._toTime(x);
  324. var snappedTime = snap ? snap(time, scale, step) : time;
  325. var element = util.getTarget(event);
  326. var what = null;
  327. if (item != null) {what = 'item';}
  328. else if (customTime != null) {what = 'custom-time';}
  329. else if (util.hasParent(element, this.timeAxis.dom.foreground)) {what = 'axis';}
  330. else if (this.timeAxis2 && util.hasParent(element, this.timeAxis2.dom.foreground)) {what = 'axis';}
  331. else if (util.hasParent(element, this.itemSet.dom.labelSet)) {what = 'group-label';}
  332. else if (util.hasParent(element, this.currentTime.bar)) {what = 'current-time';}
  333. else if (util.hasParent(element, this.dom.center)) {what = 'background';}
  334. return {
  335. event: event,
  336. item: item ? item.id : null,
  337. group: group ? group.groupId : null,
  338. what: what,
  339. pageX: pageX,
  340. pageY: pageY,
  341. x: x,
  342. y: y,
  343. time: time,
  344. snappedTime: snappedTime
  345. }
  346. };
  347. module.exports = Timeline;