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.

595 lines
18 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. var Component = require('./Component');
  4. var DataStep = require('../DataStep');
  5. /**
  6. * A horizontal time axis
  7. * @param {Object} [options] See DataAxis.setOptions for the available
  8. * options.
  9. * @constructor DataAxis
  10. * @extends Component
  11. * @param body
  12. */
  13. function DataAxis (body, options, svg, linegraphOptions) {
  14. this.id = util.randomUUID();
  15. this.body = body;
  16. this.defaultOptions = {
  17. orientation: 'left', // supported: 'left', 'right'
  18. showMinorLabels: true,
  19. showMajorLabels: true,
  20. icons: true,
  21. majorLinesOffset: 7,
  22. minorLinesOffset: 4,
  23. labelOffsetX: 10,
  24. labelOffsetY: 2,
  25. iconWidth: 20,
  26. width: '40px',
  27. visible: true,
  28. customRange: {
  29. left: {min:undefined, max:undefined},
  30. right: {min:undefined, max:undefined}
  31. },
  32. title: {
  33. left: {text:undefined},
  34. right: {text:undefined}
  35. },
  36. format: {
  37. left: {decimals: undefined},
  38. right: {decimals: undefined}
  39. }
  40. };
  41. this.linegraphOptions = linegraphOptions;
  42. this.linegraphSVG = svg;
  43. this.props = {};
  44. this.DOMelements = { // dynamic elements
  45. lines: {},
  46. labels: {},
  47. title: {}
  48. };
  49. this.dom = {};
  50. this.range = {start:0, end:0};
  51. this.options = util.extend({}, this.defaultOptions);
  52. this.conversionFactor = 1;
  53. this.setOptions(options);
  54. this.width = Number(('' + this.options.width).replace("px",""));
  55. this.minWidth = this.width;
  56. this.height = this.linegraphSVG.offsetHeight;
  57. this.stepPixels = 25;
  58. this.stepPixelsForced = 25;
  59. this.lineOffset = 0;
  60. this.master = true;
  61. this.svgElements = {};
  62. this.iconsRemoved = false;
  63. this.groups = {};
  64. this.amountOfGroups = 0;
  65. // create the HTML DOM
  66. this._create();
  67. var me = this;
  68. this.body.emitter.on("verticalDrag", function() {
  69. me.dom.lineContainer.style.top = me.body.domProps.scrollTop + 'px';
  70. });
  71. }
  72. DataAxis.prototype = new Component();
  73. DataAxis.prototype.addGroup = function(label, graphOptions) {
  74. if (!this.groups.hasOwnProperty(label)) {
  75. this.groups[label] = graphOptions;
  76. }
  77. this.amountOfGroups += 1;
  78. };
  79. DataAxis.prototype.updateGroup = function(label, graphOptions) {
  80. this.groups[label] = graphOptions;
  81. };
  82. DataAxis.prototype.removeGroup = function(label) {
  83. if (this.groups.hasOwnProperty(label)) {
  84. delete this.groups[label];
  85. this.amountOfGroups -= 1;
  86. }
  87. };
  88. DataAxis.prototype.setOptions = function (options) {
  89. if (options) {
  90. var redraw = false;
  91. if (this.options.orientation != options.orientation && options.orientation !== undefined) {
  92. redraw = true;
  93. }
  94. var fields = [
  95. 'orientation',
  96. 'showMinorLabels',
  97. 'showMajorLabels',
  98. 'icons',
  99. 'majorLinesOffset',
  100. 'minorLinesOffset',
  101. 'labelOffsetX',
  102. 'labelOffsetY',
  103. 'iconWidth',
  104. 'width',
  105. 'visible',
  106. 'customRange',
  107. 'title',
  108. 'format'
  109. ];
  110. util.selectiveExtend(fields, this.options, options);
  111. this.minWidth = Number(('' + this.options.width).replace("px",""));
  112. if (redraw == true && this.dom.frame) {
  113. this.hide();
  114. this.show();
  115. }
  116. }
  117. };
  118. /**
  119. * Create the HTML DOM for the DataAxis
  120. */
  121. DataAxis.prototype._create = function() {
  122. this.dom.frame = document.createElement('div');
  123. this.dom.frame.style.width = this.options.width;
  124. this.dom.frame.style.height = this.height;
  125. this.dom.lineContainer = document.createElement('div');
  126. this.dom.lineContainer.style.width = '100%';
  127. this.dom.lineContainer.style.height = this.height;
  128. this.dom.lineContainer.style.position = 'relative';
  129. // create svg element for graph drawing.
  130. this.svg = document.createElementNS('http://www.w3.org/2000/svg',"svg");
  131. this.svg.style.position = "absolute";
  132. this.svg.style.top = '0px';
  133. this.svg.style.height = '100%';
  134. this.svg.style.width = '100%';
  135. this.svg.style.display = "block";
  136. this.dom.frame.appendChild(this.svg);
  137. };
  138. DataAxis.prototype._redrawGroupIcons = function () {
  139. DOMutil.prepareElements(this.svgElements);
  140. var x;
  141. var iconWidth = this.options.iconWidth;
  142. var iconHeight = 15;
  143. var iconOffset = 4;
  144. var y = iconOffset + 0.5 * iconHeight;
  145. if (this.options.orientation == 'left') {
  146. x = iconOffset;
  147. }
  148. else {
  149. x = this.width - iconWidth - iconOffset;
  150. }
  151. for (var groupId in this.groups) {
  152. if (this.groups.hasOwnProperty(groupId)) {
  153. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  154. this.groups[groupId].drawIcon(x, y, this.svgElements, this.svg, iconWidth, iconHeight);
  155. y += iconHeight + iconOffset;
  156. }
  157. }
  158. }
  159. DOMutil.cleanupElements(this.svgElements);
  160. this.iconsRemoved = false;
  161. };
  162. DataAxis.prototype._cleanupIcons = function() {
  163. if (this.iconsRemoved == false) {
  164. DOMutil.prepareElements(this.svgElements);
  165. DOMutil.cleanupElements(this.svgElements);
  166. this.iconsRemoved = true;
  167. }
  168. }
  169. /**
  170. * Create the HTML DOM for the DataAxis
  171. */
  172. DataAxis.prototype.show = function() {
  173. if (!this.dom.frame.parentNode) {
  174. if (this.options.orientation == 'left') {
  175. this.body.dom.left.appendChild(this.dom.frame);
  176. }
  177. else {
  178. this.body.dom.right.appendChild(this.dom.frame);
  179. }
  180. }
  181. if (!this.dom.lineContainer.parentNode) {
  182. this.body.dom.backgroundHorizontal.appendChild(this.dom.lineContainer);
  183. }
  184. };
  185. /**
  186. * Create the HTML DOM for the DataAxis
  187. */
  188. DataAxis.prototype.hide = function() {
  189. if (this.dom.frame.parentNode) {
  190. this.dom.frame.parentNode.removeChild(this.dom.frame);
  191. }
  192. if (this.dom.lineContainer.parentNode) {
  193. this.dom.lineContainer.parentNode.removeChild(this.dom.lineContainer);
  194. }
  195. };
  196. /**
  197. * Set a range (start and end)
  198. * @param end
  199. * @param start
  200. * @param end
  201. */
  202. DataAxis.prototype.setRange = function (start, end) {
  203. this.range.start = start;
  204. this.range.end = end;
  205. };
  206. /**
  207. * Repaint the component
  208. * @return {boolean} Returns true if the component is resized
  209. */
  210. DataAxis.prototype.redraw = function () {
  211. var changeCalled = false;
  212. var activeGroups = 0;
  213. // Make sure the line container adheres to the vertical scrolling.
  214. this.dom.lineContainer.style.top = this.body.domProps.scrollTop + 'px';
  215. for (var groupId in this.groups) {
  216. if (this.groups.hasOwnProperty(groupId)) {
  217. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  218. activeGroups++;
  219. }
  220. }
  221. }
  222. if (this.amountOfGroups == 0 || activeGroups == 0) {
  223. this.hide();
  224. }
  225. else {
  226. this.show();
  227. this.height = Number(this.linegraphSVG.style.height.replace("px",""));
  228. // svg offsetheight did not work in firefox and explorer...
  229. this.dom.lineContainer.style.height = this.height + 'px';
  230. this.width = this.options.visible == true ? Number(('' + this.options.width).replace("px","")) : 0;
  231. var props = this.props;
  232. var frame = this.dom.frame;
  233. // update classname
  234. frame.className = 'dataaxis';
  235. // calculate character width and height
  236. this._calculateCharSize();
  237. var orientation = this.options.orientation;
  238. var showMinorLabels = this.options.showMinorLabels;
  239. var showMajorLabels = this.options.showMajorLabels;
  240. // determine the width and height of the elements for the axis
  241. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  242. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  243. props.minorLineWidth = this.body.dom.backgroundHorizontal.offsetWidth - this.lineOffset - this.width + 2 * this.options.minorLinesOffset;
  244. props.minorLineHeight = 1;
  245. props.majorLineWidth = this.body.dom.backgroundHorizontal.offsetWidth - this.lineOffset - this.width + 2 * this.options.majorLinesOffset;
  246. props.majorLineHeight = 1;
  247. // take frame offline while updating (is almost twice as fast)
  248. if (orientation == 'left') {
  249. frame.style.top = '0';
  250. frame.style.left = '0';
  251. frame.style.bottom = '';
  252. frame.style.width = this.width + 'px';
  253. frame.style.height = this.height + "px";
  254. }
  255. else { // right
  256. frame.style.top = '';
  257. frame.style.bottom = '0';
  258. frame.style.left = '0';
  259. frame.style.width = this.width + 'px';
  260. frame.style.height = this.height + "px";
  261. }
  262. changeCalled = this._redrawLabels();
  263. if (this.options.icons == true) {
  264. this._redrawGroupIcons();
  265. }
  266. else {
  267. this._cleanupIcons();
  268. }
  269. this._redrawTitle(orientation);
  270. }
  271. return changeCalled;
  272. };
  273. /**
  274. * Repaint major and minor text labels and vertical grid lines
  275. * @private
  276. */
  277. DataAxis.prototype._redrawLabels = function () {
  278. DOMutil.prepareElements(this.DOMelements.lines);
  279. DOMutil.prepareElements(this.DOMelements.labels);
  280. var orientation = this.options['orientation'];
  281. // calculate range and step (step such that we have space for 7 characters per label)
  282. var minimumStep = this.master ? this.props.majorCharHeight || 10 : this.stepPixelsForced;
  283. var step = new DataStep(this.range.start, this.range.end, minimumStep, this.dom.frame.offsetHeight, this.options.customRange[this.options.orientation]);
  284. this.step = step;
  285. // get the distance in pixels for a step
  286. // dead space is space that is "left over" after a step
  287. var stepPixels = (this.dom.frame.offsetHeight - (step.deadSpace * (this.dom.frame.offsetHeight / step.marginRange))) / (((step.marginRange - step.deadSpace) / step.step));
  288. this.stepPixels = stepPixels;
  289. var amountOfSteps = this.height / stepPixels;
  290. var stepDifference = 0;
  291. if (this.master == false) {
  292. stepPixels = this.stepPixelsForced;
  293. stepDifference = Math.round((this.dom.frame.offsetHeight / stepPixels) - amountOfSteps);
  294. for (var i = 0; i < 0.5 * stepDifference; i++) {
  295. step.previous();
  296. }
  297. amountOfSteps = this.height / stepPixels;
  298. }
  299. else {
  300. amountOfSteps += 0.25;
  301. }
  302. this.valueAtZero = step.marginEnd;
  303. var marginStartPos = 0;
  304. // do not draw the first label
  305. var max = 1;
  306. // Get the number of decimal places
  307. var decimals;
  308. if(this.options.format[orientation] !== undefined) {
  309. decimals = this.options.format[orientation].decimals;
  310. }
  311. this.maxLabelSize = 0;
  312. var y = 0;
  313. while (max < Math.round(amountOfSteps)) {
  314. step.next();
  315. y = Math.round(max * stepPixels);
  316. marginStartPos = max * stepPixels;
  317. var isMajor = step.isMajor();
  318. if (this.options['showMinorLabels'] && isMajor == false || this.master == false && this.options['showMinorLabels'] == true) {
  319. this._redrawLabel(y - 2, step.getCurrent(decimals), orientation, 'yAxis minor', this.props.minorCharHeight);
  320. }
  321. if (isMajor && this.options['showMajorLabels'] && this.master == true ||
  322. this.options['showMinorLabels'] == false && this.master == false && isMajor == true) {
  323. if (y >= 0) {
  324. this._redrawLabel(y - 2, step.getCurrent(decimals), orientation, 'yAxis major', this.props.majorCharHeight);
  325. }
  326. this._redrawLine(y, orientation, 'grid horizontal major', this.options.majorLinesOffset, this.props.majorLineWidth);
  327. }
  328. else {
  329. this._redrawLine(y, orientation, 'grid horizontal minor', this.options.minorLinesOffset, this.props.minorLineWidth);
  330. }
  331. max++;
  332. }
  333. if (this.master == false) {
  334. this.conversionFactor = y / (this.valueAtZero - step.current);
  335. }
  336. else {
  337. this.conversionFactor = this.dom.frame.offsetHeight / step.marginRange;
  338. }
  339. // Note that title is rotated, so we're using the height, not width!
  340. var titleWidth = 0;
  341. if (this.options.title[orientation] !== undefined && this.options.title[orientation].text !== undefined) {
  342. titleWidth = this.props.titleCharHeight;
  343. }
  344. var offset = this.options.icons == true ? Math.max(this.options.iconWidth, titleWidth) + this.options.labelOffsetX + 15 : titleWidth + this.options.labelOffsetX + 15;
  345. // this will resize the yAxis to accommodate the labels.
  346. if (this.maxLabelSize > (this.width - offset) && this.options.visible == true) {
  347. this.width = this.maxLabelSize + offset;
  348. this.options.width = this.width + "px";
  349. DOMutil.cleanupElements(this.DOMelements.lines);
  350. DOMutil.cleanupElements(this.DOMelements.labels);
  351. this.redraw();
  352. return true;
  353. }
  354. // this will resize the yAxis if it is too big for the labels.
  355. else if (this.maxLabelSize < (this.width - offset) && this.options.visible == true && this.width > this.minWidth) {
  356. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  357. this.options.width = this.width + "px";
  358. DOMutil.cleanupElements(this.DOMelements.lines);
  359. DOMutil.cleanupElements(this.DOMelements.labels);
  360. this.redraw();
  361. return true;
  362. }
  363. else {
  364. DOMutil.cleanupElements(this.DOMelements.lines);
  365. DOMutil.cleanupElements(this.DOMelements.labels);
  366. return false;
  367. }
  368. };
  369. DataAxis.prototype.convertValue = function (value) {
  370. var invertedValue = this.valueAtZero - value;
  371. var convertedValue = invertedValue * this.conversionFactor;
  372. return convertedValue;
  373. };
  374. /**
  375. * Create a label for the axis at position x
  376. * @private
  377. * @param y
  378. * @param text
  379. * @param orientation
  380. * @param className
  381. * @param characterHeight
  382. */
  383. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  384. // reuse redundant label
  385. var label = DOMutil.getDOMElement('div',this.DOMelements.labels, this.dom.frame); //this.dom.redundant.labels.shift();
  386. label.className = className;
  387. label.innerHTML = text;
  388. if (orientation == 'left') {
  389. label.style.left = '-' + this.options.labelOffsetX + 'px';
  390. label.style.textAlign = "right";
  391. }
  392. else {
  393. label.style.right = '-' + this.options.labelOffsetX + 'px';
  394. label.style.textAlign = "left";
  395. }
  396. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  397. text += '';
  398. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  399. if (this.maxLabelSize < text.length * largestWidth) {
  400. this.maxLabelSize = text.length * largestWidth;
  401. }
  402. };
  403. /**
  404. * Create a minor line for the axis at position y
  405. * @param y
  406. * @param orientation
  407. * @param className
  408. * @param offset
  409. * @param width
  410. */
  411. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  412. if (this.master == true) {
  413. var line = DOMutil.getDOMElement('div',this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  414. line.className = className;
  415. line.innerHTML = '';
  416. if (orientation == 'left') {
  417. line.style.left = (this.width - offset) + 'px';
  418. }
  419. else {
  420. line.style.right = (this.width - offset) + 'px';
  421. }
  422. line.style.width = width + 'px';
  423. line.style.top = y + 'px';
  424. }
  425. };
  426. /**
  427. * Create a title for the axis
  428. * @private
  429. * @param orientation
  430. */
  431. DataAxis.prototype._redrawTitle = function (orientation) {
  432. DOMutil.prepareElements(this.DOMelements.title);
  433. // Check if the title is defined for this axes
  434. if (this.options.title[orientation] !== undefined && this.options.title[orientation].text !== undefined) {
  435. var title = DOMutil.getDOMElement('div', this.DOMelements.title, this.dom.frame);
  436. title.className = 'yAxis title ' + orientation;
  437. title.innerHTML = this.options.title[orientation].text;
  438. // Add style - if provided
  439. if (this.options.title[orientation].style !== undefined) {
  440. util.addCssText(title, this.options.title[orientation].style);
  441. }
  442. if (orientation == 'left') {
  443. title.style.left = this.props.titleCharHeight + 'px';
  444. }
  445. else {
  446. title.style.right = this.props.titleCharHeight + 'px';
  447. }
  448. title.style.width = this.height + 'px';
  449. }
  450. // we need to clean up in case we did not use all elements.
  451. DOMutil.cleanupElements(this.DOMelements.title);
  452. };
  453. /**
  454. * Determine the size of text on the axis (both major and minor axis).
  455. * The size is calculated only once and then cached in this.props.
  456. * @private
  457. */
  458. DataAxis.prototype._calculateCharSize = function () {
  459. // determine the char width and height on the minor axis
  460. if (!('minorCharHeight' in this.props)) {
  461. var textMinor = document.createTextNode('0');
  462. var measureCharMinor = document.createElement('DIV');
  463. measureCharMinor.className = 'yAxis minor measure';
  464. measureCharMinor.appendChild(textMinor);
  465. this.dom.frame.appendChild(measureCharMinor);
  466. this.props.minorCharHeight = measureCharMinor.clientHeight;
  467. this.props.minorCharWidth = measureCharMinor.clientWidth;
  468. this.dom.frame.removeChild(measureCharMinor);
  469. }
  470. if (!('majorCharHeight' in this.props)) {
  471. var textMajor = document.createTextNode('0');
  472. var measureCharMajor = document.createElement('DIV');
  473. measureCharMajor.className = 'yAxis major measure';
  474. measureCharMajor.appendChild(textMajor);
  475. this.dom.frame.appendChild(measureCharMajor);
  476. this.props.majorCharHeight = measureCharMajor.clientHeight;
  477. this.props.majorCharWidth = measureCharMajor.clientWidth;
  478. this.dom.frame.removeChild(measureCharMajor);
  479. }
  480. if (!('titleCharHeight' in this.props)) {
  481. var textTitle = document.createTextNode('0');
  482. var measureCharTitle = document.createElement('DIV');
  483. measureCharTitle.className = 'yAxis title measure';
  484. measureCharTitle.appendChild(textTitle);
  485. this.dom.frame.appendChild(measureCharTitle);
  486. this.props.titleCharHeight = measureCharTitle.clientHeight;
  487. this.props.titleCharWidth = measureCharTitle.clientWidth;
  488. this.dom.frame.removeChild(measureCharTitle);
  489. }
  490. };
  491. /**
  492. * Snap a date to a rounded value.
  493. * The snap intervals are dependent on the current scale and step.
  494. * @param {Date} date the date to be snapped.
  495. * @return {Date} snappedDate
  496. */
  497. DataAxis.prototype.snap = function(date) {
  498. return this.step.snap(date);
  499. };
  500. module.exports = DataAxis;