使用JavaScript创建元素时,SVG折线不会显示点

问题描述 投票:0回答:1

我需要使用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);
    }

在这里,我创建了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");

在这里,我创建折线元素并将其添加到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");

上面没有像我期望的那样呈现SVG迷你图,但是所有属性都被添加,但没有显示。

我也尝试过这种方式从this问题中添加点,这给了我一个周期错误:weekly_svg.points未定义

            var point = weekly_svg.createSVGPoint();
            point.x = 10;
            point.y = 20;
            weekly_poly.points.appendItem(point);

我也研究了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");

此示例在此处与纯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>

它呈现出这样的迷你线

enter image description here

CSS

.mktcap_spark {

  width: 130px;
  height: 50px;
  min-width: 130px;
}
javascript html html5 svg polyline
1个回答
4
投票

这里有几件事情浮现在脑海中。第一个是您创建了一个与已存在的函数匹配的函数。这不是一个强大的想法 - 它会破裂,你会在未来的某个时候哭泣。

接下来是你使用createElement的(非svg)常规dom方法 - 呃呃,使用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;
}
© www.soinside.com 2019 - 2024. All rights reserved.