首页 文章

如何在饼图中为angular-nvd3自定义图例文字?

提问于
浏览
1

我在angular-nvd3中有一个饼图 . https://krispo.github.io/angular-nvd3/#/pieChart我遇到的问题是饼图,我有5个切片,当所有的饼切片都贴上标签时,它看起来非常紧密 . 我想修改图例,以便不是只显示键,而是想显示:

<key> + <number of records in that key>

使用“labelType”我可以更改切片饼图标签上显示的内容,但是如何更改图例中显示的内容?

1 回答

  • 2

    无法在API中找到任何内容来执行此操作 .

    但是通过d3做这件事有点麻烦:

    渲染后

    1)获取所有文本DOM

    2)对所有文本运行for循环 .

    3)获取文本的数据并更改内部HTML .

    dispatch: {
        renderEnd: function(e) {
          //for each text
          d3.selectAll(".nv-legend text")[0].forEach(function(d){
            //get the data
            var t= d3.select(d).data()[0];
            //set the new data in the innerhtml
            d3.select(d).html(t.key + " - " + t.y);
          });
        }
      }
    

    工作代码here

相关问题