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.

295 lines
8.6 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 | Array | google.visualization.DataTable} [items]
  16. * @param {Object} [options] See Timeline.setOptions for the available options.
  17. * @constructor
  18. * @extends Core
  19. */
  20. function Timeline (container, items, options) {
  21. if (!(this instanceof Timeline)) {
  22. throw new SyntaxError('Constructor must be called with the new operator');
  23. }
  24. var me = this;
  25. this.defaultOptions = {
  26. start: null,
  27. end: null,
  28. autoResize: true,
  29. orientation: 'bottom',
  30. width: null,
  31. height: null,
  32. maxHeight: null,
  33. minHeight: null
  34. };
  35. this.options = util.deepExtend({}, this.defaultOptions);
  36. // Create the DOM, props, and emitter
  37. this._create(container);
  38. // all components listed here will be repainted automatically
  39. this.components = [];
  40. this.body = {
  41. dom: this.dom,
  42. domProps: this.props,
  43. emitter: {
  44. on: this.on.bind(this),
  45. off: this.off.bind(this),
  46. emit: this.emit.bind(this)
  47. },
  48. util: {
  49. snap: null, // will be specified after TimeAxis is created
  50. toScreen: me._toScreen.bind(me),
  51. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  52. toTime: me._toTime.bind(me),
  53. toGlobalTime : me._toGlobalTime.bind(me)
  54. }
  55. };
  56. // range
  57. this.range = new Range(this.body);
  58. this.components.push(this.range);
  59. this.body.range = this.range;
  60. // time axis
  61. this.timeAxis = new TimeAxis(this.body);
  62. this.components.push(this.timeAxis);
  63. this.body.util.snap = this.timeAxis.snap.bind(this.timeAxis);
  64. // current time bar
  65. this.currentTime = new CurrentTime(this.body);
  66. this.components.push(this.currentTime);
  67. // custom time bar
  68. // Note: time bar will be attached in this.setOptions when selected
  69. this.customTime = new CustomTime(this.body);
  70. this.components.push(this.customTime);
  71. // item set
  72. this.itemSet = new ItemSet(this.body);
  73. this.components.push(this.itemSet);
  74. this.itemsData = null; // DataSet
  75. this.groupsData = null; // DataSet
  76. // apply options
  77. if (options) {
  78. this.setOptions(options);
  79. }
  80. // create itemset
  81. if (items) {
  82. this.setItems(items);
  83. }
  84. else {
  85. this.redraw();
  86. }
  87. }
  88. // Extend the functionality from Core
  89. Timeline.prototype = new Core();
  90. /**
  91. * Set items
  92. * @param {vis.DataSet | Array | google.visualization.DataTable | null} items
  93. */
  94. Timeline.prototype.setItems = function(items) {
  95. var initialLoad = (this.itemsData == null);
  96. // convert to type DataSet when needed
  97. var newDataSet;
  98. if (!items) {
  99. newDataSet = null;
  100. }
  101. else if (items instanceof DataSet || items instanceof DataView) {
  102. newDataSet = items;
  103. }
  104. else {
  105. // turn an array into a dataset
  106. newDataSet = new DataSet(items, {
  107. type: {
  108. start: 'Date',
  109. end: 'Date'
  110. }
  111. });
  112. }
  113. // set items
  114. this.itemsData = newDataSet;
  115. this.itemSet && this.itemSet.setItems(newDataSet);
  116. if (initialLoad) {
  117. if (this.options.start != undefined || this.options.end != undefined) {
  118. var start = this.options.start != undefined ? this.options.start : null;
  119. var end = this.options.end != undefined ? this.options.end : null;
  120. this.setWindow(start, end, {animate: false});
  121. }
  122. else {
  123. this.fit({animate: false});
  124. }
  125. }
  126. };
  127. /**
  128. * Set groups
  129. * @param {vis.DataSet | Array | google.visualization.DataTable} groups
  130. */
  131. Timeline.prototype.setGroups = function(groups) {
  132. // convert to type DataSet when needed
  133. var newDataSet;
  134. if (!groups) {
  135. newDataSet = null;
  136. }
  137. else if (groups instanceof DataSet || groups instanceof DataView) {
  138. newDataSet = groups;
  139. }
  140. else {
  141. // turn an array into a dataset
  142. newDataSet = new DataSet(groups);
  143. }
  144. this.groupsData = newDataSet;
  145. this.itemSet.setGroups(newDataSet);
  146. };
  147. /**
  148. * Set selected items by their id. Replaces the current selection
  149. * Unknown id's are silently ignored.
  150. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  151. * selected. If ids is an empty array, all items will be
  152. * unselected.
  153. * @param {Object} [options] Available options:
  154. * `focus: boolean`
  155. * If true, focus will be set to the selected item(s)
  156. * `animate: boolean | number`
  157. * If true (default), the range is animated
  158. * smoothly to the new window.
  159. * If a number, the number is taken as duration
  160. * for the animation. Default duration is 500 ms.
  161. * Only applicable when option focus is true.
  162. */
  163. Timeline.prototype.setSelection = function(ids, options) {
  164. this.itemSet && this.itemSet.setSelection(ids);
  165. if (options && options.focus) {
  166. this.focus(ids, options);
  167. }
  168. };
  169. /**
  170. * Get the selected items by their id
  171. * @return {Array} ids The ids of the selected items
  172. */
  173. Timeline.prototype.getSelection = function() {
  174. return this.itemSet && this.itemSet.getSelection() || [];
  175. };
  176. /**
  177. * Adjust the visible window such that the selected item (or multiple items)
  178. * are centered on screen.
  179. * @param {String | String[]} id An item id or array with item ids
  180. * @param {Object} [options] Available options:
  181. * `animate: boolean | number`
  182. * If true (default), the range is animated
  183. * smoothly to the new window.
  184. * If a number, the number is taken as duration
  185. * for the animation. Default duration is 500 ms.
  186. * Only applicable when option focus is true
  187. */
  188. Timeline.prototype.focus = function(id, options) {
  189. if (!this.itemsData || id == undefined) return;
  190. var ids = Array.isArray(id) ? id : [id];
  191. // get the specified item(s)
  192. var itemsData = this.itemsData.getDataSet().get(ids, {
  193. type: {
  194. start: 'Date',
  195. end: 'Date'
  196. }
  197. });
  198. // calculate minimum start and maximum end of specified items
  199. var start = null;
  200. var end = null;
  201. itemsData.forEach(function (itemData) {
  202. var s = itemData.start.valueOf();
  203. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  204. if (start === null || s < start) {
  205. start = s;
  206. }
  207. if (end === null || e > end) {
  208. end = e;
  209. }
  210. });
  211. if (start !== null && end !== null) {
  212. // calculate the new middle and interval for the window
  213. var middle = (start + end) / 2;
  214. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  215. var animate = (options && options.animate !== undefined) ? options.animate : true;
  216. this.range.setRange(middle - interval / 2, middle + interval / 2, animate);
  217. }
  218. };
  219. /**
  220. * Get the data range of the item set.
  221. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  222. * When no minimum is found, min==null
  223. * When no maximum is found, max==null
  224. */
  225. Timeline.prototype.getItemRange = function() {
  226. // calculate min from start filed
  227. var dataset = this.itemsData.getDataSet(),
  228. min = null,
  229. max = null;
  230. if (dataset) {
  231. // calculate the minimum value of the field 'start'
  232. var minItem = dataset.min('start');
  233. min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null;
  234. // Note: we convert first to Date and then to number because else
  235. // a conversion from ISODate to Number will fail
  236. // calculate maximum value of fields 'start' and 'end'
  237. var maxStartItem = dataset.max('start');
  238. if (maxStartItem) {
  239. max = util.convert(maxStartItem.start, 'Date').valueOf();
  240. }
  241. var maxEndItem = dataset.max('end');
  242. if (maxEndItem) {
  243. if (max == null) {
  244. max = util.convert(maxEndItem.end, 'Date').valueOf();
  245. }
  246. else {
  247. max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf());
  248. }
  249. }
  250. }
  251. return {
  252. min: (min != null) ? new Date(min) : null,
  253. max: (max != null) ? new Date(max) : null
  254. };
  255. };
  256. module.exports = Timeline;