我想添加一个图片在nvd3分段传奇,而不是文字。我有这段代码,但它只更改了工具提示中的文本和图像。我想改变和图例中的文字具有相同的形象。
var testdata = [
{
key: "<img src="+"./imgs/facebook.jpg"+">",
y: 1
},
{
key: ""<img src="+"./imgs/twitter.jpg"+">"",
y: 2
} ];
nv.addGraph(function() {
var chart = nv.models.pieChart()
.x(function(d) { return d.key})
.labelThreshold(.08)
.showLabels(true)
.color(d3.scale.category10().range())
.donut(true);
chart.pie.donutLabelsOutside(true).donut(true);
d3.select("#mypiechart")
.datum(testdata)
.transition().duration(1200)
.call(chart);
return chart;});有什么想法吗?
发布于 2013-12-03 18:34:22
这在nvd3.js中目前是不可能的。工具提示之所以有效,是因为您指定的img元素被设置为不包含在svg元素中的div。它不适用于图例或图表标签,因为它们是使用svg text元素构建的。为了在图表svg中显示图像,我们需要使用svg image元素。
如果我们破解nvd3.js代码,我们可以构建svg image元素。这是一个大纲,你可以做什么,以使传奇发挥作用。然后,您可以决定是要在图表标签的nv.models.pie代码中尝试类似的东西,还是只想在图表配置中将chart.showLabels设置为false。
在数据中添加一个新键以提供图像路径:
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.legend代码以显示图像:
seriesEnter.append('circle')
.style('stroke-width', 2)
.attr('class','nv-legend-symbol')
.attr('r', 5);
// Add an svg image into the legend
seriesEnter.append('image')
.attr('xlink:href', function(d) { return d.image_path} )
.attr('height',20)
.attr('width',20)
.attr('y', '-10')
.attr('x', '8');更新nv.models.legend代码以不显示键:
// Don't add the key value into the legend text
//series.select('text').text(getKey);在确定图例布局时,更新nv.models.legend代码以考虑图像宽度:
//seriesWidths.push(nodeTextLength + 28); // 28 is ~ the width of the circle plus some padding
//Include image width...
seriesWidths.push(nodeTextLength + 48);https://stackoverflow.com/questions/20315208
复制相似问题