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.

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