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.

485 lines
15 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var util = require('../../util');
  2. var Component = require('./Component');
  3. var TimeStep = require('../TimeStep');
  4. var DateUtil = require('../DateUtil');
  5. var moment = require('../../module/moment');
  6. /**
  7. * A horizontal time axis
  8. * @param {{dom: Object, domProps: Object, emitter: Emitter, range: Range}} body
  9. * @param {Object} [options] See TimeAxis.setOptions for the available
  10. * options.
  11. * @constructor TimeAxis
  12. * @extends Component
  13. */
  14. function TimeAxis (body, options) {
  15. this.dom = {
  16. foreground: null,
  17. lines: [],
  18. majorTexts: [],
  19. minorTexts: [],
  20. redundant: {
  21. lines: [],
  22. majorTexts: [],
  23. minorTexts: []
  24. }
  25. };
  26. this.props = {
  27. range: {
  28. start: 0,
  29. end: 0,
  30. minimumStep: 0
  31. },
  32. lineTop: 0
  33. };
  34. this.defaultOptions = {
  35. orientation: {
  36. axis: 'bottom'
  37. }, // axis orientation: 'top' or 'bottom'
  38. showMinorLabels: true,
  39. showMajorLabels: true,
  40. maxMinorChars: 7,
  41. format: TimeStep.FORMAT,
  42. moment: moment,
  43. timeAxis: null
  44. };
  45. this.options = util.extend({}, this.defaultOptions);
  46. this.body = body;
  47. // create the HTML DOM
  48. this._create();
  49. this.setOptions(options);
  50. }
  51. TimeAxis.prototype = new Component();
  52. /**
  53. * Set options for the TimeAxis.
  54. * Parameters will be merged in current options.
  55. * @param {Object} options Available options:
  56. * {string} [orientation.axis]
  57. * {boolean} [showMinorLabels]
  58. * {boolean} [showMajorLabels]
  59. */
  60. TimeAxis.prototype.setOptions = function(options) {
  61. if (options) {
  62. // copy all options that we know
  63. util.selectiveExtend([
  64. 'showMinorLabels',
  65. 'showMajorLabels',
  66. 'maxMinorChars',
  67. 'hiddenDates',
  68. 'timeAxis',
  69. 'moment'
  70. ], this.options, options);
  71. // deep copy the format options
  72. util.selectiveDeepExtend(['format'], this.options, options);
  73. if ('orientation' in options) {
  74. if (typeof options.orientation === 'string') {
  75. this.options.orientation.axis = options.orientation;
  76. }
  77. else if (typeof options.orientation === 'object' && 'axis' in options.orientation) {
  78. this.options.orientation.axis = options.orientation.axis;
  79. }
  80. }
  81. // apply locale to moment.js
  82. // TODO: not so nice, this is applied globally to moment.js
  83. if ('locale' in options) {
  84. if (typeof moment.locale === 'function') {
  85. // moment.js 2.8.1+
  86. moment.locale(options.locale);
  87. }
  88. else {
  89. moment.lang(options.locale);
  90. }
  91. }
  92. }
  93. };
  94. /**
  95. * Create the HTML DOM for the TimeAxis
  96. */
  97. TimeAxis.prototype._create = function() {
  98. this.dom.foreground = document.createElement('div');
  99. this.dom.background = document.createElement('div');
  100. this.dom.foreground.className = 'vis-time-axis vis-foreground';
  101. this.dom.background.className = 'vis-time-axis vis-background';
  102. };
  103. /**
  104. * Destroy the TimeAxis
  105. */
  106. TimeAxis.prototype.destroy = function() {
  107. // remove from DOM
  108. if (this.dom.foreground.parentNode) {
  109. this.dom.foreground.parentNode.removeChild(this.dom.foreground);
  110. }
  111. if (this.dom.background.parentNode) {
  112. this.dom.background.parentNode.removeChild(this.dom.background);
  113. }
  114. this.body = null;
  115. };
  116. /**
  117. * Repaint the component
  118. * @return {boolean} Returns true if the component is resized
  119. */
  120. TimeAxis.prototype.redraw = function () {
  121. var props = this.props;
  122. var foreground = this.dom.foreground;
  123. var background = this.dom.background;
  124. // determine the correct parent DOM element (depending on option orientation)
  125. var parent = (this.options.orientation.axis == 'top') ? this.body.dom.top : this.body.dom.bottom;
  126. var parentChanged = (foreground.parentNode !== parent);
  127. // calculate character width and height
  128. this._calculateCharSize();
  129. // TODO: recalculate sizes only needed when parent is resized or options is changed
  130. var showMinorLabels = this.options.showMinorLabels && this.options.orientation.axis !== 'none';
  131. var showMajorLabels = this.options.showMajorLabels && this.options.orientation.axis !== 'none';
  132. // determine the width and height of the elemens for the axis
  133. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  134. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  135. props.height = props.minorLabelHeight + props.majorLabelHeight;
  136. props.width = foreground.offsetWidth;
  137. props.minorLineHeight = this.body.domProps.root.height - props.majorLabelHeight -
  138. (this.options.orientation.axis == 'top' ? this.body.domProps.bottom.height : this.body.domProps.top.height);
  139. props.minorLineWidth = 1; // TODO: really calculate width
  140. props.majorLineHeight = props.minorLineHeight + props.majorLabelHeight;
  141. props.majorLineWidth = 1; // TODO: really calculate width
  142. // take foreground and background offline while updating (is almost twice as fast)
  143. var foregroundNextSibling = foreground.nextSibling;
  144. var backgroundNextSibling = background.nextSibling;
  145. foreground.parentNode && foreground.parentNode.removeChild(foreground);
  146. background.parentNode && background.parentNode.removeChild(background);
  147. foreground.style.height = this.props.height + 'px';
  148. this._repaintLabels();
  149. // put DOM online again (at the same place)
  150. if (foregroundNextSibling) {
  151. parent.insertBefore(foreground, foregroundNextSibling);
  152. }
  153. else {
  154. parent.appendChild(foreground)
  155. }
  156. if (backgroundNextSibling) {
  157. this.body.dom.backgroundVertical.insertBefore(background, backgroundNextSibling);
  158. }
  159. else {
  160. this.body.dom.backgroundVertical.appendChild(background)
  161. }
  162. return this._isResized() || parentChanged;
  163. };
  164. /**
  165. * Repaint major and minor text labels and vertical grid lines
  166. * @private
  167. */
  168. TimeAxis.prototype._repaintLabels = function () {
  169. var orientation = this.options.orientation.axis;
  170. // calculate range and step (step such that we have space for 7 characters per label)
  171. var start = util.convert(this.body.range.start, 'Number');
  172. var end = util.convert(this.body.range.end, 'Number');
  173. var timeLabelsize = this.body.util.toTime((this.props.minorCharWidth || 10) * this.options.maxMinorChars).valueOf();
  174. var minimumStep = timeLabelsize - DateUtil.getHiddenDurationBefore(this.options.moment, this.body.hiddenDates, this.body.range, timeLabelsize);
  175. minimumStep -= this.body.util.toTime(0).valueOf();
  176. var step = new TimeStep(new Date(start), new Date(end), minimumStep, this.body.hiddenDates);
  177. step.setMoment(this.options.moment);
  178. if (this.options.format) {
  179. step.setFormat(this.options.format);
  180. }
  181. if (this.options.timeAxis) {
  182. step.setScale(this.options.timeAxis);
  183. }
  184. this.step = step;
  185. // Move all DOM elements to a "redundant" list, where they
  186. // can be picked for re-use, and clear the lists with lines and texts.
  187. // At the end of the function _repaintLabels, left over elements will be cleaned up
  188. var dom = this.dom;
  189. dom.redundant.lines = dom.lines;
  190. dom.redundant.majorTexts = dom.majorTexts;
  191. dom.redundant.minorTexts = dom.minorTexts;
  192. dom.lines = [];
  193. dom.majorTexts = [];
  194. dom.minorTexts = [];
  195. var current;
  196. var next;
  197. var x;
  198. var xNext;
  199. var isMajor, nextIsMajor;
  200. var width = 0, prevWidth;
  201. var line;
  202. var labelMinor;
  203. var xFirstMajorLabel = undefined;
  204. var count = 0;
  205. const MAX = 1000;
  206. var className;
  207. step.start();
  208. next = step.getCurrent();
  209. xNext = this.body.util.toScreen(next);
  210. while (step.hasNext() && count < MAX) {
  211. count++;
  212. isMajor = step.isMajor();
  213. className = step.getClassName();
  214. labelMinor = step.getLabelMinor();
  215. current = next;
  216. x = xNext;
  217. step.next();
  218. next = step.getCurrent();
  219. nextIsMajor = step.isMajor();
  220. xNext = this.body.util.toScreen(next);
  221. prevWidth = width;
  222. width = xNext - x;
  223. var showMinorGrid = (width >= prevWidth * 0.4); // prevent displaying of the 31th of the month on a scale of 5 days
  224. if (this.options.showMinorLabels && showMinorGrid) {
  225. var label = this._repaintMinorText(x, labelMinor, orientation, className);
  226. label.style.width = width + 'px'; // set width to prevent overflow
  227. }
  228. if (isMajor && this.options.showMajorLabels) {
  229. if (x > 0) {
  230. if (xFirstMajorLabel == undefined) {
  231. xFirstMajorLabel = x;
  232. }
  233. label = this._repaintMajorText(x, step.getLabelMajor(), orientation, className);
  234. }
  235. line = this._repaintMajorLine(x, width, orientation, className);
  236. }
  237. else { // minor line
  238. if (showMinorGrid) {
  239. line = this._repaintMinorLine(x, width, orientation, className);
  240. }
  241. else {
  242. if (line) {
  243. // adjust the width of the previous grid
  244. line.style.width = (parseInt (line.style.width) + width) + 'px';
  245. }
  246. }
  247. }
  248. }
  249. if (count === MAX && !warnedForOverflow) {
  250. console.warn(`Something is wrong with the Timeline scale. Limited drawing of grid lines to ${MAX} lines.`);
  251. warnedForOverflow = true;
  252. }
  253. // create a major label on the left when needed
  254. if (this.options.showMajorLabels) {
  255. var leftTime = this.body.util.toTime(0),
  256. leftText = step.getLabelMajor(leftTime),
  257. widthText = leftText.length * (this.props.majorCharWidth || 10) + 10; // upper bound estimation
  258. if (xFirstMajorLabel == undefined || widthText < xFirstMajorLabel) {
  259. this._repaintMajorText(0, leftText, orientation, className);
  260. }
  261. }
  262. // Cleanup leftover DOM elements from the redundant list
  263. util.forEach(this.dom.redundant, function (arr) {
  264. while (arr.length) {
  265. var elem = arr.pop();
  266. if (elem && elem.parentNode) {
  267. elem.parentNode.removeChild(elem);
  268. }
  269. }
  270. });
  271. };
  272. /**
  273. * Create a minor label for the axis at position x
  274. * @param {Number} x
  275. * @param {String} text
  276. * @param {String} orientation "top" or "bottom" (default)
  277. * @param {String} className
  278. * @return {Element} Returns the HTML element of the created label
  279. * @private
  280. */
  281. TimeAxis.prototype._repaintMinorText = function (x, text, orientation, className) {
  282. // reuse redundant label
  283. var label = this.dom.redundant.minorTexts.shift();
  284. if (!label) {
  285. // create new label
  286. var content = document.createTextNode('');
  287. label = document.createElement('div');
  288. label.appendChild(content);
  289. this.dom.foreground.appendChild(label);
  290. }
  291. this.dom.minorTexts.push(label);
  292. label.childNodes[0].nodeValue = text;
  293. label.style.top = (orientation == 'top') ? (this.props.majorLabelHeight + 'px') : '0';
  294. label.style.left = x + 'px';
  295. label.className = 'vis-text vis-minor ' + className;
  296. //label.title = title; // TODO: this is a heavy operation
  297. return label;
  298. };
  299. /**
  300. * Create a Major label for the axis at position x
  301. * @param {Number} x
  302. * @param {String} text
  303. * @param {String} orientation "top" or "bottom" (default)
  304. * @param {String} className
  305. * @return {Element} Returns the HTML element of the created label
  306. * @private
  307. */
  308. TimeAxis.prototype._repaintMajorText = function (x, text, orientation, className) {
  309. // reuse redundant label
  310. var label = this.dom.redundant.majorTexts.shift();
  311. if (!label) {
  312. // create label
  313. var content = document.createTextNode(text);
  314. label = document.createElement('div');
  315. label.appendChild(content);
  316. this.dom.foreground.appendChild(label);
  317. }
  318. this.dom.majorTexts.push(label);
  319. label.childNodes[0].nodeValue = text;
  320. label.className = 'vis-text vis-major ' + className;
  321. //label.title = title; // TODO: this is a heavy operation
  322. label.style.top = (orientation == 'top') ? '0' : (this.props.minorLabelHeight + 'px');
  323. label.style.left = x + 'px';
  324. return label;
  325. };
  326. /**
  327. * Create a minor line for the axis at position x
  328. * @param {Number} x
  329. * @param {Number} width
  330. * @param {String} orientation "top" or "bottom" (default)
  331. * @param {String} className
  332. * @return {Element} Returns the created line
  333. * @private
  334. */
  335. TimeAxis.prototype._repaintMinorLine = function (x, width, orientation, className) {
  336. // reuse redundant line
  337. var line = this.dom.redundant.lines.shift();
  338. if (!line) {
  339. // create vertical line
  340. line = document.createElement('div');
  341. this.dom.background.appendChild(line);
  342. }
  343. this.dom.lines.push(line);
  344. var props = this.props;
  345. if (orientation == 'top') {
  346. line.style.top = props.majorLabelHeight + 'px';
  347. }
  348. else {
  349. line.style.top = this.body.domProps.top.height + 'px';
  350. }
  351. line.style.height = props.minorLineHeight + 'px';
  352. line.style.left = (x - props.minorLineWidth / 2) + 'px';
  353. line.style.width = width + 'px';
  354. line.className = 'vis-grid vis-vertical vis-minor ' + className;
  355. return line;
  356. };
  357. /**
  358. * Create a Major line for the axis at position x
  359. * @param {Number} x
  360. * @param {Number} width
  361. * @param {String} orientation "top" or "bottom" (default)
  362. * @param {String} className
  363. * @return {Element} Returns the created line
  364. * @private
  365. */
  366. TimeAxis.prototype._repaintMajorLine = function (x, width, orientation, className) {
  367. // reuse redundant line
  368. var line = this.dom.redundant.lines.shift();
  369. if (!line) {
  370. // create vertical line
  371. line = document.createElement('div');
  372. this.dom.background.appendChild(line);
  373. }
  374. this.dom.lines.push(line);
  375. var props = this.props;
  376. if (orientation == 'top') {
  377. line.style.top = '0';
  378. }
  379. else {
  380. line.style.top = this.body.domProps.top.height + 'px';
  381. }
  382. line.style.left = (x - props.majorLineWidth / 2) + 'px';
  383. line.style.height = props.majorLineHeight + 'px';
  384. line.style.width = width + 'px';
  385. line.className = 'vis-grid vis-vertical vis-major ' + className;
  386. return line;
  387. };
  388. /**
  389. * Determine the size of text on the axis (both major and minor axis).
  390. * The size is calculated only once and then cached in this.props.
  391. * @private
  392. */
  393. TimeAxis.prototype._calculateCharSize = function () {
  394. // Note: We calculate char size with every redraw. Size may change, for
  395. // example when any of the timelines parents had display:none for example.
  396. // determine the char width and height on the minor axis
  397. if (!this.dom.measureCharMinor) {
  398. this.dom.measureCharMinor = document.createElement('DIV');
  399. this.dom.measureCharMinor.className = 'vis-text vis-minor vis-measure';
  400. this.dom.measureCharMinor.style.position = 'absolute';
  401. this.dom.measureCharMinor.appendChild(document.createTextNode('0'));
  402. this.dom.foreground.appendChild(this.dom.measureCharMinor);
  403. }
  404. this.props.minorCharHeight = this.dom.measureCharMinor.clientHeight;
  405. this.props.minorCharWidth = this.dom.measureCharMinor.clientWidth;
  406. // determine the char width and height on the major axis
  407. if (!this.dom.measureCharMajor) {
  408. this.dom.measureCharMajor = document.createElement('DIV');
  409. this.dom.measureCharMajor.className = 'vis-text vis-major vis-measure';
  410. this.dom.measureCharMajor.style.position = 'absolute';
  411. this.dom.measureCharMajor.appendChild(document.createTextNode('0'));
  412. this.dom.foreground.appendChild(this.dom.measureCharMajor);
  413. }
  414. this.props.majorCharHeight = this.dom.measureCharMajor.clientHeight;
  415. this.props.majorCharWidth = this.dom.measureCharMajor.clientWidth;
  416. };
  417. var warnedForOverflow = false;
  418. module.exports = TimeAxis;