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.

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