您可以在 this 上看到我想要实现的目标的示例。页。
我有使用此代码构建的负轴和正轴:
this.svg = d3.select(el).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
this.xScale = d3.scale.linear()
.range([0, width]);
this.yScale = d3.scale.linear()
.range([height, 0]);
const xAxis = d3.svg.axis()
.scale(this.xScale);
const yAxis = d3.svg.axis()
.orient('left')
.scale(this.yScale);
const data = this.getDataFromProps(this.props.expression);
this.xScale.domain(d3.extent(data, function (d) {return d.x;}));
this.yScale.domain(d3.extent(data, function (d) {return d.y;}));
this.svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
this.svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(' + width/2 + ',0)')
.call(yAxis);
唯一的问题是 x 轴指向底部。如何将轴定位在 svg 上的 (0, 0) 处?
最佳答案
要使 x 轴与 y 轴的 0 标记对齐,请使用:-
this.svg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(0,' + (this.yScale(0)) + ')')
.call(xAxis);
关于d3.js - 使用 d3.js 将 x 轴移动到图表上的坐标 (0,0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691285/