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.

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