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.

202 lines
6.3 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 = '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 = 'legendText';
  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. for (var groupId in this.groups) {
  97. if (this.groups.hasOwnProperty(groupId)) {
  98. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  99. activeGroups++;
  100. }
  101. }
  102. }
  103. if (this.options[this.side].visible == false || this.amountOfGroups == 0 || this.options.enabled == false || activeGroups == 0) {
  104. this.hide();
  105. }
  106. else {
  107. this.show();
  108. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'bottom-left') {
  109. this.dom.frame.style.left = '4px';
  110. this.dom.frame.style.textAlign = "left";
  111. this.dom.textArea.style.textAlign = "left";
  112. this.dom.textArea.style.left = (this.options.iconSize + 15) + 'px';
  113. this.dom.textArea.style.right = '';
  114. this.svg.style.left = 0 +'px';
  115. this.svg.style.right = '';
  116. }
  117. else {
  118. this.dom.frame.style.right = '4px';
  119. this.dom.frame.style.textAlign = "right";
  120. this.dom.textArea.style.textAlign = "right";
  121. this.dom.textArea.style.right = (this.options.iconSize + 15) + 'px';
  122. this.dom.textArea.style.left = '';
  123. this.svg.style.right = 0 +'px';
  124. this.svg.style.left = '';
  125. }
  126. if (this.options[this.side].position == 'top-left' || this.options[this.side].position == 'top-right') {
  127. this.dom.frame.style.top = 4 - Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  128. this.dom.frame.style.bottom = '';
  129. }
  130. else {
  131. this.dom.frame.style.bottom = 4 - Number(this.body.dom.center.style.top.replace("px","")) + 'px';
  132. this.dom.frame.style.top = '';
  133. }
  134. if (this.options.icons == false) {
  135. this.dom.frame.style.width = this.dom.textArea.offsetWidth + 10 + 'px';
  136. this.dom.textArea.style.right = '';
  137. this.dom.textArea.style.left = '';
  138. this.svg.style.width = '0px';
  139. }
  140. else {
  141. this.dom.frame.style.width = this.options.iconSize + 15 + this.dom.textArea.offsetWidth + 10 + 'px'
  142. this.drawLegendIcons();
  143. }
  144. var content = '';
  145. for (var groupId in this.groups) {
  146. if (this.groups.hasOwnProperty(groupId)) {
  147. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  148. content += this.groups[groupId].content + '<br />';
  149. }
  150. }
  151. }
  152. this.dom.textArea.innerHTML = content;
  153. this.dom.textArea.style.lineHeight = ((0.75 * this.options.iconSize) + this.options.iconSpacing) + 'px';
  154. }
  155. };
  156. Legend.prototype.drawLegendIcons = function() {
  157. if (this.dom.frame.parentNode) {
  158. DOMutil.prepareElements(this.svgElements);
  159. var padding = window.getComputedStyle(this.dom.frame).paddingTop;
  160. var iconOffset = Number(padding.replace('px',''));
  161. var x = iconOffset;
  162. var iconWidth = this.options.iconSize;
  163. var iconHeight = 0.75 * this.options.iconSize;
  164. var y = iconOffset + 0.5 * iconHeight + 3;
  165. this.svg.style.width = iconWidth + 5 + iconOffset + 'px';
  166. for (var groupId in this.groups) {
  167. if (this.groups.hasOwnProperty(groupId)) {
  168. if (this.groups[groupId].visible == true && (this.linegraphOptions.visibility[groupId] === undefined || this.linegraphOptions.visibility[groupId] == true)) {
  169. this.groups[groupId].drawIcon(x, y, this.svgElements, this.svg, iconWidth, iconHeight);
  170. y += iconHeight + this.options.iconSpacing;
  171. }
  172. }
  173. }
  174. DOMutil.cleanupElements(this.svgElements);
  175. }
  176. };
  177. module.exports = Legend;