ii *_*to1 1 html javascript svg polyline
我需要使用 SVG/折线元素动态创建迷你图,使用纯 HTML 的示例完美地工作,当我使用 JavaScript 创建元素并添加属性时出现问题。
创建元素的函数
function createElement(type, attributes, someElement) {
var element = type == "svg" ? document.createElementNS('http://www.w3.org/2000/svg', 'svg') : document.createElement(type);
for (var key in attributes) {
if (key === "class") {
var cls = attributes[key];
for (var c in cls)
element.classList.add(cls[c]);
} else {
element[key] = attributes[key];
}
}
someElement.appendChild(element);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我创建了 SVG 元素并将其添加到名为 filter_r_inner 的 div 中,然后添加属性。
var newElement = createElement("svg", {
"class": ['mktcap_spark'],
"id": "weekly_svg",
"viewBox": "0 0 500 100"
}, filter_r_inner);
var weekly_svg = document.getElementById("weekly_svg");
weekly_svg.setAttribute("viewBox", "0 0 500 100");
Run Code Online (Sandbox Code Playgroud)
在这里,我创建了折线元素并将其添加到 SVG 元素,然后添加属性。
var newElement = createElement("polyline", {
"id": "weekly_poly"
}, weekly_svg);
var weekly_poly = document.getElementById("weekly_poly");
weekly_poly.setAttribute('points', "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100 160,90 180,80 200, 110 220, 10 240, 70 260, 100 280, 100 300, 40 320, 0 340, 100 360, 100 380, 120 400, 60 420, 70 440, 80 460, 20 480, 50 500, 30");
weekly_poly.setAttribute("fill", "none");
weekly_poly.setAttribute("stroke", "#e9be3d");
weekly_poly.setAttribute("stroke-width", "8");
Run Code Online (Sandbox Code Playgroud)
上面的内容并没有像我期望的那样渲染 SVG 迷你图,但是添加了所有属性,但没有显示任何内容。
我也尝试从这个问题中以这种方式添加点,这给了我一个 Weekly_svg.points is undefined 的错误
var point = weekly_svg.createSVGPoint();
point.x = 10;
point.y = 20;
weekly_poly.points.appendItem(point);
Run Code Online (Sandbox Code Playgroud)
我也研究过 setAttributeNS 但它需要一个“命名空间”,我尝试了这个但仍然没有显示。
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', 'points', "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100 160,90 180,80 200, 110 220, 10 240, 70 260, 100 280, 100 300, 40 320, 0 340, 100 360, 100 380, 120 400, 60 420, 70 440, 80 460, 20 480, 50 500, 30");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "fill", "none");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "stroke", "#e9be3d");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "stroke-width", "8");
Run Code Online (Sandbox Code Playgroud)
此处的示例与纯 HTML 完美配合。
<svg viewBox="0 0 500 100" class="mktcap_spark">
<polyline
fill="none"
stroke="#e9be3d"
stroke-width="8"
points="
00,120
20,60
40,120
60,10
80,80
100,80
120,60
140,100
160,90
180,80
200, 110
220, 10
240, 70
260, 100
280, 100
300, 40
320, 0
340, 100
360, 100
380, 120
400, 60
420, 70
440, 80
460, 20
480, 50
500, 30
"
/>
</svg>
Run Code Online (Sandbox Code Playgroud)
它呈现出如下所示的迷你图
CSS
.mktcap_spark {
width: 130px;
height: 50px;
min-width: 130px;
}
Run Code Online (Sandbox Code Playgroud)
这里我想到了一些事情。第一个是您创建了一个与已存在的函数相匹配的函数。这不完全是一个可靠的想法——它会崩溃,你会在未来的某个时候哭泣。
接下来是您使用(非 svg)常规 dom 方法createElement- 呃-呃,在使用 svgs 时不能做任何事情,您需要使用 createElementNS 函数。
根据一些旧代码和你的代码,我想出了如下所示的代码:
window.addEventListener('load', onDocLoad, false);
function onDocLoad(evt) {
document.body.appendChild(makeSVG(svgData));
}
var svgData = [{
type: 'svg',
data: {
viewBox: "0 0 500 100"
}
},
{
type: 'polyline',
data: {
fill: "none",
stroke: "#e9be3d",
strokeWidth: "8",
points: "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100"
}
},
];
function getNode(n, v) {
n = document.createElementNS("http://www.w3.org/2000/svg", n);
for (var p in v) {
n.setAttributeNS(null, p.replace(/[A-Z]/g, function(m, p, o, s) {
return "-" + m.toLowerCase();
}), v[p]);
}
return n
}
function makeSVG(data) {
var result;
data.forEach(
function(elem, index, array) {
if (index)
result.appendChild(getNode(elem.type, elem.data));
else
result = getNode(elem.type, elem.data);
}
);
return result;
}Run Code Online (Sandbox Code Playgroud)