gpt4 book ai didi

javascript - d3.js:axisBottom 出现在顶部

转载 作者:行者123 更新时间:2023-12-04 02:57:08 24 4
gpt4 key购买 nike

我正在尝试将 X 轴添加到我的 D3 (v5) 生成图表的底部,如下所示。然而,X 轴出现在顶部,即使我正在调用 axisBottom()。我在某处犯了错误吗?

results = {
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
y1: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
}

const W = 1000,
H = 620,
margin = {
top: 20,
right: 80,
bottom: 30,
left: 50,
};

const width = W - margin.left - margin.right,
height = H - margin.top - margin.bottom;

let scaleX = d3.scaleLinear()
.domain([results.x[0], results.x[results.x.length-1]]) // x is produced by numpy.linspace
.range([0, width]);

let ymax = Object.keys(results)
.filter(k => k !== 'x')
.map(k => d3.max(results[k].y))
.reduce((x, y) => Math.max(x, y), -Infinity);

let scaleY = d3.scaleLinear()
.domain([0, ymax])
.range([height, 0]);

let xAxis = d3.axisBottom(scaleX);

let svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);

svg.append("g")
.attr("class", "x axis")
.call(xAxis);

我附上了一张图片以供引用:轴出现在 SVG 元素的顶部;我希望它出现在底部。 axis appears right at the top of the SVG element; I expected it to appear at the bottom instead.

最佳答案

根据文档d3.axisBottom

Constructs a new bottom-oriented axis generator for the given scale, with empty tick arguments, a tick size of 6 and padding of 3. In this orientation, ticks are drawn below the horizontal domain path.

因此,您仍然需要平移轴。

关于javascript - d3.js:axisBottom 出现在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52565475/

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