简体   繁体   English

d3.js:axisBottom出现在顶部

[英]d3.js: axisBottom is appearing at the top instead

I'm trying to add an X-axis to the bottom of my D3 (v5) generated graph, as below. 我正在尝试将X轴添加到D3(v5)生成的图形的底部,如下所示。 However, the X-axis appears at the top, even though I am calling axisBottom() . 但是,即使我正在调用axisBottom() ,X轴也会显示在顶部。 Is there a mistake that I'm making somewhere? 我在某处犯错误吗?

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);

I've attached an image for reference: axis appears right at the top of the SVG element; 我已附上一张图片供参考:轴出现在SVG元素的顶部; I expected it to appear at the bottom instead. 我希望它会出现在底部。 轴显示在SVG元素的顶部;我希望它会出现在底部。

According to the documentation d3.axisBottom 根据文档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. 为给定的比例构造一个新的面向底部的轴生成器,其空的刻度参数,刻度尺寸为6且填充值为3。在此方向上,刻度绘制在水平域路径下方。

So, you still have to translate the axis. 因此,您仍然必须平移轴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM