我绘制了散点图,但我只想替换刻度标签。它不应该再次重新绘制。我使用强制定向散点图,它需要x和y轴的整数,在绘图之后我想要替换y轴标签。这是plunker
https://plnkr.co/edit/ufnBOwQEoTOrP1mUTKhC?p=preview
我想将d.sepalLength的名称替换为d.name。警报显示正确的值,但它们未在y轴上正确附加。添加以下代码以替换刻度标签。请帮忙
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.extent(data, function(d) { alert(d.name);return d.name; }));
答案 0 :(得分:3)
蛮力方式是搜索数据中是否存在将在数据中呈现的值,如果是,则将其替换为名称
var data =[
{sepalLength: "0", sepalWidth: 3.5, name: "AT"},
{sepalLength: "0", sepalWidth: 3.5, name: "AT"},
{sepalLength: "0", sepalWidth: 3.5, name: "AT"},
{sepalLength: "0", sepalWidth: 3.5, name: "AT"},
{sepalLength: "1", sepalWidth: 3.0, name: "AS"},
{sepalLength: "2", sepalWidth: 2.5, name: "AM"},
{sepalLength: "3", sepalWidth: 2.0, name: "CS"},
{sepalLength: "4", sepalWidth: 1.5, name: "CT"},
{sepalLength: "5", sepalWidth: 1.0, name: "CX"}
]
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d, i) {
var inData = data.filter(function (v) { return +v.sepalLength === d })
return inData.length ? inData[0].name : d
});
要获得更强大的解决方案,您应根据scale.tickValues
sepalLength
设置定价的固定值