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
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 resized = 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. this.props.width = this.body.domProps.left.width;
  266. this.props.height = this.body.domProps.left.height;
  267. }
  268. else { // right
  269. frame.style.top = '';
  270. frame.style.bottom = '0';
  271. frame.style.left = '0';
  272. frame.style.width = this.width + 'px';
  273. frame.style.height = this.height + "px";
  274. this.props.width = this.body.domProps.right.width;
  275. this.props.height = this.body.domProps.right.height;
  276. }
  277. resized = this._redrawLabels();
  278. resized = this._isResized() || resized;
  279. if (this.options.icons == true) {
  280. this._redrawGroupIcons();
  281. }
  282. else {
  283. this._cleanupIcons();
  284. }
  285. this._redrawTitle(orientation);
  286. }
  287. return resized;
  288. };
  289. /**
  290. * Repaint major and minor text labels and vertical grid lines
  291. * @private
  292. */
  293. DataAxis.prototype._redrawLabels = function () {
  294. var resized = false;
  295. DOMutil.prepareElements(this.DOMelements.lines);
  296. DOMutil.prepareElements(this.DOMelements.labels);
  297. var orientation = this.options['orientation'];
  298. // calculate range and step (step such that we have space for 7 characters per label)
  299. var minimumStep = this.master ? this.props.majorCharHeight || 10 : this.stepPixelsForced;
  300. var step = new DataStep(
  301. this.range.start,
  302. this.range.end,
  303. minimumStep,
  304. this.dom.frame.offsetHeight,
  305. this.options.customRange[this.options.orientation],
  306. this.master == false && this.options.alignZeros // doess the step have to align zeros? only if not master and the options is on
  307. );
  308. this.step = step;
  309. // get the distance in pixels for a step
  310. // dead space is space that is "left over" after a step
  311. var stepPixels = (this.dom.frame.offsetHeight - (step.deadSpace * (this.dom.frame.offsetHeight / step.marginRange))) / (((step.marginRange - step.deadSpace) / step.step));
  312. this.stepPixels = stepPixels;
  313. var amountOfSteps = this.height / stepPixels;
  314. var stepDifference = 0;
  315. // the slave axis needs to use the same horizontal lines as the master axis.
  316. if (this.master == false) {
  317. stepPixels = this.stepPixelsForced;
  318. stepDifference = Math.round((this.dom.frame.offsetHeight / stepPixels) - amountOfSteps);
  319. for (var i = 0; i < 0.5 * stepDifference; i++) {
  320. step.previous();
  321. }
  322. amountOfSteps = this.height / stepPixels;
  323. if (this.zeroCrossing != -1 && this.options.alignZeros == true) {
  324. var zeroStepDifference = (step.marginEnd / step.step) - this.zeroCrossing;
  325. if (zeroStepDifference > 0) {
  326. for (var i = 0; i < zeroStepDifference; i++) {step.next();}
  327. }
  328. else if (zeroStepDifference < 0) {
  329. for (var i = 0; i < -zeroStepDifference; i++) {step.previous();}
  330. }
  331. }
  332. }
  333. else {
  334. amountOfSteps += 0.25;
  335. }
  336. this.valueAtZero = step.marginEnd;
  337. var marginStartPos = 0;
  338. // do not draw the first label
  339. var max = 1;
  340. // Get the number of decimal places
  341. var decimals;
  342. if(this.options.format[orientation] !== undefined) {
  343. decimals = this.options.format[orientation].decimals;
  344. }
  345. this.maxLabelSize = 0;
  346. var y = 0;
  347. while (max < Math.round(amountOfSteps)) {
  348. step.next();
  349. y = Math.round(max * stepPixels);
  350. marginStartPos = max * stepPixels;
  351. var isMajor = step.isMajor();
  352. if (this.options['showMinorLabels'] && isMajor == false || this.master == false && this.options['showMinorLabels'] == true) {
  353. this._redrawLabel(y - 2, step.getCurrent(decimals), orientation, 'yAxis minor', this.props.minorCharHeight);
  354. }
  355. if (isMajor && this.options['showMajorLabels'] && this.master == true ||
  356. this.options['showMinorLabels'] == false && this.master == false && isMajor == true) {
  357. if (y >= 0) {
  358. this._redrawLabel(y - 2, step.getCurrent(decimals), orientation, 'yAxis major', this.props.majorCharHeight);
  359. }
  360. this._redrawLine(y, orientation, 'grid horizontal major', this.options.majorLinesOffset, this.props.majorLineWidth);
  361. }
  362. else {
  363. this._redrawLine(y, orientation, 'grid horizontal minor', this.options.minorLinesOffset, this.props.minorLineWidth);
  364. }
  365. if (this.master == true && step.current == 0) {
  366. this.zeroCrossing = max;
  367. }
  368. max++;
  369. }
  370. if (this.master == false) {
  371. this.conversionFactor = y / (this.valueAtZero - step.current);
  372. }
  373. else {
  374. this.conversionFactor = this.dom.frame.offsetHeight / step.marginRange;
  375. }
  376. // Note that title is rotated, so we're using the height, not width!
  377. var titleWidth = 0;
  378. if (this.options.title[orientation] !== undefined && this.options.title[orientation].text !== undefined) {
  379. titleWidth = this.props.titleCharHeight;
  380. }
  381. var offset = this.options.icons == true ? Math.max(this.options.iconWidth, titleWidth) + this.options.labelOffsetX + 15 : titleWidth + this.options.labelOffsetX + 15;
  382. // this will resize the yAxis to accommodate the labels.
  383. if (this.maxLabelSize > (this.width - offset) && this.options.visible == true) {
  384. this.width = 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. resized = true;
  390. }
  391. // this will resize the yAxis if it is too big for the labels.
  392. else if (this.maxLabelSize < (this.width - offset) && this.options.visible == true && this.width > this.minWidth) {
  393. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  394. this.options.width = this.width + "px";
  395. DOMutil.cleanupElements(this.DOMelements.lines);
  396. DOMutil.cleanupElements(this.DOMelements.labels);
  397. this.redraw();
  398. resized = true;
  399. }
  400. else {
  401. DOMutil.cleanupElements(this.DOMelements.lines);
  402. DOMutil.cleanupElements(this.DOMelements.labels);
  403. resized = false;
  404. }
  405. return resized;
  406. };
  407. DataAxis.prototype.convertValue = function (value) {
  408. var invertedValue = this.valueAtZero - value;
  409. var convertedValue = invertedValue * this.conversionFactor;
  410. return convertedValue;
  411. };
  412. /**
  413. * Create a label for the axis at position x
  414. * @private
  415. * @param y
  416. * @param text
  417. * @param orientation
  418. * @param className
  419. * @param characterHeight
  420. */
  421. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  422. // reuse redundant label
  423. var label = DOMutil.getDOMElement('div',this.DOMelements.labels, this.dom.frame); //this.dom.redundant.labels.shift();
  424. label.className = className;
  425. label.innerHTML = text;
  426. if (orientation == 'left') {
  427. label.style.left = '-' + this.options.labelOffsetX + 'px';
  428. label.style.textAlign = "right";
  429. }
  430. else {
  431. label.style.right = '-' + this.options.labelOffsetX + 'px';
  432. label.style.textAlign = "left";
  433. }
  434. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  435. text += '';
  436. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  437. if (this.maxLabelSize < text.length * largestWidth) {
  438. this.maxLabelSize = text.length * largestWidth;
  439. }
  440. };
  441. /**
  442. * Create a minor line for the axis at position y
  443. * @param y
  444. * @param orientation
  445. * @param className
  446. * @param offset
  447. * @param width
  448. */
  449. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  450. if (this.master == true) {
  451. var line = DOMutil.getDOMElement('div',this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  452. line.className = className;
  453. line.innerHTML = '';
  454. if (orientation == 'left') {
  455. line.style.left = (this.width - offset) + 'px';
  456. }
  457. else {
  458. line.style.right = (this.width - offset) + 'px';
  459. }
  460. line.style.width = width + 'px';
  461. line.style.top = y + 'px';
  462. }
  463. };
  464. /**
  465. * Create a title for the axis
  466. * @private
  467. * @param orientation
  468. */
  469. DataAxis.prototype._redrawTitle = function (orientation) {
  470. DOMutil.prepareElements(this.DOMelements.title);
  471. // Check if the title is defined for this axes
  472. if (this.options.title[orientation] !== undefined && this.options.title[orientation].text !== undefined) {
  473. var title = DOMutil.getDOMElement('div', this.DOMelements.title, this.dom.frame);
  474. title.className = 'yAxis title ' + orientation;
  475. title.innerHTML = this.options.title[orientation].text;
  476. // Add style - if provided
  477. if (this.options.title[orientation].style !== undefined) {
  478. util.addCssText(title, this.options.title[orientation].style);
  479. }
  480. if (orientation == 'left') {
  481. title.style.left = this.props.titleCharHeight + 'px';
  482. }
  483. else {
  484. title.style.right = this.props.titleCharHeight + 'px';
  485. }
  486. title.style.width = this.height + 'px';
  487. }
  488. // we need to clean up in case we did not use all elements.
  489. DOMutil.cleanupElements(this.DOMelements.title);
  490. };
  491. /**
  492. * Determine the size of text on the axis (both major and minor axis).
  493. * The size is calculated only once and then cached in this.props.
  494. * @private
  495. */
  496. DataAxis.prototype._calculateCharSize = function () {
  497. // determine the char width and height on the minor axis
  498. if (!('minorCharHeight' in this.props)) {
  499. var textMinor = document.createTextNode('0');
  500. var measureCharMinor = document.createElement('div');
  501. measureCharMinor.className = 'yAxis minor measure';
  502. measureCharMinor.appendChild(textMinor);
  503. this.dom.frame.appendChild(measureCharMinor);
  504. this.props.minorCharHeight = measureCharMinor.clientHeight;
  505. this.props.minorCharWidth = measureCharMinor.clientWidth;
  506. this.dom.frame.removeChild(measureCharMinor);
  507. }
  508. if (!('majorCharHeight' in this.props)) {
  509. var textMajor = document.createTextNode('0');
  510. var measureCharMajor = document.createElement('div');
  511. measureCharMajor.className = 'yAxis major measure';
  512. measureCharMajor.appendChild(textMajor);
  513. this.dom.frame.appendChild(measureCharMajor);
  514. this.props.majorCharHeight = measureCharMajor.clientHeight;
  515. this.props.majorCharWidth = measureCharMajor.clientWidth;
  516. this.dom.frame.removeChild(measureCharMajor);
  517. }
  518. if (!('titleCharHeight' in this.props)) {
  519. var textTitle = document.createTextNode('0');
  520. var measureCharTitle = document.createElement('div');
  521. measureCharTitle.className = 'yAxis title measure';
  522. measureCharTitle.appendChild(textTitle);
  523. this.dom.frame.appendChild(measureCharTitle);
  524. this.props.titleCharHeight = measureCharTitle.clientHeight;
  525. this.props.titleCharWidth = measureCharTitle.clientWidth;
  526. this.dom.frame.removeChild(measureCharTitle);
  527. }
  528. };
  529. /**
  530. * Snap a date to a rounded value.
  531. * The snap intervals are dependent on the current scale and step.
  532. * @param {Date} date the date to be snapped.
  533. * @return {Date} snappedDate
  534. */
  535. DataAxis.prototype.snap = function(date) {
  536. return this.step.snap(date);
  537. };
  538. module.exports = DataAxis;