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.

160 lines
5.6 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) {
  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. DOMContainer.appendChild(element);
  93. }
  94. }
  95. else {
  96. // create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
  97. element = document.createElement(elementType);
  98. JSONcontainer[elementType] = {used: [], redundant: []};
  99. DOMContainer.appendChild(element);
  100. }
  101. JSONcontainer[elementType].used.push(element);
  102. return element;
  103. };
  104. /**
  105. * draw a point object. this is a seperate function because it can also be called by the legend.
  106. * The reason the JSONcontainer and the target SVG svgContainer have to be supplied is so the legend can use these functions
  107. * as well.
  108. *
  109. * @param x
  110. * @param y
  111. * @param group
  112. * @param JSONcontainer
  113. * @param svgContainer
  114. * @returns {*}
  115. */
  116. exports.drawPoint = function(x, y, group, JSONcontainer, svgContainer) {
  117. var point;
  118. if (group.options.drawPoints.style == 'circle') {
  119. point = exports.getSVGElement('circle',JSONcontainer,svgContainer);
  120. point.setAttributeNS(null, "cx", x);
  121. point.setAttributeNS(null, "cy", y);
  122. point.setAttributeNS(null, "r", 0.5 * group.options.drawPoints.size);
  123. point.setAttributeNS(null, "class", group.className + " point");
  124. }
  125. else {
  126. point = exports.getSVGElement('rect',JSONcontainer,svgContainer);
  127. point.setAttributeNS(null, "x", x - 0.5*group.options.drawPoints.size);
  128. point.setAttributeNS(null, "y", y - 0.5*group.options.drawPoints.size);
  129. point.setAttributeNS(null, "width", group.options.drawPoints.size);
  130. point.setAttributeNS(null, "height", group.options.drawPoints.size);
  131. point.setAttributeNS(null, "class", group.className + " point");
  132. }
  133. return point;
  134. };
  135. /**
  136. * draw a bar SVG element centered on the X coordinate
  137. *
  138. * @param x
  139. * @param y
  140. * @param className
  141. */
  142. exports.drawBar = function (x, y, width, height, className, JSONcontainer, svgContainer) {
  143. // if (height != 0) {
  144. var rect = exports.getSVGElement('rect',JSONcontainer, svgContainer);
  145. rect.setAttributeNS(null, "x", x - 0.5 * width);
  146. rect.setAttributeNS(null, "y", y);
  147. rect.setAttributeNS(null, "width", width);
  148. rect.setAttributeNS(null, "height", height);
  149. rect.setAttributeNS(null, "class", className);
  150. // }
  151. };