嗨,我绘制了5个x线和5个y线的网格。问题在于,在svg中,y方向向下倾斜,所以我的网格y线向下倾斜,以获得向上方向的网格,我给出了在此之后的y值(例如0,-50,-100,-150,-200)我得到了我所期望的,但我不需要为-y值。我怎么能得到y线向上 我第一次挣扎做第二?如何在d3.js中反转网格
var Y = [0,50,100,150,200];
var X = [0,50,100,150,200];
var min_x = 0;
var max_x = 200;
var min_y = 0;
var max_y = 200;
var svgContainer = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500);
var yLinesGroup = svgContainer.append("g")
.attr("id", "ylines");
var ylines = yLinesGroup.selectAll("line")
.data(Y)
.enter()
.append("line");
var ylinesAttributes = ylines
.attr("x1", min_x - 30)
.attr("y1", function (d) { return (d); })
.attr("x2", max_x + 30)
.attr("y2", function (d) { return (d); })
.style("stroke", "red")
ylines
.on("mouseover", function() {
var pos = d3.mouse(this);
d3.select(this).style("stroke", "black")
.append("svg:title")
.text("X:" + pos[0] + ";Y:" + pos[1]);
})
.on("mouseout", function() {
d3.select(this).style("stroke", "gray").text("");
});
var xLinesGroup = svgContainer.append("g").attr("id", "xlines");
var xlines = xLinesGroup.selectAll("line")
.data(X)
.enter()
.append("line");
var xlinesAttributes = xlines
.attr("x1", function (d) { return d; })
.attr("y1", min_y - 30)
.attr("x2", function (d) { return d; })
.attr("y2", max_y + 30)
.style("stroke", "green")
xlines
.on("mouseover", function() {
var pos = d3.mouse(this);
d3.select(this).style("stroke", "black")
.append("svg:title")
.text("X:" + pos[0] + ";Y:" + pos[1]);
})
.on("mouseout", function() {
d3.select(this).style("stroke", "gray").text("");
});
如果您想要一组输入数据来映射一系列输出值,那么比例尺通常是最佳解决方案。 例如,使用d3.scaleLinear.domain([50,400])。range([400,50])可能是值得的(请参阅此处更多https://github.com/d3/d3-scale) –