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.

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