UNPKG

7.47 kBJavaScriptView Raw
1// DOM utility methods
2
3/**
4 * this prepares the JSON container for allocating SVG elements
5 * @param {Object} JSONcontainer
6 * @private
7 */
8exports.prepareElements = function(JSONcontainer) {
9 // cleanup the redundant svgElements;
10 for (var elementType in JSONcontainer) {
11 if (JSONcontainer.hasOwnProperty(elementType)) {
12 JSONcontainer[elementType].redundant = JSONcontainer[elementType].used;
13 JSONcontainer[elementType].used = [];
14 }
15 }
16};
17
18/**
19 * this cleans up all the unused SVG elements. By asking for the parentNode, we only need to supply the JSON container from
20 * which to remove the redundant elements.
21 *
22 * @param {Object} JSONcontainer
23 * @private
24 */
25exports.cleanupElements = function(JSONcontainer) {
26 // cleanup the redundant svgElements;
27 for (var elementType in JSONcontainer) {
28 if (JSONcontainer.hasOwnProperty(elementType)) {
29 if (JSONcontainer[elementType].redundant) {
30 for (var i = 0; i < JSONcontainer[elementType].redundant.length; i++) {
31 JSONcontainer[elementType].redundant[i].parentNode.removeChild(JSONcontainer[elementType].redundant[i]);
32 }
33 JSONcontainer[elementType].redundant = [];
34 }
35 }
36 }
37};
38
39/**
40 * Ensures that all elements are removed first up so they can be recreated cleanly
41 * @param {Object} JSONcontainer
42 */
43exports.resetElements = function(JSONcontainer) {
44 exports.prepareElements(JSONcontainer);
45 exports.cleanupElements(JSONcontainer);
46 exports.prepareElements(JSONcontainer);
47};
48
49/**
50 * Allocate or generate an SVG element if needed. Store a reference to it in the JSON container and draw it in the svgContainer
51 * the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
52 *
53 * @param {string} elementType
54 * @param {Object} JSONcontainer
55 * @param {Object} svgContainer
56 * @returns {Element}
57 * @private
58 */
59exports.getSVGElement = function (elementType, JSONcontainer, svgContainer) {
60 var element;
61 // allocate SVG element, if it doesnt yet exist, create one.
62 if (JSONcontainer.hasOwnProperty(elementType)) { // this element has been created before
63 // check if there is an redundant element
64 if (JSONcontainer[elementType].redundant.length > 0) {
65 element = JSONcontainer[elementType].redundant[0];
66 JSONcontainer[elementType].redundant.shift();
67 }
68 else {
69 // create a new element and add it to the SVG
70 element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
71 svgContainer.appendChild(element);
72 }
73 }
74 else {
75 // create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
76 element = document.createElementNS('http://www.w3.org/2000/svg', elementType);
77 JSONcontainer[elementType] = {used: [], redundant: []};
78 svgContainer.appendChild(element);
79 }
80 JSONcontainer[elementType].used.push(element);
81 return element;
82};
83
84
85/**
86 * Allocate or generate an SVG element if needed. Store a reference to it in the JSON container and draw it in the svgContainer
87 * the JSON container and the SVG container have to be supplied so other svg containers (like the legend) can use this.
88 *
89 * @param {string} elementType
90 * @param {Object} JSONcontainer
91 * @param {Element} DOMContainer
92 * @param {Element} insertBefore
93 * @returns {*}
94 */
95exports.getDOMElement = function (elementType, JSONcontainer, DOMContainer, insertBefore) {
96 var element;
97 // allocate DOM element, if it doesnt yet exist, create one.
98 if (JSONcontainer.hasOwnProperty(elementType)) { // this element has been created before
99 // check if there is an redundant element
100 if (JSONcontainer[elementType].redundant.length > 0) {
101 element = JSONcontainer[elementType].redundant[0];
102 JSONcontainer[elementType].redundant.shift();
103 }
104 else {
105 // create a new element and add it to the SVG
106 element = document.createElement(elementType);
107 if (insertBefore !== undefined) {
108 DOMContainer.insertBefore(element, insertBefore);
109 }
110 else {
111 DOMContainer.appendChild(element);
112 }
113 }
114 }
115 else {
116 // create a new element and add it to the SVG, also create a new object in the svgElements to keep track of it.
117 element = document.createElement(elementType);
118 JSONcontainer[elementType] = {used: [], redundant: []};
119 if (insertBefore !== undefined) {
120 DOMContainer.insertBefore(element, insertBefore);
121 }
122 else {
123 DOMContainer.appendChild(element);
124 }
125 }
126 JSONcontainer[elementType].used.push(element);
127 return element;
128};
129
130
131
132
133/**
134 * Draw a point object. This is a separate function because it can also be called by the legend.
135 * The reason the JSONcontainer and the target SVG svgContainer have to be supplied is so the legend can use these functions
136 * as well.
137 *
138 * @param {number} x
139 * @param {number} y
140 * @param {Object} groupTemplate: A template containing the necessary information to draw the datapoint e.g., {style: 'circle', size: 5, className: 'className' }
141 * @param {Object} JSONcontainer
142 * @param {Object} svgContainer
143 * @param {Object} labelObj
144 * @returns {vis.PointItem}
145 */
146exports.drawPoint = function(x, y, groupTemplate, JSONcontainer, svgContainer, labelObj) {
147 var point;
148 if (groupTemplate.style == 'circle') {
149 point = exports.getSVGElement('circle', JSONcontainer, svgContainer);
150 point.setAttributeNS(null, "cx", x);
151 point.setAttributeNS(null, "cy", y);
152 point.setAttributeNS(null, "r", 0.5 * groupTemplate.size);
153 }
154 else {
155 point = exports.getSVGElement('rect', JSONcontainer, svgContainer);
156 point.setAttributeNS(null, "x", x - 0.5 * groupTemplate.size);
157 point.setAttributeNS(null, "y", y - 0.5 * groupTemplate.size);
158 point.setAttributeNS(null, "width", groupTemplate.size);
159 point.setAttributeNS(null, "height", groupTemplate.size);
160 }
161
162 if (groupTemplate.styles !== undefined) {
163 point.setAttributeNS(null, "style", groupTemplate.styles);
164 }
165 point.setAttributeNS(null, "class", groupTemplate.className + " vis-point");
166 //handle label
167
168
169 if (labelObj) {
170 var label = exports.getSVGElement('text', JSONcontainer, svgContainer);
171 if (labelObj.xOffset) {
172 x = x + labelObj.xOffset;
173 }
174
175 if (labelObj.yOffset) {
176 y = y + labelObj.yOffset;
177 }
178 if (labelObj.content) {
179 label.textContent = labelObj.content;
180 }
181
182 if (labelObj.className) {
183 label.setAttributeNS(null, "class", labelObj.className + " vis-label");
184 }
185 label.setAttributeNS(null, "x", x);
186 label.setAttributeNS(null, "y", y);
187 }
188
189 return point;
190};
191
192/**
193 * draw a bar SVG element centered on the X coordinate
194 *
195 * @param {number} x
196 * @param {number} y
197 * @param {number} width
198 * @param {number} height
199 * @param {string} className
200 * @param {Object} JSONcontainer
201 * @param {Object} svgContainer
202 * @param {string} style
203 */
204exports.drawBar = function (x, y, width, height, className, JSONcontainer, svgContainer, style) {
205 if (height != 0) {
206 if (height < 0) {
207 height *= -1;
208 y -= height;
209 }
210 var rect = exports.getSVGElement('rect',JSONcontainer, svgContainer);
211 rect.setAttributeNS(null, "x", x - 0.5 * width);
212 rect.setAttributeNS(null, "y", y);
213 rect.setAttributeNS(null, "width", width);
214 rect.setAttributeNS(null, "height", height);
215 rect.setAttributeNS(null, "class", className);
216 if (style) {
217 rect.setAttributeNS(null, "style", style);
218 }
219 }
220};
\No newline at end of file