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.

314 lines
9.2 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 {vis.DataSet | 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 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',
  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. snap: null, // will be specified after TimeAxis is created
  58. toScreen: me._toScreen.bind(me),
  59. toGlobalScreen: me._toGlobalScreen.bind(me), // this refers to the root.width
  60. toTime: me._toTime.bind(me),
  61. toGlobalTime : me._toGlobalTime.bind(me)
  62. }
  63. };
  64. // range
  65. this.range = new Range(this.body);
  66. this.components.push(this.range);
  67. this.body.range = this.range;
  68. // time axis
  69. this.timeAxis = new TimeAxis(this.body);
  70. this.components.push(this.timeAxis);
  71. this.body.util.snap = this.timeAxis.snap.bind(this.timeAxis);
  72. // current time bar
  73. this.currentTime = new CurrentTime(this.body);
  74. this.components.push(this.currentTime);
  75. // custom time bar
  76. // Note: time bar will be attached in this.setOptions when selected
  77. this.customTime = new CustomTime(this.body);
  78. this.components.push(this.customTime);
  79. // item set
  80. this.itemSet = new ItemSet(this.body);
  81. this.components.push(this.itemSet);
  82. this.itemsData = null; // DataSet
  83. this.groupsData = null; // DataSet
  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. Timeline.prototype = new Core();
  102. /**
  103. * Set items
  104. * @param {vis.DataSet | Array | google.visualization.DataTable | null} items
  105. */
  106. Timeline.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.itemSet && this.itemSet.setItems(newDataSet);
  128. if (initialLoad) {
  129. if (this.options.start != undefined || this.options.end != undefined) {
  130. if (this.options.start == undefined || this.options.end == undefined) {
  131. var dataRange = this._getDataRange();
  132. }
  133. var start = this.options.start != undefined ? this.options.start : dataRange.start;
  134. var end = this.options.end != undefined ? this.options.end : dataRange.end;
  135. this.setWindow(start, end, {animate: false});
  136. }
  137. else {
  138. this.fit({animate: false});
  139. }
  140. }
  141. };
  142. /**
  143. * Set groups
  144. * @param {vis.DataSet | Array | google.visualization.DataTable} groups
  145. */
  146. Timeline.prototype.setGroups = function(groups) {
  147. // convert to type DataSet when needed
  148. var newDataSet;
  149. if (!groups) {
  150. newDataSet = null;
  151. }
  152. else if (groups instanceof DataSet || groups instanceof DataView) {
  153. newDataSet = groups;
  154. }
  155. else {
  156. // turn an array into a dataset
  157. newDataSet = new DataSet(groups);
  158. }
  159. this.groupsData = newDataSet;
  160. this.itemSet.setGroups(newDataSet);
  161. };
  162. /**
  163. * Set selected items by their id. Replaces the current selection
  164. * Unknown id's are silently ignored.
  165. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  166. * selected. If ids is an empty array, all items will be
  167. * unselected.
  168. * @param {Object} [options] Available options:
  169. * `focus: boolean`
  170. * If true, focus will be set to the selected item(s)
  171. * `animate: boolean | number`
  172. * If true (default), the range is animated
  173. * smoothly to the new window.
  174. * If a number, the number is taken as duration
  175. * for the animation. Default duration is 500 ms.
  176. * Only applicable when option focus is true.
  177. */
  178. Timeline.prototype.setSelection = function(ids, options) {
  179. this.itemSet && this.itemSet.setSelection(ids);
  180. if (options && options.focus) {
  181. this.focus(ids, options);
  182. }
  183. };
  184. /**
  185. * Get the selected items by their id
  186. * @return {Array} ids The ids of the selected items
  187. */
  188. Timeline.prototype.getSelection = function() {
  189. return this.itemSet && this.itemSet.getSelection() || [];
  190. };
  191. /**
  192. * Adjust the visible window such that the selected item (or multiple items)
  193. * are centered on screen.
  194. * @param {String | String[]} id An item id or array with item ids
  195. * @param {Object} [options] Available options:
  196. * `animate: boolean | number`
  197. * If true (default), the range is animated
  198. * smoothly to the new window.
  199. * If a number, the number is taken as duration
  200. * for the animation. Default duration is 500 ms.
  201. * Only applicable when option focus is true
  202. */
  203. Timeline.prototype.focus = function(id, options) {
  204. if (!this.itemsData || id == undefined) return;
  205. var ids = Array.isArray(id) ? id : [id];
  206. // get the specified item(s)
  207. var itemsData = this.itemsData.getDataSet().get(ids, {
  208. type: {
  209. start: 'Date',
  210. end: 'Date'
  211. }
  212. });
  213. // calculate minimum start and maximum end of specified items
  214. var start = null;
  215. var end = null;
  216. itemsData.forEach(function (itemData) {
  217. var s = itemData.start.valueOf();
  218. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  219. if (start === null || s < start) {
  220. start = s;
  221. }
  222. if (end === null || e > end) {
  223. end = e;
  224. }
  225. });
  226. if (start !== null && end !== null) {
  227. // calculate the new middle and interval for the window
  228. var middle = (start + end) / 2;
  229. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  230. var animate = (options && options.animate !== undefined) ? options.animate : true;
  231. this.range.setRange(middle - interval / 2, middle + interval / 2, animate);
  232. }
  233. };
  234. /**
  235. * Get the data range of the item set.
  236. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  237. * When no minimum is found, min==null
  238. * When no maximum is found, max==null
  239. */
  240. Timeline.prototype.getItemRange = function() {
  241. // calculate min from start filed
  242. var dataset = this.itemsData.getDataSet(),
  243. min = null,
  244. max = null;
  245. if (dataset) {
  246. // calculate the minimum value of the field 'start'
  247. var minItem = dataset.min('start');
  248. min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null;
  249. // Note: we convert first to Date and then to number because else
  250. // a conversion from ISODate to Number will fail
  251. // calculate maximum value of fields 'start' and 'end'
  252. var maxStartItem = dataset.max('start');
  253. if (maxStartItem) {
  254. max = util.convert(maxStartItem.start, 'Date').valueOf();
  255. }
  256. var maxEndItem = dataset.max('end');
  257. if (maxEndItem) {
  258. if (max == null) {
  259. max = util.convert(maxEndItem.end, 'Date').valueOf();
  260. }
  261. else {
  262. max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf());
  263. }
  264. }
  265. }
  266. return {
  267. min: (min != null) ? new Date(min) : null,
  268. max: (max != null) ? new Date(max) : null
  269. };
  270. };
  271. module.exports = Timeline;