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.

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