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.

197 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.dom.frame.appendChild(this.svg);
  64. this.dom.frame.appendChild(this.dom.textArea);
  65. };
  66. /**
  67. * Hide the component from the DOM
  68. */
  69. Legend.prototype.hide = function() {
  70. // remove the frame containing the items
  71. if (this.dom.frame.parentNode) {
  72. this.dom.frame.parentNode.removeChild(this.dom.frame);
  73. }
  74. };
  75. /**
  76. * Show the component in the DOM (when not already visible).
  77. * @return {Boolean} changed
  78. */
  79. Legend.prototype.show = function() {
  80. // show frame containing the items
  81. if (!this.dom.frame.parentNode) {
  82. this.body.dom.center.appendChild(this.dom.frame);
  83. }
  84. };
  85. Legend.prototype.setOptions = function(options) {
  86. var fields = ['enabled','orientation','icons','left','right'];
  87. util.selectiveDeepExtend(fields, this.options, options);
  88. };
  89. Legend.prototype.redraw = function() {
  90. var activeGroups = 0;
  91. for (var groupId in this.groups) {
  92. if (this.groups.hasOwnProperty(groupId)) {
  93. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  94. activeGroups++;
  95. }
  96. }
  97. }
  98. if (this.options[this.side].visible == false || this.amountOfGroups == 0 || this.options.enabled == false || activeGroups == 0) {
  99. this.hide();
  100. }
  101. else {
  102. this.show();
  103. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'bottom-left') {
  104. this.dom.frame.style.left = '4px';
  105. this.dom.frame.style.textAlign = "left";
  106. this.dom.textArea.style.textAlign = "left";
  107. this.dom.textArea.style.left = (this.options.iconSize + 15) + 'px';
  108. this.dom.textArea.style.right = '';
  109. this.svg.style.left = 0 +'px';
  110. this.svg.style.right = '';
  111. }
  112. else {
  113. this.dom.frame.style.right = '4px';
  114. this.dom.frame.style.textAlign = "right";
  115. this.dom.textArea.style.textAlign = "right";
  116. this.dom.textArea.style.right = (this.options.iconSize + 15) + 'px';
  117. this.dom.textArea.style.left = '';
  118. this.svg.style.right = 0 +'px';
  119. this.svg.style.left = '';
  120. }
  121. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'top-right') {
  122. this.dom.frame.style.top = 4 - Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  123. this.dom.frame.style.bottom = '';
  124. }
  125. else {
  126. this.dom.frame.style.bottom = 4 - Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  127. this.dom.frame.style.top = '';
  128. }
  129. if (this.options.icons == false) {
  130. this.dom.frame.style.width = this.dom.textArea.offsetWidth + 10 + 'px';
  131. this.dom.textArea.style.right = '';
  132. this.dom.textArea.style.left = '';
  133. this.svg.style.width = '0px';
  134. }
  135. else {
  136. this.dom.frame.style.width = this.options.iconSize + 15 + this.dom.textArea.offsetWidth + 10 + 'px'
  137. this.drawLegendIcons();
  138. }
  139. var content = '';
  140. for (var groupId in this.groups) {
  141. if (this.groups.hasOwnProperty(groupId)) {
  142. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  143. content += this.groups[groupId].content + '<br />';
  144. }
  145. }
  146. }
  147. this.dom.textArea.innerHTML = content;
  148. this.dom.textArea.style.lineHeight = ((0.75 * this.options.iconSize) + this.options.iconSpacing) + 'px';
  149. }
  150. };
  151. Legend.prototype.drawLegendIcons = function() {
  152. if (this.dom.frame.parentNode) {
  153. DOMutil.prepareElements(this.svgElements);
  154. var padding = window.getComputedStyle(this.dom.frame).paddingTop;
  155. var iconOffset = Number(padding.replace('px',''));
  156. var x = iconOffset;
  157. var iconWidth = this.options.iconSize;
  158. var iconHeight = 0.75 * this.options.iconSize;
  159. var y = iconOffset + 0.5 * iconHeight + 3;
  160. this.svg.style.width = iconWidth + 5 + iconOffset + 'px';
  161. for (var groupId in this.groups) {
  162. if (this.groups.hasOwnProperty(groupId)) {
  163. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  164. this.groups[groupId].drawIcon(x, y, this.svgElements, this.svg, iconWidth, iconHeight);
  165. y += iconHeight + this.options.iconSpacing;
  166. }
  167. }
  168. }
  169. DOMutil.cleanupElements(this.svgElements);
  170. }
  171. };
  172. module.exports = Legend;