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.

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