【发布时间】:2020-10-23 11:45:49
【问题描述】:
我正在根据数据(动态变化)绘制直方图,但某些条形的高度不适合 svg 区域(超出 svg 区域) 这是我怀疑的一段代码:
private drawBars(data: any[]): void {
let f = Math.min.apply(Math, this.fixedData.map(function (o) {
return o.xAxis;
}));
/** Create the X-axis band scale */
const x = d3.scaleBand()
.range([f, 240])
.domain(data.map(d => d.xAxis))
.padding(0);
/** Create the Y-axis band scale */
const y = d3.scaleLinear()
.domain([0, this.axisMax])
.range([this.height, 0])
.nice();
/** Create and fill the bars */
this.svg.selectAll("*").remove()
this.svg.selectAll("bars")
.data(data, d => d)
.enter()
.append("rect")
.attr("x", d => x(d.xAxis))
.attr("y", d => y(d.yAxis))
.attr("width", x.bandwidth())
.attr("height", (d) => this.height - y(d.yAxis))
.attr("fill", (d) => this.colorPicker(d))
}
这是这种奇怪行为的证明:
任何想法,我将不胜感激!
【问题讨论】:
-
请注意,多个数据的行为相同
-
看起来
this.axisMax太低了。您能否检查该值是否计算正确?
标签: javascript svg d3.js histogram