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.

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