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.

330 lines
9.7 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', // '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. // custom time bar
  81. // Note: time bar will be attached in this.setOptions when selected
  82. this.customTime = new CustomTime(this.body);
  83. this.components.push(this.customTime);
  84. // item set
  85. this.itemSet = new ItemSet(this.body);
  86. this.components.push(this.itemSet);
  87. this.itemsData = null; // DataSet
  88. this.groupsData = null; // DataSet
  89. // apply options
  90. if (options) {
  91. this.setOptions(options);
  92. }
  93. // IMPORTANT: THIS HAPPENS BEFORE SET ITEMS!
  94. if (groups) {
  95. this.setGroups(groups);
  96. }
  97. // create itemset
  98. if (items) {
  99. this.setItems(items);
  100. }
  101. else {
  102. this._redraw();
  103. }
  104. }
  105. // Extend the functionality from Core
  106. Timeline.prototype = new Core();
  107. /**
  108. * Force a redraw. The size of all items will be recalculated.
  109. * Can be useful to manually redraw when option autoResize=false and the window
  110. * has been resized, or when the items CSS has been changed.
  111. */
  112. Timeline.prototype.redraw = function() {
  113. this.itemSet && this.itemSet.markDirty({refreshItems: true});
  114. this._redraw();
  115. };
  116. /**
  117. * Set items
  118. * @param {vis.DataSet | Array | google.visualization.DataTable | null} items
  119. */
  120. Timeline.prototype.setItems = function(items) {
  121. var initialLoad = (this.itemsData == null);
  122. // convert to type DataSet when needed
  123. var newDataSet;
  124. if (!items) {
  125. newDataSet = null;
  126. }
  127. else if (items instanceof DataSet || items instanceof DataView) {
  128. newDataSet = items;
  129. }
  130. else {
  131. // turn an array into a dataset
  132. newDataSet = new DataSet(items, {
  133. type: {
  134. start: 'Date',
  135. end: 'Date'
  136. }
  137. });
  138. }
  139. // set items
  140. this.itemsData = newDataSet;
  141. this.itemSet && this.itemSet.setItems(newDataSet);
  142. if (initialLoad) {
  143. if (this.options.start != undefined || this.options.end != undefined) {
  144. if (this.options.start == undefined || this.options.end == undefined) {
  145. var dataRange = this._getDataRange();
  146. }
  147. var start = this.options.start != undefined ? this.options.start : dataRange.start;
  148. var end = this.options.end != undefined ? this.options.end : dataRange.end;
  149. this.setWindow(start, end, {animate: false});
  150. }
  151. else {
  152. this.fit({animate: false});
  153. }
  154. }
  155. };
  156. /**
  157. * Set groups
  158. * @param {vis.DataSet | Array | google.visualization.DataTable} groups
  159. */
  160. Timeline.prototype.setGroups = function(groups) {
  161. // convert to type DataSet when needed
  162. var newDataSet;
  163. if (!groups) {
  164. newDataSet = null;
  165. }
  166. else if (groups instanceof DataSet || groups instanceof DataView) {
  167. newDataSet = groups;
  168. }
  169. else {
  170. // turn an array into a dataset
  171. newDataSet = new DataSet(groups);
  172. }
  173. this.groupsData = newDataSet;
  174. this.itemSet.setGroups(newDataSet);
  175. };
  176. /**
  177. * Set selected items by their id. Replaces the current selection
  178. * Unknown id's are silently ignored.
  179. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  180. * selected. If ids is an empty array, all items will be
  181. * unselected.
  182. * @param {Object} [options] Available options:
  183. * `focus: boolean`
  184. * If true, focus will be set to the selected item(s)
  185. * `animate: boolean | number`
  186. * If true (default), the range is animated
  187. * smoothly to the new window.
  188. * If a number, the number is taken as duration
  189. * for the animation. Default duration is 500 ms.
  190. * Only applicable when option focus is true.
  191. */
  192. Timeline.prototype.setSelection = function(ids, options) {
  193. this.itemSet && this.itemSet.setSelection(ids);
  194. if (options && options.focus) {
  195. this.focus(ids, options);
  196. }
  197. };
  198. /**
  199. * Get the selected items by their id
  200. * @return {Array} ids The ids of the selected items
  201. */
  202. Timeline.prototype.getSelection = function() {
  203. return this.itemSet && this.itemSet.getSelection() || [];
  204. };
  205. /**
  206. * Adjust the visible window such that the selected item (or multiple items)
  207. * are centered on screen.
  208. * @param {String | String[]} id An item id or array with item ids
  209. * @param {Object} [options] Available options:
  210. * `animate: boolean | number`
  211. * If true (default), the range is animated
  212. * smoothly to the new window.
  213. * If a number, the number is taken as duration
  214. * for the animation. Default duration is 500 ms.
  215. * Only applicable when option focus is true
  216. */
  217. Timeline.prototype.focus = function(id, options) {
  218. if (!this.itemsData || id == undefined) return;
  219. var ids = Array.isArray(id) ? id : [id];
  220. // get the specified item(s)
  221. var itemsData = this.itemsData.getDataSet().get(ids, {
  222. type: {
  223. start: 'Date',
  224. end: 'Date'
  225. }
  226. });
  227. // calculate minimum start and maximum end of specified items
  228. var start = null;
  229. var end = null;
  230. itemsData.forEach(function (itemData) {
  231. var s = itemData.start.valueOf();
  232. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  233. if (start === null || s < start) {
  234. start = s;
  235. }
  236. if (end === null || e > end) {
  237. end = e;
  238. }
  239. });
  240. if (start !== null && end !== null) {
  241. // calculate the new middle and interval for the window
  242. var middle = (start + end) / 2;
  243. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  244. var animate = (options && options.animate !== undefined) ? options.animate : true;
  245. this.range.setRange(middle - interval / 2, middle + interval / 2, animate);
  246. }
  247. };
  248. /**
  249. * Get the data range of the item set.
  250. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  251. * When no minimum is found, min==null
  252. * When no maximum is found, max==null
  253. */
  254. Timeline.prototype.getItemRange = function() {
  255. // calculate min from start filed
  256. var dataset = this.itemsData.getDataSet(),
  257. min = null,
  258. max = null;
  259. if (dataset) {
  260. // calculate the minimum value of the field 'start'
  261. var minItem = dataset.min('start');
  262. min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null;
  263. // Note: we convert first to Date and then to number because else
  264. // a conversion from ISODate to Number will fail
  265. // calculate maximum value of fields 'start' and 'end'
  266. var maxStartItem = dataset.max('start');
  267. if (maxStartItem) {
  268. max = util.convert(maxStartItem.start, 'Date').valueOf();
  269. }
  270. var maxEndItem = dataset.max('end');
  271. if (maxEndItem) {
  272. if (max == null) {
  273. max = util.convert(maxEndItem.end, 'Date').valueOf();
  274. }
  275. else {
  276. max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf());
  277. }
  278. }
  279. }
  280. return {
  281. min: (min != null) ? new Date(min) : null,
  282. max: (max != null) ? new Date(max) : null
  283. };
  284. };
  285. module.exports = Timeline;