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.

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