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.

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