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.

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