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.

221 lines
7.0 KiB

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