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.

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