【问题标题】:How can I style a tooltip for an NVD3 Chart?如何为 NVD3 图表设置工具提示样式?
【发布时间】:2016-10-19 23:31:21
【问题描述】:

引用 NVD3 框架。我正在尝试为下面列出的以下饼图添加自定义工具提示:

var app = angular.module('plunker', ['nvd3']);

app.controller('MainCtrl', function($scope) {
  $scope.options = {
        chart: {
            type: 'pieChart',
            height: 500,
            x: function(d){return d.key;},
            y: function(d){return d.y;},
            color:['#CE1B1F', '#FFC455', '#00A6CD'],
            showLabels: false,
            duration: 500,
            labelThreshold: 0.01,
            labelSunbeamLayout: true,
            legend: {
                margin: {
                    top: 5,
                    right: 35,
                    bottom: 5,
                    left: 0
                }
            }
        }
    };

    $scope.data = [
        {
            key: "A",
            y: 2
        },
        {
            key: "B",
            y: 1
        },
        {
            key: "C",
            y: 3
        },
    ];
});

由于我只是使用 Krispo 的 [github][1] 中的示例,因此我不确定如何自定义工具提示,使其类似于以下内容:

【问题讨论】:

    标签: javascript angularjs d3.js nvd3.js


    【解决方案1】:

    要添加自定义工具提示,您需要将tooltip 添加到现有的 nvd3 选项中,如下所示:

    tooltip: {
         contentGenerator: function (e) {
               //Create and return desired tool-tip as html using e.series and e.data
         }
    }
    

    如果您需要为每个系列使用一些额外的值或属性,您可以在 $scope.data 中像这样定义它们:

    $scope.data = [
            {
                key: "CAT I",
                y: 2,
                MyAttribute1:"DLA Avn ... CAT I",
                MyAttribute2:"DLA Energy ... CAT I"
            },
            {
                key: "CAT II",
                y: 3,
                MyAttribute1:"DLA Avn ... CAT II",
                MyAttribute2:"DLA Energy ... CAT II"
            },
            {
                key: "CAT III",
                y: 1,
                MyAttribute1:"DLA Avn ... CAT III",
                MyAttribute2:"DLA Energy ... CAT III"
            },
        ];
    

    现在您可以使用e.data 访问工具提示函数中的自定义值,如下所示:

    tooltip: {
             contentGenerator: function (e) {
                  var series = e.series[0];
                  if (series.value === null) return;
    
                  var rows = 
                    "<tr>" +
                      "<td class='key'>" + series.key + '- #3: ' + "</td>" +
                      "<td class='x-value'>" + e.data.MyAttribute1 + "</td>" + 
                    "</tr>" +
                    "<tr>" +
                      "<td class='key'>" + series.key + '- #5: ' + "</td>" +
                      "<td class='x-value'>" + e.data.MyAttribute2 + "</td>" + 
                    "</tr>";
    
                  var header = 
                    "<thead>" + 
                      "<tr>" +
                        "<td class='legend-color-guide'><div style='background-color: " + series.color + ";'></div></td>" +
                        "<td class='key'><strong>" + series.key + "</strong></td>" +
                      "</tr>" + 
                    "</thead>";
    
                  return "<table>" +
                      header +
                      "<tbody>" + 
                        rows + 
                      "</tbody>" +
                    "</table>";
                } 
    }
    

    有一个 Edited Plunker 向您展示如何做到这一点。

    希望有帮助。

    【讨论】:

    • 这绝对是我想要的。根据我的理解,您能解释一下信息是如何传递到函数参数“e”中的吗?
    • @KunalOjha,当鼠标悬停在任何seri上时,nvd3将seri信息和数据作为e.series[0]和e.data传递,e.series[0]包含键,该系列的值和颜色,并且 e.data 包含您在 nvd3 数据中设置的任何值或属性。
    • 按系列您指的是饼图的每个单独的楔形吗?
    • @KunalOjha,没错
    • @hsh 请你帮忙回答这个问题是不是 nvd3 相关stackoverflow.com/questions/40709470/…
    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多