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.

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