【问题标题】:SVG polyline will not show points when elements are created with JavaScript使用 JavaScript 创建元素时,SVG 折线不会显示点
【发布时间】:2019-02-22 17:11:34
【问题描述】:

我需要使用 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 问题中添加积分,这给了我一个每周_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>

它会呈现如下所示的迷你图

CSS

.mktcap_spark {

  width: 130px;
  height: 50px;
  min-width: 130px;
}

【问题讨论】:

  • 3 年后我又遇到了同样的问题,我用谷歌搜索它并在谷歌上找到我自己的问题作为口渴...... gg

标签: javascript html svg polyline


【解决方案1】:

这里想到了几件事。第一个是您创建了一个与已经存在的函数相匹配的函数。不完全是一个可靠的想法 - 它会崩溃,你会在未来的某个时候哭泣。

接下来就是你使用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;
}

【讨论】:

  • 现在我完全记得我为什么做这个函数了,它是为了避免'viewBox'到view-box和'strokeWidth'到'stroke-width'的问题(viewBox会变成视图-box 在您的示例中不起作用)....顺便说一句,该功能从未损坏,它运行平稳,易于使用且可以正常工作。无论如何,感谢您的回答,它让我意识到我做错了什么。
猜你喜欢
  • 2021-04-25
  • 1970-01-01
  • 2014-11-14
  • 2019-11-30
  • 2015-07-16
  • 2019-05-24
  • 1970-01-01
  • 2017-01-26
  • 2023-04-07
相关资源
最近更新 更多