我想在nvd3饼图切片中添加一个图标。 我想要这个:
任何帮助?
编辑:
饼图的html代码是:
<g class="nv-pieWrap">
<g class="nvd3 nv-wrap nv-pie nv-chart-9221" transform="translate(0,0)">
<g>
<g class="nv-pie" transform="translate(270,145.5)">
<g class="nv-slice" fill="#3b5998" stroke="#3b5998"><path d="M7.127208979246237e-15,-116.4A116.4,116.4 0 0,1 115.55131294501349,-14.030469581719618L72.21957059063342,-8.769043488574761A72.75,72.75 0 0,0 4.454505612028897e-15,-72.75Z"></path></g>
<g class="nv-slice" fill="#326ada" stroke="#326ada"><path d="M115.55131294501349,-14.030469581719618A116.4,116.4 0 1,1 -1.2476559499083328e-13,-116.4L-7.79784968692708e-14,-72.75A72.75,72.75 0 1,0 72.21957059063342,-8.769043488574761Z"></path></g>
</g>
<g class="nv-pieLabels" transform="translate(270,145.5)"></g></g></g></g>
阅读此How can i add an image in nvd3 piechart legend?
后我在我的数据中添加了一个image_path,并添加了js这段代码
var slices = wrap.select('.nv-pie').selectAll('.nv-slice')
.data(pie);
var ae = slices.enter().append('g')
.attr('class', 'nv-slice')
.on('mouseover', function(d,i){
d3.select(this).classed('hover', true);
dispatch.elementMouseover({
label: getX(d.data),
value: getY(d.data),
point: d.data,
pointIndex: i,
pos: [d3.event.pageX, d3.event.pageY],
id: id
});
})
.on('mouseout', function(d,i){
d3.select(this).classed('hover', false);
dispatch.elementMouseout({
label: getX(d.data),
value: getY(d.data),
point: d.data,
index: i,
id: id
});
})
.on('click', function(d,i) {
dispatch.elementClick({
label: getX(d.data),
value: getY(d.data),
point: d.data,
index: i,
pos: d3.event,
id: id
});
d3.event.stopPropagation();
})
.on('dblclick', function(d,i) {
dispatch.elementDblClick({
label: getX(d.data),
value: getY(d.data),
point: d.data,
index: i,
pos: d3.event,
id: id
});
d3.event.stopPropagation();
});
/////////////I ADDED THIS APPEND
ae.append('image')
.attr('xlink:href', function(d) { return d.image_path} )
.attr('height',16)
.attr('width',16)
.attr('y', '-10')
.attr('x', '8');
///////////////////////////////
slices
.attr('fill', function(d,i) { return color(d, i); })
.attr('stroke', function(d,i) { return color(d, i); });
var paths = ae.append('path')
.each(function(d) { this._current = d; });
//.attr('d', arc);
答案 0 :(得分:4)
如果您想破解nv.d3.js
代码以允许此操作,则只需几步即可。
定义数据集中每个元素的图像路径:
var testdata = [
{
key: "<img src="+"./imgs/facebook.jpg"+">",
y: 1,
image_path: "./imgs/facebook.jpg"
},
{
key: "<img src="+"./imgs/twitter.jpg"+">",
y: 2,
image_path: "./imgs/twitter.jpg"
}];
然后使用类似的内容更新nv.models.pie
代码......
为图像添加锚点:
pieLabels.enter().append("g").classed("nv-label",true)
...
group.append('text')
.style('text-anchor', labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle') //center the text on it's origin or begin/end if orthogonal aligned
.style('fill', '#000')
// Set up the anchor for the image
group.append('image')
.attr('height', 20)
.attr('width', 20)
.attr('y', '-10')
.attr('x', '-10');
隐藏文字标签并显示图片:
// Don't insert the label text. We're using images.
// pieLabels.select(".nv-label text")
// .style('text-anchor', labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle') //center the text on it's origin or begin/end if orthogonal aligned
// .text(function(d, i) {
// var percent = (d.endAngle - d.startAngle) / (2 * Math.PI);
// var labelTypes = {
// "key" : getX(d.data),
// "value": getY(d.data),
// "percent": d3.format('%')(percent)
// };
// return (d.value && percent > labelThreshold) ? labelTypes[labelType] : '';
// });
// Show the image if the slice is large enough
pieLabels.select(".nv-label image")
.attr('xlink:href', function(d, i) {
var percent = (d.endAngle - d.startAngle) / (2 * Math.PI);
return (d.data.image_path && percent > labelThreshold) ? d.data.image_path : '';
});
您可能需要根据需要调整一些数字。