【发布时间】:2017-12-03 07:52:59
【问题描述】:
我正在尝试创建一个 2D 笛卡尔坐标系,其原点位于 svg 的中心。基本上是这样的: 我目前的方法是平移轴,使它们的原点位于中间。
// Add the x Axis
svg.append("g")
.attr("transform", "translate(" + 0 + "," + height/2 + ")")
.call(d3.axisBottom(x).ticks(100));
// Add the y Axis
svg.append("g")
.attr("transform", "translate(" + width/2 + "," + 0 + ")")
.call(d3.axisLeft(y).ticks(100));
});
但是,我需要手动调整范围以使 y 轴居中。每个屏幕尺寸所需的值都不同,这就是为什么我需要帮助弄清楚如何始终让我的坐标轴在每个屏幕上居中。
var x = d3.scaleLinear().range([width/2-5*width,width/2+5*width]).domain([-50, 50]);
var y = d3.scaleLinear().range([height/2-5*height,height/2+3.244*width]).domain([50, -50]); //the value 3.244 is the problem, it changes on every screen
我创建了一个Fiddle,以便向您展示我是如何创建坐标轴的。
【问题讨论】:
标签: javascript html css d3.js svg