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.

446 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 options = this.options;
  118. var props = this.props;
  119. var foreground = this.dom.foreground;
  120. var background = this.dom.background;
  121. // determine the correct parent DOM element (depending on option orientation)
  122. var parent = (options.orientation.axis == 'top') ? this.body.dom.top : this.body.dom.bottom;
  123. var parentChanged = (foreground.parentNode !== parent);
  124. // calculate character width and height
  125. this._calculateCharSize();
  126. // TODO: recalculate sizes only needed when parent is resized or options is changed
  127. var showMinorLabels = this.options.showMinorLabels;
  128. var showMajorLabels = this.options.showMajorLabels;
  129. // determine the width and height of the elemens for the axis
  130. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  131. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  132. props.height = props.minorLabelHeight + props.majorLabelHeight;
  133. props.width = foreground.offsetWidth;
  134. props.minorLineHeight = this.body.domProps.root.height - props.majorLabelHeight -
  135. (options.orientation.axis == 'top' ? this.body.domProps.bottom.height : this.body.domProps.top.height);
  136. props.minorLineWidth = 1; // TODO: really calculate width
  137. props.majorLineHeight = props.minorLineHeight + props.majorLabelHeight;
  138. props.majorLineWidth = 1; // TODO: really calculate width
  139. // take foreground and background offline while updating (is almost twice as fast)
  140. var foregroundNextSibling = foreground.nextSibling;
  141. var backgroundNextSibling = background.nextSibling;
  142. foreground.parentNode && foreground.parentNode.removeChild(foreground);
  143. background.parentNode && background.parentNode.removeChild(background);
  144. foreground.style.height = this.props.height + 'px';
  145. this._repaintLabels();
  146. // put DOM online again (at the same place)
  147. if (foregroundNextSibling) {
  148. parent.insertBefore(foreground, foregroundNextSibling);
  149. }
  150. else {
  151. parent.appendChild(foreground)
  152. }
  153. if (backgroundNextSibling) {
  154. this.body.dom.backgroundVertical.insertBefore(background, backgroundNextSibling);
  155. }
  156. else {
  157. this.body.dom.backgroundVertical.appendChild(background)
  158. }
  159. return this._isResized() || parentChanged;
  160. };
  161. /**
  162. * Repaint major and minor text labels and vertical grid lines
  163. * @private
  164. */
  165. TimeAxis.prototype._repaintLabels = function () {
  166. var orientation = this.options.orientation.axis;
  167. // calculate range and step (step such that we have space for 7 characters per label)
  168. var start = util.convert(this.body.range.start, 'Number');
  169. var end = util.convert(this.body.range.end, 'Number');
  170. var timeLabelsize = this.body.util.toTime((this.props.minorCharWidth || 10) * 7).valueOf();
  171. var minimumStep = timeLabelsize - DateUtil.getHiddenDurationBefore(this.body.hiddenDates, this.body.range, timeLabelsize);
  172. minimumStep -= this.body.util.toTime(0).valueOf();
  173. var step = new TimeStep(new Date(start), new Date(end), minimumStep, this.body.hiddenDates);
  174. if (this.options.format) {
  175. step.setFormat(this.options.format);
  176. }
  177. if (this.options.timeAxis) {
  178. step.setScale(this.options.timeAxis);
  179. }
  180. this.step = step;
  181. // Move all DOM elements to a "redundant" list, where they
  182. // can be picked for re-use, and clear the lists with lines and texts.
  183. // At the end of the function _repaintLabels, left over elements will be cleaned up
  184. var dom = this.dom;
  185. dom.redundant.lines = dom.lines;
  186. dom.redundant.majorTexts = dom.majorTexts;
  187. dom.redundant.minorTexts = dom.minorTexts;
  188. dom.lines = [];
  189. dom.majorTexts = [];
  190. dom.minorTexts = [];
  191. var cur;
  192. var x = 0;
  193. var isMajor;
  194. var xPrev = 0;
  195. var width = 0;
  196. var prevLine;
  197. var xFirstMajorLabel = undefined;
  198. var max = 0;
  199. var className;
  200. step.first();
  201. while (step.hasNext() && max < 1000) {
  202. max++;
  203. cur = step.getCurrent();
  204. isMajor = step.isMajor();
  205. className = step.getClassName();
  206. xPrev = x;
  207. x = this.body.util.toScreen(cur);
  208. width = x - xPrev;
  209. if (prevLine) {
  210. prevLine.style.width = width + 'px';
  211. }
  212. if (this.options.showMinorLabels) {
  213. this._repaintMinorText(x, step.getLabelMinor(), orientation, className);
  214. }
  215. if (isMajor && this.options.showMajorLabels) {
  216. if (x > 0) {
  217. if (xFirstMajorLabel == undefined) {
  218. xFirstMajorLabel = x;
  219. }
  220. this._repaintMajorText(x, step.getLabelMajor(), orientation, className);
  221. }
  222. prevLine = this._repaintMajorLine(x, orientation, className);
  223. }
  224. else {
  225. prevLine = this._repaintMinorLine(x, orientation, className);
  226. }
  227. step.next();
  228. }
  229. // create a major label on the left when needed
  230. if (this.options.showMajorLabels) {
  231. var leftTime = this.body.util.toTime(0),
  232. leftText = step.getLabelMajor(leftTime),
  233. widthText = leftText.length * (this.props.majorCharWidth || 10) + 10; // upper bound estimation
  234. if (xFirstMajorLabel == undefined || widthText < xFirstMajorLabel) {
  235. this._repaintMajorText(0, leftText, orientation, className);
  236. }
  237. }
  238. // Cleanup leftover DOM elements from the redundant list
  239. util.forEach(this.dom.redundant, function (arr) {
  240. while (arr.length) {
  241. var elem = arr.pop();
  242. if (elem && elem.parentNode) {
  243. elem.parentNode.removeChild(elem);
  244. }
  245. }
  246. });
  247. };
  248. /**
  249. * Create a minor label for the axis at position x
  250. * @param {Number} x
  251. * @param {String} text
  252. * @param {String} orientation "top" or "bottom" (default)
  253. * @param {String} className
  254. * @private
  255. */
  256. TimeAxis.prototype._repaintMinorText = function (x, text, orientation, className) {
  257. // reuse redundant label
  258. var label = this.dom.redundant.minorTexts.shift();
  259. if (!label) {
  260. // create new label
  261. var content = document.createTextNode('');
  262. label = document.createElement('div');
  263. label.appendChild(content);
  264. this.dom.foreground.appendChild(label);
  265. }
  266. this.dom.minorTexts.push(label);
  267. label.childNodes[0].nodeValue = text;
  268. label.style.top = (orientation == 'top') ? (this.props.majorLabelHeight + 'px') : '0';
  269. label.style.left = x + 'px';
  270. label.className = 'vis-text vis-minor ' + className;
  271. //label.title = title; // TODO: this is a heavy operation
  272. };
  273. /**
  274. * Create a Major label for the axis at position x
  275. * @param {Number} x
  276. * @param {String} text
  277. * @param {String} orientation "top" or "bottom" (default)
  278. * @param {String} className
  279. * @private
  280. */
  281. TimeAxis.prototype._repaintMajorText = function (x, text, orientation, className) {
  282. // reuse redundant label
  283. var label = this.dom.redundant.majorTexts.shift();
  284. if (!label) {
  285. // create label
  286. var content = document.createTextNode(text);
  287. label = document.createElement('div');
  288. label.appendChild(content);
  289. this.dom.foreground.appendChild(label);
  290. }
  291. this.dom.majorTexts.push(label);
  292. label.childNodes[0].nodeValue = text;
  293. label.className = 'vis-text vis-major ' + className;
  294. //label.title = title; // TODO: this is a heavy operation
  295. label.style.top = (orientation == 'top') ? '0' : (this.props.minorLabelHeight + 'px');
  296. label.style.left = x + 'px';
  297. };
  298. /**
  299. * Create a minor line for the axis at position x
  300. * @param {Number} x
  301. * @param {String} orientation "top" or "bottom" (default)
  302. * @param {String} className
  303. * @return {Element} Returns the created line
  304. * @private
  305. */
  306. TimeAxis.prototype._repaintMinorLine = function (x, orientation, className) {
  307. // reuse redundant line
  308. var line = this.dom.redundant.lines.shift();
  309. if (!line) {
  310. // create vertical line
  311. line = document.createElement('div');
  312. this.dom.background.appendChild(line);
  313. }
  314. this.dom.lines.push(line);
  315. var props = this.props;
  316. if (orientation == 'top') {
  317. line.style.top = props.majorLabelHeight + 'px';
  318. }
  319. else {
  320. line.style.top = this.body.domProps.top.height + 'px';
  321. }
  322. line.style.height = props.minorLineHeight + 'px';
  323. line.style.left = (x - props.minorLineWidth / 2) + 'px';
  324. line.className = 'vis-grid vis-vertical vis-minor ' + className;
  325. return line;
  326. };
  327. /**
  328. * Create a Major line for the axis at position x
  329. * @param {Number} x
  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._repaintMajorLine = function (x, 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 = '0';
  347. }
  348. else {
  349. line.style.top = this.body.domProps.top.height + 'px';
  350. }
  351. line.style.left = (x - props.majorLineWidth / 2) + 'px';
  352. line.style.height = props.majorLineHeight + 'px';
  353. line.className = 'vis-grid vis-vertical vis-major ' + className;
  354. return line;
  355. };
  356. /**
  357. * Determine the size of text on the axis (both major and minor axis).
  358. * The size is calculated only once and then cached in this.props.
  359. * @private
  360. */
  361. TimeAxis.prototype._calculateCharSize = function () {
  362. // Note: We calculate char size with every redraw. Size may change, for
  363. // example when any of the timelines parents had display:none for example.
  364. // determine the char width and height on the minor axis
  365. if (!this.dom.measureCharMinor) {
  366. this.dom.measureCharMinor = document.createElement('DIV');
  367. this.dom.measureCharMinor.className = 'vis-text vis-minor vis-measure';
  368. this.dom.measureCharMinor.style.position = 'absolute';
  369. this.dom.measureCharMinor.appendChild(document.createTextNode('0'));
  370. this.dom.foreground.appendChild(this.dom.measureCharMinor);
  371. }
  372. this.props.minorCharHeight = this.dom.measureCharMinor.clientHeight;
  373. this.props.minorCharWidth = this.dom.measureCharMinor.clientWidth;
  374. // determine the char width and height on the major axis
  375. if (!this.dom.measureCharMajor) {
  376. this.dom.measureCharMajor = document.createElement('DIV');
  377. this.dom.measureCharMajor.className = 'vis-text vis-major vis-measure';
  378. this.dom.measureCharMajor.style.position = 'absolute';
  379. this.dom.measureCharMajor.appendChild(document.createTextNode('0'));
  380. this.dom.foreground.appendChild(this.dom.measureCharMajor);
  381. }
  382. this.props.majorCharHeight = this.dom.measureCharMajor.clientHeight;
  383. this.props.majorCharWidth = this.dom.measureCharMajor.clientWidth;
  384. };
  385. module.exports = TimeAxis;