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.

549 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 = this.options.title[orientation].text === undefined ? 0 : this.props.titleCharHeight;
  312. var offset = this.options.icons == true ? Math.max(this.options.iconWidth, titleWidth) + this.options.labelOffsetX + 15 : titleWidth + this.options.labelOffsetX + 15;
  313. // this will resize the yAxis to accommodate the labels.
  314. if (this.maxLabelSize > (this.width - offset) && this.options.visible == true) {
  315. this.width = this.maxLabelSize + offset;
  316. this.options.width = this.width + "px";
  317. DOMutil.cleanupElements(this.DOMelements.lines);
  318. DOMutil.cleanupElements(this.DOMelements.labels);
  319. this.redraw();
  320. return true;
  321. }
  322. // this will resize the yAxis if it is too big for the labels.
  323. else if (this.maxLabelSize < (this.width - offset) && this.options.visible == true && this.width > this.minWidth) {
  324. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  325. this.options.width = this.width + "px";
  326. DOMutil.cleanupElements(this.DOMelements.lines);
  327. DOMutil.cleanupElements(this.DOMelements.labels);
  328. this.redraw();
  329. return true;
  330. }
  331. else {
  332. DOMutil.cleanupElements(this.DOMelements.lines);
  333. DOMutil.cleanupElements(this.DOMelements.labels);
  334. return false;
  335. }
  336. };
  337. DataAxis.prototype.convertValue = function (value) {
  338. var invertedValue = this.valueAtZero - value;
  339. var convertedValue = invertedValue * this.conversionFactor;
  340. return convertedValue;
  341. };
  342. /**
  343. * Create a label for the axis at position x
  344. * @private
  345. * @param y
  346. * @param text
  347. * @param orientation
  348. * @param className
  349. * @param characterHeight
  350. */
  351. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  352. // reuse redundant label
  353. var label = DOMutil.getDOMElement('div',this.DOMelements.labels, this.dom.frame); //this.dom.redundant.labels.shift();
  354. label.className = className;
  355. label.innerHTML = text;
  356. if (orientation == 'left') {
  357. label.style.left = '-' + this.options.labelOffsetX + 'px';
  358. label.style.textAlign = "right";
  359. }
  360. else {
  361. label.style.right = '-' + this.options.labelOffsetX + 'px';
  362. label.style.textAlign = "left";
  363. }
  364. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  365. text += '';
  366. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  367. if (this.maxLabelSize < text.length * largestWidth) {
  368. this.maxLabelSize = text.length * largestWidth;
  369. }
  370. };
  371. /**
  372. * Create a minor line for the axis at position y
  373. * @param y
  374. * @param orientation
  375. * @param className
  376. * @param offset
  377. * @param width
  378. */
  379. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  380. if (this.master == true) {
  381. var line = DOMutil.getDOMElement('div',this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  382. line.className = className;
  383. line.innerHTML = '';
  384. if (orientation == 'left') {
  385. line.style.left = (this.width - offset) + 'px';
  386. }
  387. else {
  388. line.style.right = (this.width - offset) + 'px';
  389. }
  390. line.style.width = width + 'px';
  391. line.style.top = y + 'px';
  392. }
  393. };
  394. /**
  395. * Create a title for the axis
  396. * @private
  397. * @param orientation
  398. */
  399. DataAxis.prototype._redrawTitle = function (orientation) {
  400. DOMutil.prepareElements(this.DOMelements.title);
  401. // Check if the title is defined for this axes
  402. if(this.options.title[orientation] == false || this.options.title[orientation].text === undefined) {
  403. return;
  404. }
  405. var title = DOMutil.getDOMElement('div',this.DOMelements.title, this.dom.frame);
  406. title.className = 'yAxis title ' + orientation;
  407. title.innerHTML = this.options.title[orientation].text;
  408. if (orientation == 'left') {
  409. title.style.left = this.props.titleCharHeight + 'px';
  410. }
  411. else {
  412. title.style.right = this.props.titleCharHeight + 'px';
  413. }
  414. title.style.width = this.height + 'px';
  415. };
  416. /**
  417. * Determine the size of text on the axis (both major and minor axis).
  418. * The size is calculated only once and then cached in this.props.
  419. * @private
  420. */
  421. DataAxis.prototype._calculateCharSize = function () {
  422. // determine the char width and height on the minor axis
  423. if (!('minorCharHeight' in this.props)) {
  424. var textMinor = document.createTextNode('0');
  425. var measureCharMinor = document.createElement('DIV');
  426. measureCharMinor.className = 'yAxis minor measure';
  427. measureCharMinor.appendChild(textMinor);
  428. this.dom.frame.appendChild(measureCharMinor);
  429. this.props.minorCharHeight = measureCharMinor.clientHeight;
  430. this.props.minorCharWidth = measureCharMinor.clientWidth;
  431. this.dom.frame.removeChild(measureCharMinor);
  432. }
  433. if (!('majorCharHeight' in this.props)) {
  434. var textMajor = document.createTextNode('0');
  435. var measureCharMajor = document.createElement('DIV');
  436. measureCharMajor.className = 'yAxis major measure';
  437. measureCharMajor.appendChild(textMajor);
  438. this.dom.frame.appendChild(measureCharMajor);
  439. this.props.majorCharHeight = measureCharMajor.clientHeight;
  440. this.props.majorCharWidth = measureCharMajor.clientWidth;
  441. this.dom.frame.removeChild(measureCharMajor);
  442. }
  443. if (!('titleCharHeight' in this.props)) {
  444. var textTitle = document.createTextNode('0');
  445. var measureCharTitle = document.createElement('DIV');
  446. measureCharTitle.className = 'yAxis title measure';
  447. measureCharTitle.appendChild(textTitle);
  448. this.dom.frame.appendChild(measureCharTitle);
  449. this.props.titleCharHeight = measureCharTitle.clientHeight;
  450. this.props.titleCharWidth = measureCharTitle.clientWidth;
  451. this.dom.frame.removeChild(measureCharTitle);
  452. }
  453. };
  454. /**
  455. * Snap a date to a rounded value.
  456. * The snap intervals are dependent on the current scale and step.
  457. * @param {Date} date the date to be snapped.
  458. * @return {Date} snappedDate
  459. */
  460. DataAxis.prototype.snap = function(date) {
  461. return this.step.snap(date);
  462. };
  463. module.exports = DataAxis;