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.

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