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.

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