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.

211 lines
6.6 KiB

  1. var util = require('../../util');
  2. var DOMutil = require('../../DOMutil');
  3. var Component = require('./Component');
  4. /**
  5. * Legend for Graph2d
  6. */
  7. function Legend(body, options, side, linegraphOptions) {
  8. this.body = body;
  9. this.defaultOptions = {
  10. enabled: true,
  11. icons: true,
  12. iconSize: 20,
  13. iconSpacing: 6,
  14. left: {
  15. visible: true,
  16. position: 'top-left' // top/bottom - left,center,right
  17. },
  18. right: {
  19. visible: true,
  20. position: 'top-left' // top/bottom - left,center,right
  21. }
  22. }
  23. this.side = side;
  24. this.options = util.extend({},this.defaultOptions);
  25. this.linegraphOptions = linegraphOptions;
  26. this.svgElements = {};
  27. this.dom = {};
  28. this.groups = {};
  29. this.amountOfGroups = 0;
  30. this._create();
  31. this.setOptions(options);
  32. }
  33. Legend.prototype = new Component();
  34. Legend.prototype.clear = function() {
  35. this.groups = {};
  36. this.amountOfGroups = 0;
  37. };
  38. Legend.prototype.addGroup = function(label, graphOptions) {
  39. if (!this.groups.hasOwnProperty(label)) {
  40. this.groups[label] = graphOptions;
  41. }
  42. this.amountOfGroups += 1;
  43. };
  44. Legend.prototype.updateGroup = function(label, graphOptions) {
  45. this.groups[label] = graphOptions;
  46. };
  47. Legend.prototype.removeGroup = function(label) {
  48. if (this.groups.hasOwnProperty(label)) {
  49. delete this.groups[label];
  50. this.amountOfGroups -= 1;
  51. }
  52. };
  53. Legend.prototype._create = function() {
  54. this.dom.frame = document.createElement('div');
  55. this.dom.frame.className = 'vis-legend';
  56. this.dom.frame.style.position = "absolute";
  57. this.dom.frame.style.top = "10px";
  58. this.dom.frame.style.display = "block";
  59. this.dom.textArea = document.createElement('div');
  60. this.dom.textArea.className = 'vis-legend-text';
  61. this.dom.textArea.style.position = "relative";
  62. this.dom.textArea.style.top = "0px";
  63. this.svg = document.createElementNS('http://www.w3.org/2000/svg',"svg");
  64. this.svg.style.position = 'absolute';
  65. this.svg.style.top = 0 +'px';
  66. this.svg.style.width = this.options.iconSize + 5 + 'px';
  67. this.svg.style.height = '100%';
  68. this.dom.frame.appendChild(this.svg);
  69. this.dom.frame.appendChild(this.dom.textArea);
  70. };
  71. /**
  72. * Hide the component from the DOM
  73. */
  74. Legend.prototype.hide = function() {
  75. // remove the frame containing the items
  76. if (this.dom.frame.parentNode) {
  77. this.dom.frame.parentNode.removeChild(this.dom.frame);
  78. }
  79. };
  80. /**
  81. * Show the component in the DOM (when not already visible).
  82. * @return {Boolean} changed
  83. */
  84. Legend.prototype.show = function() {
  85. // show frame containing the items
  86. if (!this.dom.frame.parentNode) {
  87. this.body.dom.center.appendChild(this.dom.frame);
  88. }
  89. };
  90. Legend.prototype.setOptions = function(options) {
  91. var fields = ['enabled','orientation','icons','left','right'];
  92. util.selectiveDeepExtend(fields, this.options, options);
  93. };
  94. Legend.prototype.redraw = function() {
  95. var activeGroups = 0;
  96. var groupArray = Object.keys(this.groups);
  97. groupArray.sort(function (a,b) {
  98. return (a < b ? -1 : 1);
  99. })
  100. for (var i = 0; i < groupArray.length; i++) {
  101. var groupId = groupArray[i];
  102. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  103. activeGroups++;
  104. }
  105. }
  106. if (this.options[this.side].visible == false || this.amountOfGroups == 0 || this.options.enabled == false || activeGroups == 0) {
  107. this.hide();
  108. }
  109. else {
  110. this.show();
  111. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'bottom-left') {
  112. this.dom.frame.style.left = '4px';
  113. this.dom.frame.style.textAlign = "left";
  114. this.dom.textArea.style.textAlign = "left";
  115. this.dom.textArea.style.left = (this.options.iconSize + 15) + 'px';
  116. this.dom.textArea.style.right = '';
  117. this.svg.style.left = 0 +'px';
  118. this.svg.style.right = '';
  119. }
  120. else {
  121. this.dom.frame.style.right = '4px';
  122. this.dom.frame.style.textAlign = "right";
  123. this.dom.textArea.style.textAlign = "right";
  124. this.dom.textArea.style.right = (this.options.iconSize + 15) + 'px';
  125. this.dom.textArea.style.left = '';
  126. this.svg.style.right = 0 +'px';
  127. this.svg.style.left = '';
  128. }
  129. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'top-right') {
  130. this.dom.frame.style.top = 4 - Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  131. this.dom.frame.style.bottom = '';
  132. }
  133. else {
  134. var scrollableHeight = this.body.domProps.center.height - this.body.domProps.centerContainer.height;
  135. this.dom.frame.style.bottom = 4 + scrollableHeight + Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  136. this.dom.frame.style.top = '';
  137. }
  138. if (this.options.icons == false) {
  139. this.dom.frame.style.width = this.dom.textArea.offsetWidth + 10 + 'px';
  140. this.dom.textArea.style.right = '';
  141. this.dom.textArea.style.left = '';
  142. this.svg.style.width = '0px';
  143. }
  144. else {
  145. this.dom.frame.style.width = this.options.iconSize + 15 + this.dom.textArea.offsetWidth + 10 + 'px'
  146. this.drawLegendIcons();
  147. }
  148. var content = '';
  149. for (var i = 0; i < groupArray.length; i++) {
  150. var groupId = groupArray[i];
  151. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  152. content += this.groups[groupId].content + '<br />';
  153. }
  154. }
  155. this.dom.textArea.innerHTML = content;
  156. this.dom.textArea.style.lineHeight = ((0.75 * this.options.iconSize) + this.options.iconSpacing) + 'px';
  157. }
  158. };
  159. Legend.prototype.drawLegendIcons = function() {
  160. if (this.dom.frame.parentNode) {
  161. var groupArray = Object.keys(this.groups);
  162. groupArray.sort(function (a,b) {
  163. return (a < b ? -1 : 1);
  164. })
  165. DOMutil.prepareElements(this.svgElements);
  166. var padding = window.getComputedStyle(this.dom.frame).paddingTop;
  167. var iconOffset = Number(padding.replace('px',''));
  168. var x = iconOffset;
  169. var iconWidth = this.options.iconSize;
  170. var iconHeight = 0.75 * this.options.iconSize;
  171. var y = iconOffset + 0.5 * iconHeight + 3;
  172. this.svg.style.width = iconWidth + 5 + iconOffset + 'px';
  173. for (var i = 0; i < groupArray.length; i++) {
  174. var groupId = groupArray[i];
  175. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  176. this.groups[groupId].drawIcon(x, y, this.svgElements, this.svg, iconWidth, iconHeight);
  177. y += iconHeight + this.options.iconSpacing;
  178. }
  179. }
  180. DOMutil.cleanupElements(this.svgElements);
  181. }
  182. };
  183. module.exports = Legend;