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.

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