【发布时间】:2014-02-03 16:14:40
【问题描述】:
我使用 NVD3 作为散点图,但是当悬停在工具提示上时,我想要标签而不是键。
这是我的 json:
long_data = [
{
key: 'PC1',
color: '#00cc00',
values: [
{
"label" : "Lichtpuntje" ,
"x" : 11.16,
"y" : -0.99,
"size":1000,
"color": '#FFCCOO'
} ,
{ ....
这是js
nv.addGraph(function() {
chart = nv.models.scatterChart()
.showDistX(true)
.showDistY(true)
.useVoronoi(true)
.color(d3.scale.category10().range())
.transitionDuration(300)
...
chart.xAxis.tickFormat(d3.format('.02f'));
chart.yAxis.tickFormat(d3.format('.02f'));
chart.tooltipContent(function(i) { return labelArray[i]; });
d3.select('#test1 svg')
.datum(long_data)
.call(chart);
...
我怎样才能让工具提示具有标签值?或者我怎样才能将 i 作为索引参数?
【问题讨论】:
-
您已经在使用
.tooltipContent()。根据您的描述,听起来该功能应该类似于function(d) { return d.label; }。这不适合你吗? -
chart.tooltipContent(function(d) { return d.label; }); d 给了我密钥(“PC1”),所以 d.label 是未定义的
-
啊,对了,看起来确实好像您只是通过了密钥。这意味着您必须修改 NVD3 源来传递值。
-
This answer 讨论了这个问题,并在您只有一个数据系列时给出了解决方案(您将数据格式更改为将每个数据点放在一个单独的系列中)。
-
P.S 工具提示格式化函数实际上得到了 三个 参数,其他的是 x 和 y 值。这并不理想,但您可以使用系列、x 和 y 值来过滤数据数组并返回正确的数据元素,包括其标签。