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.

170 lines
5.9 KiB

10 years ago
  1. // DOM utility methods
  2. /**
  3. * this prepares the JSON container for allocating SVG elements
  4. * @param JSONcontainer
  5. * @private
  6. */
  7. exports.prepareElements = function(JSONcontainer) {
  8. // cleanup the redundant svgElements;
  9. for (var elementType in JSONcontainer) {
  10. if (JSONcontainer.hasOwnProperty(elementType)) {
  11. JSONcontainer[elementType].redundant = JSONcontainer[elementType].used;
  12. JSONcontainer[elementType].used = [];
  13. }
  14. }
  15. };
  16. /**
  17. * this cleans up all the unused SVG elements. By asking for the parentNode, we only need to supply the JSON container from
  18. * which to remove the redundant elements.
  19. *
  20. * @param JSONcontainer
  21. * @private
  22. */
  23. exports.cleanupElements = function(JSONcontainer) {
  24. // cleanup the redundant svgElements;
  25. for (var elementType in JSONcontainer) {
  26. if (JSONcontainer.hasOwnProperty(elementType)) {
  27. if (JSONcontainer[elementType].redundant) {
  28. for (var i = 0; i < JSONcontainer[elementType].redundant.length; i++) {
  29. JSONcontainer[elementType].redundant[i].parentNode.removeChild(JSONcontainer[elementType].redundant[i]);
  30. }
  31. JSONcontainer[elementType].redundant = [];
  32. }
  33. }
  34. }
  35. };
  36. /**
  37. * Allocate or generate an SVG element if needed. Store a reference to it in the JSON container and draw it in the svgContainer
  38. * the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
  39. *
  40. * @param elementType
  41. * @param JSONcontainer
  42. * @param svgContainer
  43. * @returns {*}
  44. * @private
  45. */
  46. exports.getSVGElement = function (elementType, JSONcontainer, svgContainer) {
  47. var element;
  48. // allocate SVG element, if it doesnt yet exist, create one.
  49. if (JSONcontainer.hasOwnProperty(elementType)) { // this element has been created before
  50. // check if there is an redundant element
  51. if (JSONcontainer[elementType].redundant.length > 0) {
  52. element = JSONcontainer[elementType].redundant[0];
  53. JSONcontainer[elementType].redundant.shift();
  54. }
  55. else {
  56. // create a new element and add it to the SVG
  57. element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
  58. svgContainer.appendChild(element);
  59. }
  60. }
  61. else {
  62. // create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
  63. element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
  64. JSONcontainer[elementType] = {used: [], redundant: []};
  65. svgContainer.appendChild(element);
  66. }
  67. JSONcontainer[elementType].used.push(element);
  68. return element;
  69. };
  70. /**
  71. * Allocate or generate an SVG element if needed. Store a reference to it in the JSON container and draw it in the svgContainer
  72. * the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
  73. *
  74. * @param elementType
  75. * @param JSONcontainer
  76. * @param DOMContainer
  77. * @returns {*}
  78. * @private
  79. */
  80. exports.getDOMElement = function (elementType, JSONcontainer, DOMContainer, insertBefore) {
  81. var element;
  82. // allocate DOM element, if it doesnt yet exist, create one.
  83. if (JSONcontainer.hasOwnProperty(elementType)) { // this element has been created before
  84. // check if there is an redundant element
  85. if (JSONcontainer[elementType].redundant.length > 0) {
  86. element = JSONcontainer[elementType].redundant[0];
  87. JSONcontainer[elementType].redundant.shift();
  88. }
  89. else {
  90. // create a new element and add it to the SVG
  91. element = document.createElement(elementType);
  92. if (insertBefore !== undefined) {
  93. DOMContainer.insertBefore(element, insertBefore);
  94. }
  95. else {
  96. DOMContainer.appendChild(element);
  97. }
  98. }
  99. }
  100. else {
  101. // create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
  102. element = document.createElement(elementType);
  103. JSONcontainer[elementType] = {used: [], redundant: []};
  104. if (insertBefore !== undefined) {
  105. DOMContainer.insertBefore(element, insertBefore);
  106. }
  107. else {
  108. DOMContainer.appendChild(element);
  109. }
  110. }
  111. JSONcontainer[elementType].used.push(element);
  112. return element;
  113. };
  114. /**
  115. * draw a point object. this is a seperate function because it can also be called by the legend.
  116. * The reason the JSONcontainer and the target SVG svgContainer have to be supplied is so the legend can use these functions
  117. * as well.
  118. *
  119. * @param x
  120. * @param y
  121. * @param group
  122. * @param JSONcontainer
  123. * @param svgContainer
  124. * @returns {*}
  125. */
  126. exports.drawPoint = function(x, y, group, JSONcontainer, svgContainer) {
  127. var point;
  128. if (group.options.drawPoints.style == 'circle') {
  129. point = exports.getSVGElement('circle',JSONcontainer,svgContainer);
  130. point.setAttributeNS(null, "cx", x);
  131. point.setAttributeNS(null, "cy", y);
  132. point.setAttributeNS(null, "r", 0.5 * group.options.drawPoints.size);
  133. point.setAttributeNS(null, "class", group.className + " point");
  134. }
  135. else {
  136. point = exports.getSVGElement('rect',JSONcontainer,svgContainer);
  137. point.setAttributeNS(null, "x", x - 0.5*group.options.drawPoints.size);
  138. point.setAttributeNS(null, "y", y - 0.5*group.options.drawPoints.size);
  139. point.setAttributeNS(null, "width", group.options.drawPoints.size);
  140. point.setAttributeNS(null, "height", group.options.drawPoints.size);
  141. point.setAttributeNS(null, "class", group.className + " point");
  142. }
  143. return point;
  144. };
  145. /**
  146. * draw a bar SVG element centered on the X coordinate
  147. *
  148. * @param x
  149. * @param y
  150. * @param className
  151. */
  152. exports.drawBar = function (x, y, width, height, className, JSONcontainer, svgContainer) {
  153. // if (height != 0) {
  154. var rect = exports.getSVGElement('rect',JSONcontainer, svgContainer);
  155. rect.setAttributeNS(null, "x", x - 0.5 * width);
  156. rect.setAttributeNS(null, "y", y);
  157. rect.setAttributeNS(null, "width", width);
  158. rect.setAttributeNS(null, "height", height);
  159. rect.setAttributeNS(null, "class", className);
  160. // }
  161. };