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.

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