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.

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