我已经使用D3js制作了各种甜甜圈图,我需要在甜甜圈中插入一个相同的图像,但是我不知道实现此目的的方法。谁能帮我这个?这是我的JS代码:
var data1 = [4,96];
var data2 = [1,99];
var data3 = [16,84];
var data4 = [12,88];
var data5 = [29,71];
var data6 = [15,85];
var data7 = [12,88];
var data8 = [10,90];
/* Reusable Drawing donut function*/
var width = 300,
height = 300,
radius = (Math.min(width, height) / 2) - 100;
function drawDonut(dataa,divchart){
var sym = "%"
var color = ["#00338D","#BC204B"];
var pie = d3.pie()
.value(function(d) { return d })(dataa);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - (radius/1.9));
var labelArc = d3.arc()
.outerRadius(radius - 31)
.innerRadius(radius - 31);
var svg = d3.select(divchart)
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" +60 + "," + 60 +")");
var g = svg.selectAll("arc")
.data(pie)
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.data(color)
.style("fill", function(d){return d});
}
drawDonut(data1,"#pie1")
drawDonut(data2,"#pie2")
drawDonut(data3,"#pie3")
drawDonut(data4,"#pie4")
drawDonut(data5,"#pie5")
drawDonut(data6,"#pie6")
drawDonut(data7,"#pie7")
drawDonut(data8,"#pie8")
我要插入的图像是一个svg,这是代码:
<g>
<path class="st0" d="M15.6,10.9c1.3,0,2.4-1.2,2.4-2.7c0-1.5-1.1-2.7-2.4-2.7c-1.3,0-2.4,1.2-2.4,2.7C13.2,9.7,14.2,10.9,15.6,10.9
L15.6,10.9z"/>
<path class="st0" d="M18.6,11.6h-1.2l-1.8,5.5l-1.8-5.5h-1.2c-1.3,0-2.4,1.2-2.4,2.7v13h2.4l1.2,16.4h3.6l1.2-16.4H21v-13
C21,12.8,19.9,11.6,18.6,11.6L18.6,11.6z"/>
<path class="st0" d="M31.9,10.9c1.3,0,2.4-1.2,2.4-2.7c0-1.5-1.1-2.7-2.4-2.7c-1.3,0-2.4,1.2-2.4,2.7C29.5,9.7,30.6,10.9,31.9,10.9
L31.9,10.9z"/>
<path class="st0" d="M39.8,25.2l-3.6-11.6c0,0-0.6-2-2.4-2h-3.6c-1.8,0-2.4,2-2.4,2l-3.6,11.6l1.2,0.7l4.2-9.5l-3.6,14.3h3.6
l1.2,13h2.4l1.2-13H38l-3.6-14.3l4.2,9.5L39.8,25.2L39.8,25.2z"/>
</g>
请问我能帮助decifer如何在甜甜圈图的孔中插入图像吗?谢谢
答案 0 :(得分:1)
您可以将每个d
属性存储为一个单独的常量,并使用与父级相同的组选择将这些路径分别附加在drawDonut
函数内部。
但是,一个更简单的解决方案是将所有内容保存为单个字符串...
var myGroup = '<g><path class="st0" d="M15.6,10.9c1.3,0,2.4-1.2,2.4-2.7c0-1.5-1.1-
-2.7c-1.3,0-2.4,1.2-2.4,2.7...etc...-14.3l4.2,9.5L39.8,25.2L39.8,25.2z"/></g>';
....并使用html
方法将其附加:
g.append("g").html(myGroup);
注意事项:html()
方法在内部使用innerHTML
。在旧版浏览器中,这不适用于SVG元素。据阿米莉亚·贝拉米·罗伊兹(Amelia Bellamy-Royds)在她的《 Using SVG with CCS3 and HTML5 (2018)》中:
最新版本的Web浏览器甚至在SVG元素上支持
innerHTML
,但这是核心DOM规范的最新内容。
为避免在另一个组内形成不必要的组,您还可以删除字符串中的<g>
和</g>
。
这是演示:
var myGroup = '<g><path class="st0" d="M15.6,10.9c1.3,0,2.4-1.2,2.4-2.7c0-1.5-1.1-2.7-2.4-2.7c-1.3,0-2.4,1.2-2.4,2.7C13.2,9.7,14.2,10.9,15.6,10.9L15.6,10.9z"/><path class="st0" d="M18.6,11.6h-1.2l-1.8,5.5l-1.8-5.5h-1.2c-1.3,0-2.4,1.2-2.4,2.7v13h2.4l1.2,16.4h3.6l1.2-16.4H21v-13C21,12.8,19.9,11.6,18.6,11.6L18.6,11.6z"/><path class="st0" d="M31.9,10.9c1.3,0,2.4-1.2,2.4-2.7c0-1.5-1.1-2.7-2.4-2.7c-1.3,0-2.4,1.2-2.4,2.7C29.5,9.7,30.6,10.9,31.9,10.9L31.9,10.9z"/><path class="st0" d="M39.8,25.2l-3.6-11.6c0,0-0.6-2-2.4-2h-3.6c-1.8,0-2.4,2-2.4,2l-3.6,11.6l1.2,0.7l4.2-9.5l-3.6,14.3h3.6l1.2,13h2.4l1.2-13H38l-3.6-14.3l4.2,9.5L39.8,25.2L39.8,25.2z"/></g>';
var data1 = [4, 96];
var data2 = [1, 99];
var data3 = [16, 84];
var data4 = [12, 88];
var data5 = [29, 71];
var data6 = [15, 85];
var data7 = [12, 88];
var data8 = [10, 90];
/* Reusable Drawing donut function*/
var width = 100,
height = 100,
radius = (Math.min(width, height) / 2);
function drawDonut(dataa, divchart) {
var sym = "%"
var color = ["#00338D", "#BC204B"];
var pie = d3.pie()
.value(function(d) {
return d
})(dataa);
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(radius - (radius / 1.9));
var labelArc = d3.arc()
.outerRadius(radius - 31)
.innerRadius(radius - 31);
var svg = d3.select(divchart)
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + 50 + "," + 50 + ")");
var g = svg.selectAll("arc")
.data(pie)
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.data(color)
.style("fill", function(d) {
return d
});
g.append("g")
.attr("transform", "translate(-15,-15) scale(0.6)")
.html(myGroup);
}
drawDonut(data1, "#pie1")
drawDonut(data2, "#pie2")
drawDonut(data3, "#pie3")
drawDonut(data4, "#pie4")
drawDonut(data5, "#pie5")
drawDonut(data6, "#pie6")
drawDonut(data7, "#pie7")
drawDonut(data8, "#pie8")
div {
display: inline;
}
<script src="https://d3js.org/d3.v5.min.js"></script>
<div id="pie1"></div>
<div id="pie2"></div>
<div id="pie3"></div>
<div id="pie4"></div>
<div id="pie5"></div>
<div id="pie6"></div>
<div id="pie7"></div>
<div id="pie1"></div>
<div id="pie8"></div>
PS:变换中有一些魔术数字。您可以通过计算组的大小和初始绘制点来避免它们。