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.

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