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.

475 lines
13 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
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. this.groups[groupId].drawIcon(x, y, this.svgElements, this.svg, iconWidth, iconHeight);
  130. y += iconHeight + iconOffset;
  131. }
  132. }
  133. DOMutil.cleanupElements(this.svgElements);
  134. };
  135. /**
  136. * Create the HTML DOM for the DataAxis
  137. */
  138. DataAxis.prototype.show = function() {
  139. if (!this.dom.frame.parentNode) {
  140. if (this.options.orientation == 'left') {
  141. this.body.dom.left.appendChild(this.dom.frame);
  142. }
  143. else {
  144. this.body.dom.right.appendChild(this.dom.frame);
  145. }
  146. }
  147. if (!this.dom.lineContainer.parentNode) {
  148. this.body.dom.backgroundHorizontal.appendChild(this.dom.lineContainer);
  149. }
  150. };
  151. /**
  152. * Create the HTML DOM for the DataAxis
  153. */
  154. DataAxis.prototype.hide = function() {
  155. if (this.dom.frame.parentNode) {
  156. this.dom.frame.parentNode.removeChild(this.dom.frame);
  157. }
  158. if (this.dom.lineContainer.parentNode) {
  159. this.dom.lineContainer.parentNode.removeChild(this.dom.lineContainer);
  160. }
  161. };
  162. /**
  163. * Set a range (start and end)
  164. * @param end
  165. * @param start
  166. * @param end
  167. */
  168. DataAxis.prototype.setRange = function (start, end) {
  169. this.range.start = start;
  170. this.range.end = end;
  171. };
  172. /**
  173. * Repaint the component
  174. * @return {boolean} Returns true if the component is resized
  175. */
  176. DataAxis.prototype.redraw = function () {
  177. var changeCalled = false;
  178. if (this.amountOfGroups == 0) {
  179. this.hide();
  180. }
  181. else {
  182. this.show();
  183. this.height = Number(this.linegraphSVG.style.height.replace("px",""));
  184. // svg offsetheight did not work in firefox and explorer...
  185. this.dom.lineContainer.style.height = this.height + 'px';
  186. this.width = this.options.visible == true ? Number(('' + this.options.width).replace("px","")) : 0;
  187. var props = this.props;
  188. var frame = this.dom.frame;
  189. // update classname
  190. frame.className = 'dataaxis';
  191. // calculate character width and height
  192. this._calculateCharSize();
  193. var orientation = this.options.orientation;
  194. var showMinorLabels = this.options.showMinorLabels;
  195. var showMajorLabels = this.options.showMajorLabels;
  196. // determine the width and height of the elemens for the axis
  197. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  198. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  199. props.minorLineWidth = this.body.dom.backgroundHorizontal.offsetWidth - this.lineOffset - this.width + 2 * this.options.minorLinesOffset;
  200. props.minorLineHeight = 1;
  201. props.majorLineWidth = this.body.dom.backgroundHorizontal.offsetWidth - this.lineOffset - this.width + 2 * this.options.majorLinesOffset;
  202. props.majorLineHeight = 1;
  203. // take frame offline while updating (is almost twice as fast)
  204. if (orientation == 'left') {
  205. frame.style.top = '0';
  206. frame.style.left = '0';
  207. frame.style.bottom = '';
  208. frame.style.width = this.width + 'px';
  209. frame.style.height = this.height + "px";
  210. }
  211. else { // right
  212. frame.style.top = '';
  213. frame.style.bottom = '0';
  214. frame.style.left = '0';
  215. frame.style.width = this.width + 'px';
  216. frame.style.height = this.height + "px";
  217. }
  218. changeCalled = this._redrawLabels();
  219. if (this.options.icons == true) {
  220. this._redrawGroupIcons();
  221. }
  222. }
  223. return changeCalled;
  224. };
  225. /**
  226. * Repaint major and minor text labels and vertical grid lines
  227. * @private
  228. */
  229. DataAxis.prototype._redrawLabels = function () {
  230. DOMutil.prepareElements(this.DOMelements);
  231. var orientation = this.options['orientation'];
  232. // calculate range and step (step such that we have space for 7 characters per label)
  233. var minimumStep = this.master ? this.props.majorCharHeight || 10 : this.stepPixelsForced;
  234. var step = new DataStep(this.range.start, this.range.end, minimumStep, this.dom.frame.offsetHeight);
  235. this.step = step;
  236. step.first();
  237. // get the distance in pixels for a step
  238. var stepPixels = this.dom.frame.offsetHeight / ((step.marginRange / step.step) + 1);
  239. this.stepPixels = stepPixels;
  240. var amountOfSteps = this.height / stepPixels;
  241. var stepDifference = 0;
  242. if (this.master == false) {
  243. stepPixels = this.stepPixelsForced;
  244. stepDifference = Math.round((this.height / stepPixels) - amountOfSteps);
  245. for (var i = 0; i < 0.5 * stepDifference; i++) {
  246. step.previous();
  247. }
  248. amountOfSteps = this.height / stepPixels;
  249. }
  250. this.valueAtZero = step.marginEnd;
  251. var marginStartPos = 0;
  252. // do not draw the first label
  253. var max = 1;
  254. step.next();
  255. this.maxLabelSize = 0;
  256. var y = 0;
  257. while (max < Math.round(amountOfSteps)) {
  258. y = Math.round(max * stepPixels);
  259. marginStartPos = max * stepPixels;
  260. var isMajor = step.isMajor();
  261. if (this.options['showMinorLabels'] && isMajor == false || this.master == false && this.options['showMinorLabels'] == true) {
  262. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'yAxis minor', this.props.minorCharHeight);
  263. }
  264. if (isMajor && this.options['showMajorLabels'] && this.master == true ||
  265. this.options['showMinorLabels'] == false && this.master == false && isMajor == true) {
  266. if (y >= 0) {
  267. this._redrawLabel(y - 2, step.getCurrent(), orientation, 'yAxis major', this.props.majorCharHeight);
  268. }
  269. this._redrawLine(y, orientation, 'grid horizontal major', this.options.majorLinesOffset, this.props.majorLineWidth);
  270. }
  271. else {
  272. this._redrawLine(y, orientation, 'grid horizontal minor', this.options.minorLinesOffset, this.props.minorLineWidth);
  273. }
  274. step.next();
  275. max++;
  276. }
  277. this.conversionFactor = marginStartPos/((amountOfSteps-1) * step.step);
  278. var offset = this.options.icons == true ? this.options.iconWidth + this.options.labelOffsetX + 15 : this.options.labelOffsetX + 15;
  279. // this will resize the yAxis to accomodate the labels.
  280. if (this.maxLabelSize > (this.width - offset) && this.options.visible == true) {
  281. this.width = this.maxLabelSize + offset;
  282. this.options.width = this.width + "px";
  283. DOMutil.cleanupElements(this.DOMelements);
  284. this.redraw();
  285. return true;
  286. }
  287. // this will resize the yAxis if it is too big for the labels.
  288. else if (this.maxLabelSize < (this.width - offset) && this.options.visible == true && this.width > this.minWidth) {
  289. this.width = Math.max(this.minWidth,this.maxLabelSize + offset);
  290. this.options.width = this.width + "px";
  291. DOMutil.cleanupElements(this.DOMelements);
  292. this.redraw();
  293. return true;
  294. }
  295. else {
  296. DOMutil.cleanupElements(this.DOMelements);
  297. return false;
  298. }
  299. };
  300. /**
  301. * Create a label for the axis at position x
  302. * @private
  303. * @param y
  304. * @param text
  305. * @param orientation
  306. * @param className
  307. * @param characterHeight
  308. */
  309. DataAxis.prototype._redrawLabel = function (y, text, orientation, className, characterHeight) {
  310. // reuse redundant label
  311. var label = DOMutil.getDOMElement('div',this.DOMelements, this.dom.frame); //this.dom.redundant.labels.shift();
  312. label.className = className;
  313. label.innerHTML = text;
  314. if (orientation == 'left') {
  315. label.style.left = '-' + this.options.labelOffsetX + 'px';
  316. label.style.textAlign = "right";
  317. }
  318. else {
  319. label.style.right = '-' + this.options.labelOffsetX + 'px';
  320. label.style.textAlign = "left";
  321. }
  322. label.style.top = y - 0.5 * characterHeight + this.options.labelOffsetY + 'px';
  323. text += '';
  324. var largestWidth = Math.max(this.props.majorCharWidth,this.props.minorCharWidth);
  325. if (this.maxLabelSize < text.length * largestWidth) {
  326. this.maxLabelSize = text.length * largestWidth;
  327. }
  328. };
  329. /**
  330. * Create a minor line for the axis at position y
  331. * @param y
  332. * @param orientation
  333. * @param className
  334. * @param offset
  335. * @param width
  336. */
  337. DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) {
  338. if (this.master == true) {
  339. var line = DOMutil.getDOMElement('div',this.DOMelements, this.dom.lineContainer);//this.dom.redundant.lines.shift();
  340. line.className = className;
  341. line.innerHTML = '';
  342. if (orientation == 'left') {
  343. line.style.left = (this.width - offset) + 'px';
  344. }
  345. else {
  346. line.style.right = (this.width - offset) + 'px';
  347. }
  348. line.style.width = width + 'px';
  349. line.style.top = y + 'px';
  350. }
  351. };
  352. DataAxis.prototype.convertValue = function (value) {
  353. var invertedValue = this.valueAtZero - value;
  354. var convertedValue = invertedValue * this.conversionFactor;
  355. return convertedValue; // the -2 is to compensate for the borders
  356. };
  357. /**
  358. * Determine the size of text on the axis (both major and minor axis).
  359. * The size is calculated only once and then cached in this.props.
  360. * @private
  361. */
  362. DataAxis.prototype._calculateCharSize = function () {
  363. // determine the char width and height on the minor axis
  364. if (!('minorCharHeight' in this.props)) {
  365. var textMinor = document.createTextNode('0');
  366. var measureCharMinor = document.createElement('DIV');
  367. measureCharMinor.className = 'yAxis minor measure';
  368. measureCharMinor.appendChild(textMinor);
  369. this.dom.frame.appendChild(measureCharMinor);
  370. this.props.minorCharHeight = measureCharMinor.clientHeight;
  371. this.props.minorCharWidth = measureCharMinor.clientWidth;
  372. this.dom.frame.removeChild(measureCharMinor);
  373. }
  374. if (!('majorCharHeight' in this.props)) {
  375. var textMajor = document.createTextNode('0');
  376. var measureCharMajor = document.createElement('DIV');
  377. measureCharMajor.className = 'yAxis major measure';
  378. measureCharMajor.appendChild(textMajor);
  379. this.dom.frame.appendChild(measureCharMajor);
  380. this.props.majorCharHeight = measureCharMajor.clientHeight;
  381. this.props.majorCharWidth = measureCharMajor.clientWidth;
  382. this.dom.frame.removeChild(measureCharMajor);
  383. }
  384. };
  385. /**
  386. * Snap a date to a rounded value.
  387. * The snap intervals are dependent on the current scale and step.
  388. * @param {Date} date the date to be snapped.
  389. * @return {Date} snappedDate
  390. */
  391. DataAxis.prototype.snap = function(date) {
  392. return this.step.snap(date);
  393. };
  394. module.exports = DataAxis;