How can I specify a custom XML/SVG namespace with D3.js?

心不动则不痛 提交于 2019-12-11 07:43:15

问题


I am trying to define my own namespace inside a SVG so I can use my own tags and attributes. If I understand correctly, the SVG should look like this:

<svg xmlns:myns="http://www.example.com/whatever/">
  <g myns:mycustomattr="123">
    ... 

I am adding root SVG element through D3, but adding a namespace to it with attr fails:

var svg = d3.select("#container")
  .append("svg")
    .attr('xmlns:myns', 'http://www.example.com/whatever/') 

The code above results in: NamespaceError: An attempt was made to create or change an object in a way which is incorrect with regard to namespaces

What would be the correct way to add namespace to SVG using D3.js?


回答1:


You just set d3.ns.prefix

d3.ns.prefix.myns = "http://www.example.com/whatever/";

Then use it:

var svg = d3.select("#container")
  .append("svg")
    .attr('myns:someAttribute', 'some value');


来源:https://stackoverflow.com/questions/20264697/how-can-i-specify-a-custom-xml-svg-namespace-with-d3-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!