gpt4 book ai didi

javascript - 在 NVD3 scatterChart 上绘制标签

转载 作者:行者123 更新时间:2023-11-28 00:29:20 24 4
gpt4 key购买 nike

如果我有 http://nvd3.org/examples/scatter.html 示例中的以下代码,我可以为绘制的点添加标签吗?不是工具提示,但将我的数据中的值之一呈现为始终附加到该点的标签?显然,在这个特定的示例中这样做会太拥挤,但在人口较少的图表中,如果每个点都有一个名称,那么最好进行标记。谢谢!

  nv.addGraph(function() {
var chart = nv.models.scatterChart()
.showDistX(true) //showDist, when true, will display those little distribution lines on the axis.
.showDistY(true)
.transitionDuration(350)
.color(d3.scale.category10().range());

//Configure how the tooltip looks.
chart.tooltipContent(function(key) {
return '<h3>' + key + '</h3>';
});

//Axis settings
chart.xAxis.tickFormat(d3.format('.02f'));
chart.yAxis.tickFormat(d3.format('.02f'));

//We want to show shapes other than circles.
chart.scatter.onlyCircles(false);

var myData = randomData(4,40);
d3.select('#chart svg')
.datum(myData)
.call(chart);

nv.utils.windowResize(chart.update);

return chart;
});

/**************************************
* Simple test data generator
*/
function randomData(groups, points) { //# groups,# points per group
var data = [],
shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
random = d3.random.normal();

for (i = 0; i < groups; i++) {
data.push({
key: 'Group ' + i,
values: []
});

for (j = 0; j < points; j++) {
data[i].values.push({
x: random()
, y: random()
, size: Math.random() //Configure the size of each scatter point
, shape: (Math.random() > 0.95) ? shapes[j % 6] : "circle" //Configure the shape of each scatter point.
});
}
}

return data;
}

最佳答案

您可以只添加标签。这很俗气,但提供了一个想法:

var svg = d3.select('svg');
svg.selectAll('path')
.each(function(scatterpoint) {
svg.append('text')
.x(scatterpoint.x)
.y(scatterpoint.y)
.text(scatterpoint.maybe_you_have_text_here);
})

关于javascript - 在 NVD3 scatterChart 上绘制标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29158538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com