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.

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