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.

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