甜甜圈饼图 - 添加标题 - NVd3.js

Eni*_*aRM 3 javascript d3.js pie-chart nvd3.js

我正在探索NVD3.js库.令我惊讶的是,它能在多快的速度下生成.

但有时似乎很难改变图表.在这种情况下,我想为我的图表添加标题.

另外,我想在工具提示中添加其他数据.所以在悬停时,它还会在我的数据中包含注释.

数据样本:

var data = [
{
  key: "Loans",
  y: 52.24
  note: "Expect greatest value"
}];
Run Code Online (Sandbox Code Playgroud)

这是我正在玩的代码:

nv.addGraph(function() {

var width = 500,
    height = 500;

var chart = nv.models.pieChart()
    .x(function(d) { return d.key; })
    .values(function(d) { return d; })
    .color(d3.scale.category10().range())
    .width(width)
    .height(height)
    .donut(true);

chart.pie
    .startAngle(function(d) { return d.startAngle/2 -Math.PI/2; })
    .endAngle(function(d) { return d.endAngle/2 -Math.PI/2 ;});


  d3.select("#chart")
      //.datum(historicalBarChart)
      .datum([data])
    .transition().duration(1200)
      .attr('width', width)
      .attr('height', height)
      .call(chart);

return chart;
});
Run Code Online (Sandbox Code Playgroud)

甜甜圈 - 饼图示例


更新: 工具提示的原始代码位于src->models->pieChart.js:

tooltip = function(key, y, e, graph) {
    return '<h3>' + key + '</h3>' +
           '<p> Confidence: ' +  y + '%</p>'
  }
Run Code Online (Sandbox Code Playgroud)

我已经尝试用我自己的功能来覆盖它.但要么得到错误,要么没有改变.

标题更新:我通常使用以下代码(或类似的东西)作为标题.

svg.append("text")
    .attr("x", (width / 2))
    .attr("y", 0 - (margin.top / 2))
    .attr("text-anchor", "middle")
    .style("font-size", "16px")
    .style("text-decoration", "underline")
    .text("Awesome Title");
Run Code Online (Sandbox Code Playgroud)

但是,当然,这在NVD3中无效.我不知道用什么函数来指定标题.

Wol*_*des 5

我想你在寻找图表.tooltipContent()JSFiddle:http://jsbin.com/idosop/7/edit

      var tp = function(key, y, e, graph) {
            console.log(key, e, y);
            return '<h3>' + key + '</h3>' +
                   '<p>!!' +  y + '!!</p>' +
              '<p>Likes: ' +  e.point.likes + '</p>';
      };
      chart.tooltipContent(tp);
Run Code Online (Sandbox Code Playgroud)