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.

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