【问题标题】:nvd3 piechart.js - How to edit the tooltip? version 1.8nvd3 piechart.js - 如何编辑工具提示? 1.8版
【发布时间】:2016-01-07 17:51:44
【问题描述】:

我正在使用 nvd3 的饼图。最近我将 nvd3 升级到 1.8.1,我需要overwrite tooltip。这是我的旧版本代码:

piechartCurrent.tooltip.contentGenerator(function (obj) {
        content = '<h3 style="background-color: ';
        content += obj.color + '">';
        content += obj.data.key + '</h3><p>$' +  Math.round(obj.data.y * 100) / 100 + ' ('+ obj.data.symbols +')</p>';
        return content;
    });  

看起来像这样:

但我想为工具提示使用新样式:

什么是新工具提示的 html 代码?我需要覆盖工具提示,因为我需要一些自定义文本。

编辑:
Jsfiddle example

编辑 2: Jsfiddle with symbols.

【问题讨论】:

  • 能否把你的代码sn-p放在问题里,这样才能给出正确答案。

标签: nvd3.js


【解决方案1】:

如果您使用NVD3 v1.8.1,工具提示标题将默认隐藏。但是试试这个:

piechartCurrent.tooltip.headerEnabled(false);

这里是它的source的链接

更新答案

您需要删除chart.tooltip.contentGenerator() 函数才能使用默认样式,您一直在覆盖默认样式。

如果您想覆盖,工具提示中有两个部分。关键和价值。 Key 是图表元素的显示位置,例如:“Finance”

chart.tooltip.keyFormatter(function (d, i) {
    var symbol = '';

    pieSectorData().forEach(function (entry) {
       // Search data for key and return the symbols
       if (entry.key == d){
           symbol = entry.symbols
       }
    });
    return  d + '(' + symbol + ')'
});

要在工具提示中使用$ 符号格式化您的,请使用以下代码:

chart.tooltip.valueFormatter(function (d, i) {
    return '$' + d
});

希望对你有帮助。

【讨论】:

  • 如果标题被禁用,那么我只需要填充颜色的方块的代码。那个方形彩盒怎么做?
  • 你能把你的代码放在JSFiddle里吗,我们可以看看。
  • 对。抱歉,我没有将它包含在我的第一个 jsfiddle 中,但我还需要添加更多信息。符号列表。这里是用符号 jsfiddle.net/25b3rvL8/2 编辑的 jsfiddle 我在参数中检查了 d nad i,但 d 只是值,i 是 0。
  • @Lucas03 我已经用keyFormatter 函数更新了答案。
  • 好的,谢谢!可能key = entry.symbols 应该是symbol = entry.symbols
【解决方案2】:

出于某种原因,shabeer90 的回答毕竟对我不起作用。我正在工作/更改 js 文件中的符号,但没有任何效果。所以我使用了我的原始代码tooltip.contentGenerator。我用 div 对颜色进行了简单的硬编码:
样式.css:

.square-box {
float: left;
width: 10px;
height: 10px;
margin: 4px;
border-width: 1px;
border-style: solid;
border-color: rgba(0,0,0,.2);
}

和js编辑图表的提示:

chart.tooltip.contentGenerator(function (obj) {
        content = '<p><div class="square-box" style="background-color:'+obj.color+';"></div> '+
                obj.data.key +' ('+ obj.data.symbols +') <strong>$' +  Math.round(obj.data.y * 100) / 100 + '</strong> </p>';
        return content;
    });

【讨论】:

  • 这是您的代码的working version,其中包含我的答案。它使用keyFormatter 函数根据需要显示工具提示。有点惊讶它不适合你。还是我错过了什么?
  • 您的代码有效,但就像我说的,我正在使用 javascript 更改 pieSectorData() 中的符号,而您的代码以某种方式破坏了它。 (我只是在这里添加了这个作为替代方案,但我认为你的答案更好)
猜你喜欢
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多