首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在nvd3分段图例中添加图像?

问如何在nvd3分段图例中添加图像?
EN

Stack Overflow用户
提问于 2013-12-01 17:44:52
回答 1查看 1.4K关注 0票数 1

我想添加一个图片在nvd3分段传奇,而不是文字。我有这段代码,但它只更改了工具提示中的文本和图像。我想改变和图例中的文字具有相同的形象。

代码语言:javascript
复制
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;});

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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。

在数据中添加一个新键以提供图像路径:

代码语言:javascript
复制
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代码以显示图像:

代码语言:javascript
复制
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代码以不显示键:

代码语言:javascript
复制
// Don't add the key value into the legend text
//series.select('text').text(getKey);

在确定图例布局时,更新nv.models.legend代码以考虑图像宽度:

代码语言:javascript
复制
//seriesWidths.push(nodeTextLength + 28); // 28 is ~ the width of the circle plus some padding
//Include image width...
seriesWidths.push(nodeTextLength + 48);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20315208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档