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.

639 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
  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: false,
  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. left:{
  30. range: {min:undefined,max:undefined},
  31. format: function (value) {return ''+Number.parseFloat(value.toPrecision(3));},
  32. title: {text:undefined,style:undefined}
  33. },
  34. right:{
  35. range: {min:undefined,max:undefined},
  36. format: function (value) {return ''+Number.parseFloat(value.toPrecision(3));},
  37. title: {text:undefined,style:undefined}
  38. }
  39. };
  40. this.linegraphOptions = linegraphOptions;
  41. this.linegraphSVG = svg;
  42. this.props = {};
  43. this.DOMelements = { // dynamic elements
  44. lines: {},
  45. labels: {},
  46. title: {}
  47. };
  48. this.dom = {};
  49. this.range = {start:0, end:0};
  50. this.options = util.extend({}, this.defaultOptions);
  51. this.conversionFactor = 1;
  52. this.setOptions(options);
  53. this.width = Number(('' + this.options.width).replace("px",""));
  54. this.minWidth = this.width;
  55. this.height = this.linegraphSVG.getBoundingClientRect().height;
  56. this.hidden = false;
  57. this.stepPixels = 25;
  58. this.zeroCrossing = -1;
  59. this.amountOfSteps = -1;
  60. this.lineOffset = 0;
  61. this.master = true;
  62. this.svgElements = {};
  63. this.iconsRemoved = false;
  64. this.groups = {};
  65. this.amountOfGroups = 0;
  66. // create the HTML DOM
  67. this._create();
  68. this.framework = {svg: this.svg, svgElements: this.svgElements, options: this.options, groups: this.groups};
  69. var me = this;
  70. this.body.emitter.on("verticalDrag", function() {
  71. me.dom.lineContainer.style.top = me.body.domProps.scrollTop + 'px';
  72. });
  73. }
  74. DataAxis.prototype = new Component();
  75. DataAxis.prototype.addGroup = function(label, graphOptions) {
  76. if (!this.groups.hasOwnProperty(label)) {
  77. this.groups[label] = graphOptions;
  78. }
  79. this.amountOfGroups += 1;
  80. };
  81. DataAxis.prototype.updateGroup = function(label, graphOptions) {
  82. if (!this.groups.hasOwnProperty(label)) {
  83. this.amountOfGroups += 1;
  84. }
  85. this.groups[label] = graphOptions;
  86. };
  87. DataAxis.prototype.removeGroup = function(label) {
  88. if (this.groups.hasOwnProperty(label)) {
  89. delete this.groups[label];
  90. this.amountOfGroups -= 1;
  91. }
  92. };
  93. DataAxis.prototype.setOptions = function (options) {
  94. if (options) {
  95. var redraw = false;
  96. if (this.options.orientation != options.orientation && options.orientation !== undefined) {
  97. redraw = true;
  98. }
  99. var fields = [
  100. 'orientation',
  101. 'showMinorLabels',
  102. 'showMajorLabels',
  103. 'icons',
  104. 'majorLinesOffset',
  105. 'minorLinesOffset',
  106. 'labelOffsetX',
  107. 'labelOffsetY',
  108. 'iconWidth',
  109. 'width',
  110. 'visible',
  111. 'left',
  112. 'right',
  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].getLegend(iconWidth, iconHeight, this.framework, x, y);
  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, stepSize, rangeStart, rangeEnd;
  304. if (this.master === false) {
  305. if (this.zeroCrossing !== -1 && this.options.alignZeros === true) {
  306. if (this.range.end > 0) {
  307. stepSize = this.range.end / this.zeroCrossing; // size of one step
  308. rangeStart = this.range.end - this.amountOfSteps * stepSize;
  309. rangeEnd = this.range.end;
  310. }
  311. else {
  312. // all of the range (including start) has to be done before the zero crossing.
  313. stepSize = -1 * this.range.start / (this.amountOfSteps - this.zeroCrossing); // absolute size of a step
  314. rangeStart = this.range.start;
  315. rangeEnd = this.range.start + stepSize * this.amountOfSteps;
  316. }
  317. }
  318. else {
  319. rangeStart = this.range.start;
  320. rangeEnd = this.range.end;
  321. }
  322. }
  323. else {
  324. // calculate range and step (step such that we have space for 7 characters per label)
  325. rangeStart = this.range.start;
  326. rangeEnd = this.range.end;
  327. }
  328. var minimumStep = this.props.majorCharHeight;
  329. this.step = step = new DataStep(
  330. rangeStart,
  331. rangeEnd,
  332. minimumStep,
  333. this.dom.frame.offsetHeight,
  334. this.options[this.options.orientation].range,
  335. this.options[this.options.orientation].format,
  336. this.master === false && this.options.alignZeros // does the step have to align zeros? only if not master and the options is on
  337. );
  338. // the slave axis needs to use the same horizontal lines as the master axis.
  339. if (this.master === true) {
  340. this.stepPixels = ((this.dom.frame.offsetHeight) / step.marginRange) * step.step;
  341. this.amountOfSteps = Math.ceil(this.dom.frame.offsetHeight / this.stepPixels);
  342. }
  343. else {
  344. // align with zero
  345. if (this.options.alignZeros === true && this.zeroCrossing !== -1) {
  346. // distance is the amount of steps away from the zero crossing we are.
  347. let distance = (step.current - this.zeroCrossing * step.step) / step.step;
  348. this.step.shift(distance);
  349. }
  350. }
  351. // value at the bottom of the SVG
  352. this.valueAtBottom = step.marginEnd;
  353. this.maxLabelSize = 0;
  354. var y = 0; // init value
  355. var stepIndex = 0; // init value
  356. var isMajor = false; // init value
  357. while (stepIndex < this.amountOfSteps) {
  358. y = Math.round(stepIndex * this.stepPixels);
  359. isMajor = step.isMajor();
  360. if (stepIndex > 0 && stepIndex !== this.amountOfSteps) {
  361. if (this.options['showMinorLabels'] && isMajor === false || this.master === false && this.options['showMinorLabels'] === true) {
  362. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'vis-y-axis vis-minor', this.props.minorCharHeight);
  363. }
  364. if (isMajor && this.options['showMajorLabels'] && this.master === true ||
  365. this.options['showMinorLabels'] === false && this.master === false && isMajor === true) {
  366. if (y >= 0) {
  367. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'vis-y-axis vis-major', this.props.majorCharHeight);
  368. }
  369. this._redrawLine(y, orientation, 'vis-grid vis-horizontal vis-major', this.options.majorLinesOffset, this.props.majorLineWidth);
  370. }
  371. else {
  372. this._redrawLine(y, orientation, 'vis-grid vis-horizontal vis-minor', this.options.minorLinesOffset, this.props.minorLineWidth);
  373. }
  374. }
  375. // get zero crossing
  376. if (this.master === true && step.current === 0) {
  377. this.zeroCrossing = stepIndex;
  378. }
  379. step.next();
  380. stepIndex += 1;
  381. }
  382. // get zero crossing if it's the last step
  383. if (this.master === true && step.current === 0) {
  384. this.zeroCrossing = stepIndex;
  385. }
  386. this.conversionFactor = this.stepPixels / step.step;
  387. // Note that title is rotated, so we're using the height, not width!
  388. var titleWidth = 0;
  389. if (this.options[orientation].title !== undefined && this.options[orientation].title.text !== undefined) {
  390. titleWidth = this.props.titleCharHeight;
  391. }
  392. var offset = this.options.icons === true ? Math.max(this.options.iconWidth, titleWidth) + this.options.labelOffsetX + 15 : titleWidth + this.options.labelOffsetX + 15;
  393. // this will resize the yAxis to accommodate the labels.
  394. if (this.maxLabelSize > (this.width - offset) && this.options.visible === true) {
  395. this.width = this.maxLabelSize + offset;
  396. this.options.width = this.width + "px";
  397. DOMutil.cleanupElements(this.DOMelements.lines);
  398. DOMutil.cleanupElements(this.DOMelements.labels);
  399. this.redraw();
  400. resized = true;
  401. }
  402. // this will resize the yAxis if it is too big for the labels.
  403. else if (this.maxLabelSize < (this.width - offset) && this.options.visible === true && this.width > this.minWidth) {
  404. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  405. this.options.width = this.width + "px";
  406. DOMutil.cleanupElements(this.DOMelements.lines);
  407. DOMutil.cleanupElements(this.DOMelements.labels);
  408. this.redraw();
  409. resized = true;
  410. }
  411. else {
  412. DOMutil.cleanupElements(this.DOMelements.lines);
  413. DOMutil.cleanupElements(this.DOMelements.labels);
  414. resized = false;
  415. }
  416. return resized;
  417. };
  418. DataAxis.prototype.convertValue = function (value) {
  419. var invertedValue = this.valueAtBottom - value;
  420. var convertedValue = invertedValue * this.conversionFactor;
  421. return convertedValue;
  422. };
  423. DataAxis.prototype.screenToValue = function (x) {
  424. return this.valueAtBottom - (x / this.conversionFactor);
  425. };
  426. /**
  427. * Create a label for the axis at position x
  428. * @private
  429. * @param y
  430. * @param text
  431. * @param orientation
  432. * @param className
  433. * @param characterHeight
  434. */
  435. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  436. // reuse redundant label
  437. var label = DOMutil.getDOMElement('div',this.DOMelements.labels, this.dom.frame); //this.dom.redundant.labels.shift();
  438. label.className = className;
  439. label.innerHTML = text;
  440. if (orientation === 'left') {
  441. label.style.left = '-' + this.options.labelOffsetX + 'px';
  442. label.style.textAlign = "right";
  443. }
  444. else {
  445. label.style.right = '-' + this.options.labelOffsetX + 'px';
  446. label.style.textAlign = "left";
  447. }
  448. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  449. text += '';
  450. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  451. if (this.maxLabelSize < text.length * largestWidth) {
  452. this.maxLabelSize = text.length * largestWidth;
  453. }
  454. };
  455. /**
  456. * Create a minor line for the axis at position y
  457. * @param y
  458. * @param orientation
  459. * @param className
  460. * @param offset
  461. * @param width
  462. */
  463. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  464. if (this.master === true) {
  465. var line = DOMutil.getDOMElement('div',this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  466. line.className = className;
  467. line.innerHTML = '';
  468. if (orientation === 'left') {
  469. line.style.left = (this.width - offset) + 'px';
  470. }
  471. else {
  472. line.style.right = (this.width - offset) + 'px';
  473. }
  474. line.style.width = width + 'px';
  475. line.style.top = y + 'px';
  476. }
  477. };
  478. /**
  479. * Create a title for the axis
  480. * @private
  481. * @param orientation
  482. */
  483. DataAxis.prototype._redrawTitle = function (orientation) {
  484. DOMutil.prepareElements(this.DOMelements.title);
  485. // Check if the title is defined for this axes
  486. if (this.options[orientation].title !== undefined && this.options[orientation].title.text !== undefined) {
  487. var title = DOMutil.getDOMElement('div', this.DOMelements.title, this.dom.frame);
  488. title.className = 'vis-y-axis vis-title vis-' + orientation;
  489. title.innerHTML = this.options[orientation].title.text;
  490. // Add style - if provided
  491. if (this.options[orientation].title.style !== undefined) {
  492. util.addCssText(title, this.options[orientation].title.style);
  493. }
  494. if (orientation === 'left') {
  495. title.style.left = this.props.titleCharHeight + 'px';
  496. }
  497. else {
  498. title.style.right = this.props.titleCharHeight + 'px';
  499. }
  500. title.style.width = this.height + 'px';
  501. }
  502. // we need to clean up in case we did not use all elements.
  503. DOMutil.cleanupElements(this.DOMelements.title);
  504. };
  505. /**
  506. * Determine the size of text on the axis (both major and minor axis).
  507. * The size is calculated only once and then cached in this.props.
  508. * @private
  509. */
  510. DataAxis.prototype._calculateCharSize = function () {
  511. // determine the char width and height on the minor axis
  512. if (!('minorCharHeight' in this.props)) {
  513. var textMinor = document.createTextNode('0');
  514. var measureCharMinor = document.createElement('div');
  515. measureCharMinor.className = 'vis-y-axis vis-minor vis-measure';
  516. measureCharMinor.appendChild(textMinor);
  517. this.dom.frame.appendChild(measureCharMinor);
  518. this.props.minorCharHeight = measureCharMinor.clientHeight;
  519. this.props.minorCharWidth = measureCharMinor.clientWidth;
  520. this.dom.frame.removeChild(measureCharMinor);
  521. }
  522. if (!('majorCharHeight' in this.props)) {
  523. var textMajor = document.createTextNode('0');
  524. var measureCharMajor = document.createElement('div');
  525. measureCharMajor.className = 'vis-y-axis vis-major vis-measure';
  526. measureCharMajor.appendChild(textMajor);
  527. this.dom.frame.appendChild(measureCharMajor);
  528. this.props.majorCharHeight = measureCharMajor.clientHeight;
  529. this.props.majorCharWidth = measureCharMajor.clientWidth;
  530. this.dom.frame.removeChild(measureCharMajor);
  531. }
  532. if (!('titleCharHeight' in this.props)) {
  533. var textTitle = document.createTextNode('0');
  534. var measureCharTitle = document.createElement('div');
  535. measureCharTitle.className = 'vis-y-axis vis-title vis-measure';
  536. measureCharTitle.appendChild(textTitle);
  537. this.dom.frame.appendChild(measureCharTitle);
  538. this.props.titleCharHeight = measureCharTitle.clientHeight;
  539. this.props.titleCharWidth = measureCharTitle.clientWidth;
  540. this.dom.frame.removeChild(measureCharTitle);
  541. }
  542. };
  543. module.exports = DataAxis;