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.

316 lines
9.3 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. console.log(this.options.start, this.options.end, dataRange)
  136. this.setWindow(start, end, {animate: false});
  137. }
  138. else {
  139. this.fit({animate: false});
  140. }
  141. }
  142. };
  143. /**
  144. * Set groups
  145. * @param {vis.DataSet | Array | google.visualization.DataTable} groups
  146. */
  147. Timeline.prototype.setGroups = function(groups) {
  148. // convert to type DataSet when needed
  149. var newDataSet;
  150. if (!groups) {
  151. newDataSet = null;
  152. }
  153. else if (groups instanceof DataSet || groups instanceof DataView) {
  154. newDataSet = groups;
  155. }
  156. else {
  157. // turn an array into a dataset
  158. newDataSet = new DataSet(groups);
  159. }
  160. this.groupsData = newDataSet;
  161. this.itemSet.setGroups(newDataSet);
  162. };
  163. /**
  164. * Set selected items by their id. Replaces the current selection
  165. * Unknown id's are silently ignored.
  166. * @param {string[] | string} [ids] An array with zero or more id's of the items to be
  167. * selected. If ids is an empty array, all items will be
  168. * unselected.
  169. * @param {Object} [options] Available options:
  170. * `focus: boolean`
  171. * If true, focus will be set to the selected item(s)
  172. * `animate: boolean | number`
  173. * If true (default), the range is animated
  174. * smoothly to the new window.
  175. * If a number, the number is taken as duration
  176. * for the animation. Default duration is 500 ms.
  177. * Only applicable when option focus is true.
  178. */
  179. Timeline.prototype.setSelection = function(ids, options) {
  180. this.itemSet && this.itemSet.setSelection(ids);
  181. if (options && options.focus) {
  182. this.focus(ids, options);
  183. }
  184. };
  185. /**
  186. * Get the selected items by their id
  187. * @return {Array} ids The ids of the selected items
  188. */
  189. Timeline.prototype.getSelection = function() {
  190. return this.itemSet && this.itemSet.getSelection() || [];
  191. };
  192. /**
  193. * Adjust the visible window such that the selected item (or multiple items)
  194. * are centered on screen.
  195. * @param {String | String[]} id An item id or array with item ids
  196. * @param {Object} [options] Available options:
  197. * `animate: boolean | number`
  198. * If true (default), the range is animated
  199. * smoothly to the new window.
  200. * If a number, the number is taken as duration
  201. * for the animation. Default duration is 500 ms.
  202. * Only applicable when option focus is true
  203. */
  204. Timeline.prototype.focus = function(id, options) {
  205. if (!this.itemsData || id == undefined) return;
  206. var ids = Array.isArray(id) ? id : [id];
  207. // get the specified item(s)
  208. var itemsData = this.itemsData.getDataSet().get(ids, {
  209. type: {
  210. start: 'Date',
  211. end: 'Date'
  212. }
  213. });
  214. // calculate minimum start and maximum end of specified items
  215. var start = null;
  216. var end = null;
  217. itemsData.forEach(function (itemData) {
  218. var s = itemData.start.valueOf();
  219. var e = 'end' in itemData ? itemData.end.valueOf() : itemData.start.valueOf();
  220. if (start === null || s < start) {
  221. start = s;
  222. }
  223. if (end === null || e > end) {
  224. end = e;
  225. }
  226. });
  227. if (start !== null && end !== null) {
  228. // calculate the new middle and interval for the window
  229. var middle = (start + end) / 2;
  230. var interval = Math.max((this.range.end - this.range.start), (end - start) * 1.1);
  231. var animate = (options && options.animate !== undefined) ? options.animate : true;
  232. this.range.setRange(middle - interval / 2, middle + interval / 2, animate);
  233. }
  234. };
  235. /**
  236. * Get the data range of the item set.
  237. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  238. * When no minimum is found, min==null
  239. * When no maximum is found, max==null
  240. */
  241. Timeline.prototype.getItemRange = function() {
  242. // calculate min from start filed
  243. var dataset = this.itemsData.getDataSet(),
  244. min = null,
  245. max = null;
  246. if (dataset) {
  247. // calculate the minimum value of the field 'start'
  248. var minItem = dataset.min('start');
  249. min = minItem ? util.convert(minItem.start, 'Date').valueOf() : null;
  250. // Note: we convert first to Date and then to number because else
  251. // a conversion from ISODate to Number will fail
  252. // calculate maximum value of fields 'start' and 'end'
  253. var maxStartItem = dataset.max('start');
  254. if (maxStartItem) {
  255. max = util.convert(maxStartItem.start, 'Date').valueOf();
  256. }
  257. var maxEndItem = dataset.max('end');
  258. if (maxEndItem) {
  259. if (max == null) {
  260. max = util.convert(maxEndItem.end, 'Date').valueOf();
  261. }
  262. else {
  263. max = Math.max(max, util.convert(maxEndItem.end, 'Date').valueOf());
  264. }
  265. }
  266. }
  267. return {
  268. min: (min != null) ? new Date(min) : null,
  269. max: (max != null) ? new Date(max) : null
  270. };
  271. };
  272. module.exports = Timeline;