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. };
  33. this.linegraphOptions = linegraphOptions;
  34. this.linegraphSVG = svg;
  35. this.props = {};
  36. this.DOMelements = { // dynamic elements
  37. lines: {},
  38. labels: {}
  39. };
  40. this.dom = {};
  41. this.range = {start:0, end:0};
  42. this.options = util.extend({}, this.defaultOptions);
  43. this.conversionFactor = 1;
  44. this.setOptions(options);
  45. this.width = Number(('' + this.options.width).replace("px",""));
  46. this.minWidth = this.width;
  47. this.height = this.linegraphSVG.offsetHeight;
  48. this.stepPixels = 25;
  49. this.stepPixelsForced = 25;
  50. this.lineOffset = 0;
  51. this.master = true;
  52. this.svgElements = {};
  53. this.groups = {};
  54. this.amountOfGroups = 0;
  55. // create the HTML DOM
  56. this._create();
  57. var me = this;
  58. this.body.emitter.on("verticalDrag", function() {
  59. me.dom.lineContainer.style.top = me.body.domProps.scrollTop + 'px';
  60. });
  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. ];
  98. util.selectiveExtend(fields, this.options, options);
  99. this.minWidth = Number(('' + this.options.width).replace("px",""));
  100. if (redraw == true && this.dom.frame) {
  101. this.hide();
  102. this.show();
  103. }
  104. }
  105. };
  106. /**
  107. * Create the HTML DOM for the DataAxis
  108. */
  109. DataAxis.prototype._create = function() {
  110. this.dom.frame = document.createElement('div');
  111. this.dom.frame.style.width = this.options.width;
  112. this.dom.frame.style.height = this.height;
  113. this.dom.lineContainer = document.createElement('div');
  114. this.dom.lineContainer.style.width = '100%';
  115. this.dom.lineContainer.style.height = this.height;
  116. this.dom.lineContainer.style.position = 'relative';
  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. this.dom.lineContainer.style.top = this.body.domProps.scrollTop + 'px';
  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 elemens 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. }
  246. return changeCalled;
  247. };
  248. /**
  249. * Repaint major and minor text labels and vertical grid lines
  250. * @private
  251. */
  252. DataAxis.prototype._redrawLabels = function () {
  253. DOMutil.prepareElements(this.DOMelements.lines);
  254. DOMutil.prepareElements(this.DOMelements.labels);
  255. var orientation = this.options['orientation'];
  256. // calculate range and step (step such that we have space for 7 characters per label)
  257. var minimumStep = this.master ? this.props.majorCharHeight || 10 : this.stepPixelsForced;
  258. var step = new DataStep(this.range.start, this.range.end, minimumStep, this.dom.frame.offsetHeight, this.options.customRange[this.options.orientation]);
  259. this.step = step;
  260. // get the distance in pixels for a step
  261. // dead space is space that is "left over" after a step
  262. var stepPixels = (this.dom.frame.offsetHeight - (step.deadSpace * (this.dom.frame.offsetHeight / step.marginRange))) / (((step.marginRange - step.deadSpace) / step.step));
  263. this.stepPixels = stepPixels;
  264. var amountOfSteps = this.height / stepPixels;
  265. var stepDifference = 0;
  266. if (this.master == false) {
  267. stepPixels = this.stepPixelsForced;
  268. stepDifference = Math.round((this.dom.frame.offsetHeight / stepPixels) - amountOfSteps);
  269. for (var i = 0; i < 0.5 * stepDifference; i++) {
  270. step.previous();
  271. }
  272. amountOfSteps = this.height / stepPixels;
  273. }
  274. else {
  275. amountOfSteps += 0.25;
  276. }
  277. this.valueAtZero = step.marginEnd;
  278. var marginStartPos = 0;
  279. // do not draw the first label
  280. var max = 1;
  281. this.maxLabelSize = 0;
  282. var y = 0;
  283. while (max < Math.round(amountOfSteps)) {
  284. step.next();
  285. y = Math.round(max * stepPixels);
  286. marginStartPos = max * stepPixels;
  287. var isMajor = step.isMajor();
  288. if (this.options['showMinorLabels'] && isMajor == false || this.master == false && this.options['showMinorLabels'] == true) {
  289. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'yAxis minor', this.props.minorCharHeight);
  290. }
  291. if (isMajor && this.options['showMajorLabels'] && this.master == true ||
  292. this.options['showMinorLabels'] == false && this.master == false && isMajor == true) {
  293. if (y >= 0) {
  294. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'yAxis major', this.props.majorCharHeight);
  295. }
  296. this._redrawLine(y, orientation, 'grid horizontal major', this.options.majorLinesOffset, this.props.majorLineWidth);
  297. }
  298. else {
  299. this._redrawLine(y, orientation, 'grid horizontal minor', this.options.minorLinesOffset, this.props.minorLineWidth);
  300. }
  301. max++;
  302. }
  303. if (this.master == false) {
  304. this.conversionFactor = y / (this.valueAtZero - step.current);
  305. }
  306. else {
  307. this.conversionFactor = this.dom.frame.offsetHeight / step.marginRange;
  308. }
  309. var offset = this.options.icons == true ? this.options.iconWidth + this.options.labelOffsetX + 15 : this.options.labelOffsetX + 15;
  310. // this will resize the yAxis to accomodate the labels.
  311. if (this.maxLabelSize > (this.width - offset) && this.options.visible == true) {
  312. this.width = this.maxLabelSize + offset;
  313. this.options.width = this.width + "px";
  314. DOMutil.cleanupElements(this.DOMelements.lines);
  315. DOMutil.cleanupElements(this.DOMelements.labels);
  316. this.redraw();
  317. return true;
  318. }
  319. // this will resize the yAxis if it is too big for the labels.
  320. else if (this.maxLabelSize < (this.width - offset) && this.options.visible == true && this.width > this.minWidth) {
  321. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  322. this.options.width = this.width + "px";
  323. DOMutil.cleanupElements(this.DOMelements.lines);
  324. DOMutil.cleanupElements(this.DOMelements.labels);
  325. this.redraw();
  326. return true;
  327. }
  328. else {
  329. DOMutil.cleanupElements(this.DOMelements.lines);
  330. DOMutil.cleanupElements(this.DOMelements.labels);
  331. return false;
  332. }
  333. };
  334. DataAxis.prototype.convertValue = function (value) {
  335. var invertedValue = this.valueAtZero - value;
  336. var convertedValue = invertedValue * this.conversionFactor;
  337. return convertedValue;
  338. };
  339. /**
  340. * Create a label for the axis at position x
  341. * @private
  342. * @param y
  343. * @param text
  344. * @param orientation
  345. * @param className
  346. * @param characterHeight
  347. */
  348. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  349. // reuse redundant label
  350. var label = DOMutil.getDOMElement('div',this.DOMelements.labels, this.dom.frame); //this.dom.redundant.labels.shift();
  351. label.className = className;
  352. label.innerHTML = text;
  353. if (orientation == 'left') {
  354. label.style.left = '-' + this.options.labelOffsetX + 'px';
  355. label.style.textAlign = "right";
  356. }
  357. else {
  358. label.style.right = '-' + this.options.labelOffsetX + 'px';
  359. label.style.textAlign = "left";
  360. }
  361. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  362. text += '';
  363. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  364. if (this.maxLabelSize < text.length * largestWidth) {
  365. this.maxLabelSize = text.length * largestWidth;
  366. }
  367. };
  368. /**
  369. * Create a minor line for the axis at position y
  370. * @param y
  371. * @param orientation
  372. * @param className
  373. * @param offset
  374. * @param width
  375. */
  376. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  377. if (this.master == true) {
  378. var line = DOMutil.getDOMElement('div',this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  379. line.className = className;
  380. line.innerHTML = '';
  381. if (orientation == 'left') {
  382. line.style.left = (this.width - offset) + 'px';
  383. }
  384. else {
  385. line.style.right = (this.width - offset) + 'px';
  386. }
  387. line.style.width = width + 'px';
  388. line.style.top = y + 'px';
  389. }
  390. };
  391. /**
  392. * Determine the size of text on the axis (both major and minor axis).
  393. * The size is calculated only once and then cached in this.props.
  394. * @private
  395. */
  396. DataAxis.prototype._calculateCharSize = function () {
  397. // determine the char width and height on the minor axis
  398. if (!('minorCharHeight' in this.props)) {
  399. var textMinor = document.createTextNode('0');
  400. var measureCharMinor = document.createElement('DIV');
  401. measureCharMinor.className = 'yAxis minor measure';
  402. measureCharMinor.appendChild(textMinor);
  403. this.dom.frame.appendChild(measureCharMinor);
  404. this.props.minorCharHeight = measureCharMinor.clientHeight;
  405. this.props.minorCharWidth = measureCharMinor.clientWidth;
  406. this.dom.frame.removeChild(measureCharMinor);
  407. }
  408. if (!('majorCharHeight' in this.props)) {
  409. var textMajor = document.createTextNode('0');
  410. var measureCharMajor = document.createElement('DIV');
  411. measureCharMajor.className = 'yAxis major measure';
  412. measureCharMajor.appendChild(textMajor);
  413. this.dom.frame.appendChild(measureCharMajor);
  414. this.props.majorCharHeight = measureCharMajor.clientHeight;
  415. this.props.majorCharWidth = measureCharMajor.clientWidth;
  416. this.dom.frame.removeChild(measureCharMajor);
  417. }
  418. };
  419. /**
  420. * Snap a date to a rounded value.
  421. * The snap intervals are dependent on the current scale and step.
  422. * @param {Date} date the date to be snapped.
  423. * @return {Date} snappedDate
  424. */
  425. DataAxis.prototype.snap = function(date) {
  426. return this.step.snap(date);
  427. };
  428. module.exports = DataAxis;