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.

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