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.

422 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. majorLines: [],
  18. majorTexts: [],
  19. minorLines: [],
  20. minorTexts: [],
  21. redundant: {
  22. majorLines: [],
  23. majorTexts: [],
  24. minorLines: [],
  25. minorTexts: []
  26. }
  27. };
  28. this.props = {
  29. range: {
  30. start: 0,
  31. end: 0,
  32. minimumStep: 0
  33. },
  34. lineTop: 0
  35. };
  36. this.defaultOptions = {
  37. orientation: 'bottom', // supported: 'top', 'bottom'
  38. // TODO: implement timeaxis orientations 'left' and 'right'
  39. showMinorLabels: true,
  40. showMajorLabels: 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([
  62. 'orientation',
  63. 'showMinorLabels',
  64. 'showMajorLabels',
  65. 'hiddenDates',
  66. 'format'
  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. this.step = step;
  170. // Move all DOM elements to a "redundant" list, where they
  171. // can be picked for re-use, and clear the lists with lines and texts.
  172. // At the end of the function _repaintLabels, left over elements will be cleaned up
  173. var dom = this.dom;
  174. dom.redundant.majorLines = dom.majorLines;
  175. dom.redundant.majorTexts = dom.majorTexts;
  176. dom.redundant.minorLines = dom.minorLines;
  177. dom.redundant.minorTexts = dom.minorTexts;
  178. dom.majorLines = [];
  179. dom.majorTexts = [];
  180. dom.minorLines = [];
  181. dom.minorTexts = [];
  182. step.first();
  183. var xFirstMajorLabel = undefined;
  184. var max = 0;
  185. while (step.hasNext() && max < 1000) {
  186. max++;
  187. var cur = step.getCurrent();
  188. var x = this.body.util.toScreen(cur);
  189. var isMajor = step.isMajor();
  190. // TODO: lines must have a width, such that we can create css backgrounds
  191. if (this.options.showMinorLabels) {
  192. this._repaintMinorText(x, step.getLabelMinor(), orientation);
  193. }
  194. if (isMajor && this.options.showMajorLabels) {
  195. if (x > 0) {
  196. if (xFirstMajorLabel == undefined) {
  197. xFirstMajorLabel = x;
  198. }
  199. this._repaintMajorText(x, step.getLabelMajor(), orientation);
  200. }
  201. this._repaintMajorLine(x, orientation);
  202. }
  203. else {
  204. this._repaintMinorLine(x, orientation);
  205. }
  206. step.next();
  207. }
  208. // create a major label on the left when needed
  209. if (this.options.showMajorLabels) {
  210. var leftTime = this.body.util.toTime(0),
  211. leftText = step.getLabelMajor(leftTime),
  212. widthText = leftText.length * (this.props.majorCharWidth || 10) + 10; // upper bound estimation
  213. if (xFirstMajorLabel == undefined || widthText < xFirstMajorLabel) {
  214. this._repaintMajorText(0, leftText, orientation);
  215. }
  216. }
  217. // Cleanup leftover DOM elements from the redundant list
  218. util.forEach(this.dom.redundant, function (arr) {
  219. while (arr.length) {
  220. var elem = arr.pop();
  221. if (elem && elem.parentNode) {
  222. elem.parentNode.removeChild(elem);
  223. }
  224. }
  225. });
  226. };
  227. /**
  228. * Create a minor label for the axis at position x
  229. * @param {Number} x
  230. * @param {String} text
  231. * @param {String} orientation "top" or "bottom" (default)
  232. * @private
  233. */
  234. TimeAxis.prototype._repaintMinorText = function (x, text, orientation) {
  235. // reuse redundant label
  236. var label = this.dom.redundant.minorTexts.shift();
  237. if (!label) {
  238. // create new label
  239. var content = document.createTextNode('');
  240. label = document.createElement('div');
  241. label.appendChild(content);
  242. label.className = 'text minor';
  243. this.dom.foreground.appendChild(label);
  244. }
  245. this.dom.minorTexts.push(label);
  246. label.childNodes[0].nodeValue = text;
  247. label.style.top = (orientation == 'top') ? (this.props.majorLabelHeight + 'px') : '0';
  248. label.style.left = x + 'px';
  249. //label.title = title; // TODO: this is a heavy operation
  250. };
  251. /**
  252. * Create a Major label for the axis at position x
  253. * @param {Number} x
  254. * @param {String} text
  255. * @param {String} orientation "top" or "bottom" (default)
  256. * @private
  257. */
  258. TimeAxis.prototype._repaintMajorText = function (x, text, orientation) {
  259. // reuse redundant label
  260. var label = this.dom.redundant.majorTexts.shift();
  261. if (!label) {
  262. // create label
  263. var content = document.createTextNode(text);
  264. label = document.createElement('div');
  265. label.className = 'text major';
  266. label.appendChild(content);
  267. this.dom.foreground.appendChild(label);
  268. }
  269. this.dom.majorTexts.push(label);
  270. label.childNodes[0].nodeValue = text;
  271. //label.title = title; // TODO: this is a heavy operation
  272. label.style.top = (orientation == 'top') ? '0' : (this.props.minorLabelHeight + 'px');
  273. label.style.left = x + 'px';
  274. };
  275. /**
  276. * Create a minor line for the axis at position x
  277. * @param {Number} x
  278. * @param {String} orientation "top" or "bottom" (default)
  279. * @private
  280. */
  281. TimeAxis.prototype._repaintMinorLine = function (x, orientation) {
  282. // reuse redundant line
  283. var line = this.dom.redundant.minorLines.shift();
  284. if (!line) {
  285. // create vertical line
  286. line = document.createElement('div');
  287. line.className = 'grid vertical minor';
  288. this.dom.background.appendChild(line);
  289. }
  290. this.dom.minorLines.push(line);
  291. var props = this.props;
  292. if (orientation == 'top') {
  293. line.style.top = props.majorLabelHeight + 'px';
  294. }
  295. else {
  296. line.style.top = this.body.domProps.top.height + 'px';
  297. }
  298. line.style.height = props.minorLineHeight + 'px';
  299. line.style.left = (x - props.minorLineWidth / 2) + 'px';
  300. };
  301. /**
  302. * Create a Major line for the axis at position x
  303. * @param {Number} x
  304. * @param {String} orientation "top" or "bottom" (default)
  305. * @private
  306. */
  307. TimeAxis.prototype._repaintMajorLine = function (x, orientation) {
  308. // reuse redundant line
  309. var line = this.dom.redundant.majorLines.shift();
  310. if (!line) {
  311. // create vertical line
  312. line = document.createElement('DIV');
  313. line.className = 'grid vertical major';
  314. this.dom.background.appendChild(line);
  315. }
  316. this.dom.majorLines.push(line);
  317. var props = this.props;
  318. if (orientation == 'top') {
  319. line.style.top = '0';
  320. }
  321. else {
  322. line.style.top = this.body.domProps.top.height + 'px';
  323. }
  324. line.style.left = (x - props.majorLineWidth / 2) + 'px';
  325. line.style.height = props.majorLineHeight + 'px';
  326. };
  327. /**
  328. * Determine the size of text on the axis (both major and minor axis).
  329. * The size is calculated only once and then cached in this.props.
  330. * @private
  331. */
  332. TimeAxis.prototype._calculateCharSize = function () {
  333. // Note: We calculate char size with every redraw. Size may change, for
  334. // example when any of the timelines parents had display:none for example.
  335. // determine the char width and height on the minor axis
  336. if (!this.dom.measureCharMinor) {
  337. this.dom.measureCharMinor = document.createElement('DIV');
  338. this.dom.measureCharMinor.className = 'text minor measure';
  339. this.dom.measureCharMinor.style.position = 'absolute';
  340. this.dom.measureCharMinor.appendChild(document.createTextNode('0'));
  341. this.dom.foreground.appendChild(this.dom.measureCharMinor);
  342. }
  343. this.props.minorCharHeight = this.dom.measureCharMinor.clientHeight;
  344. this.props.minorCharWidth = this.dom.measureCharMinor.clientWidth;
  345. // determine the char width and height on the major axis
  346. if (!this.dom.measureCharMajor) {
  347. this.dom.measureCharMajor = document.createElement('DIV');
  348. this.dom.measureCharMajor.className = 'text major measure';
  349. this.dom.measureCharMajor.style.position = 'absolute';
  350. this.dom.measureCharMajor.appendChild(document.createTextNode('0'));
  351. this.dom.foreground.appendChild(this.dom.measureCharMajor);
  352. }
  353. this.props.majorCharHeight = this.dom.measureCharMajor.clientHeight;
  354. this.props.majorCharWidth = this.dom.measureCharMajor.clientWidth;
  355. };
  356. /**
  357. * Snap a date to a rounded value.
  358. * The snap intervals are dependent on the current scale and step.
  359. * @param {Date} date the date to be snapped.
  360. * @return {Date} snappedDate
  361. */
  362. TimeAxis.prototype.snap = function(date) {
  363. return this.step.snap(date);
  364. };
  365. module.exports = TimeAxis;