【问题标题】:How to add more attributes in tooltip series in Angular NVD3 line chart如何在 Angular NVD3 折线图中的工具提示系列中添加更多属性
【发布时间】:2021-01-11 00:24:06
【问题描述】:

如果可能,我需要在 Angular NVD3 折线图中的工具提示系列中添加更多属性,而无需修改 NVD3 源代码。我知道有类似的帖子,但没有一个涵盖这种情况。

这是选项中的工具提示部分:

interactiveLayer: {
  tooltip: {
    contentGenerator: function (d) {
        
        // output is key, value, color, which is the default for tooltips 
        console.log(JSON.stringify(d.series[0]));
        //{"key":"Name","value":1000,"color":"rgba(255,140,0, 1)"}

        // and I need more attributes to be added
        // into data points, such as label, count, location (see data below)
        //{"key":"Name","value":1000,"color":"rgba(255,140,0, 1), "label" : "some label", "count" : 23, "location" : "Paris"}
    }
  }
}

这是我的数据:

$scope.data =
[
{
  values: FirstGraphPointsArray, 
  key: 'Name',
  color: 'rgba(255,140,0, 1)'
},
{
   values: SecondGraphPointsArray
   key: 'City',
   color: 'rgba(255,140,0, 1)'
}
]

最后是data中数组的结构:

FirstGraphPointsArray -> [{ x: xVariable, y: yVariable, label: labelVariable, count: countVariable, location : locationVariable }, {second element...}, {third element...}];
SecondGraphPointsArray -> [a similar array...]

如何从这些数组中获取更多属性(标签、计数、位置)到 contentGenerator: 函数 (d)。如上所述,我只从函数参数 (d) 中接收默认值

    console.log(JSON.stringify(d.series[0]));
    //{"key":"Name","value":1000,"color":"rgba(255,140,0, 1)"}

【问题讨论】:

  • 您可以将此 contentGenerator 函数与您自己的代码一起使用来生成自定义工具提示,以更改工具提示中的 HTML 结构。您可以“克隆”原始 HTML,并在工具提示表中包含新的 和 以获取其他参数。似乎是更改工具提示中包含的默认参数/值数量的唯一方法。你试过这种方法吗?
  • 感谢您的 cmets。下面我分享一下我想出的答案。
  • 不客气!很高兴知道您找到了解决方案。

标签: tooltip nvd3.js angular-nvd3


【解决方案1】:

我想出了一个解决方案并想分享它,以防其他人遇到相同的任务。我最终通过默认路由访问了一些来自 d 的参数 - function(d),而一些自定义的 - 直接来自 $scope.data。

重要:使用d.index,表示数据点在列表中的位置很关键。这确保对于任何给定的索引,从 function(d) 中提取的参数和直接提取的参数属于同一数据点(参见下面的代码)。

interactiveLayer: {
  tooltip: {
    contentGenerator: function (d) {
      var customTooltipcontent = "<h6 style='font-weight:bold'>" + d.value + "</h6>";

    customTooltipcontent += "<table class='custom-tooltip-table'>";
    customTooltipcontent += "<tr style='border-bottom: 1px solid green;'><td></td><td>Name</td><td>Value</td><td>Count</td></tr>";
    for (var i = 0; i < d.series.length; i++) {
      customTooltipcontent += "<tr><td><div style='width:10px; height:10px; background:" + d.series[i].color + "'></div></td><td>" + d.series[i].key + "</td><td>" + d.series[i].value + "</td><td>" + $scope.data[0].values[d.index].count + "</td><td>" + $scope.data[0].values[d.index].location + "</td></tr>"
    }
    customTooltipcontent += "</table>";

    return (customTooltipcontent);
    }
   }
 }

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签