【发布时间】:2018-02-23 15:09:25
【问题描述】:
我使用 d3 v4 制作了一个水平条形图,除了一件事外,它工作正常。我无法固定条形高度。我目前正在使用带宽(),如果我用固定值替换它(15),问题是它与 y 轴标签/刻度线不对齐http://jsbin.com/gigesi/3/edit?html,css,js,output
var w = 200;
var h = 400;
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h)
.attr("transform", "translate(80,30)");
var data = [
{Item: "Item 1", count: "11"},
{Item: "Item 2", count: "14"},
{Item: "Item 3", count: "10"},
{Item: "Item 4", count: "14"}
];
var xScale = d3.scaleLinear()
.rangeRound([0,w])
.domain([0, d3.max(data, function(d) {
return d.count;
})]);
var yScale = d3.scaleBand()
.rangeRound([h,0]).padding(0.2)
.domain(data.map(function(d) {
return d.Item;
}));
var yAxis = d3.axisLeft(yScale);
svg.append('g')
.attr('class','axis')
.call(yAxis);
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('width', function(d,i) {
return xScale(d.count);
})
.attr('height', yScale.bandwidth())
.attr('y', function(d, i) {
return yScale(d.Item);
}).attr("fill","#000");
【问题讨论】:
-
我尝试进行此更改 .attr('height', 15 + this.data.length / this.yScale.bandwidth()) 但这无助于与数据长度时的刻度对齐有 1 或 2 个条目。
标签: d3.js height bar-chart fixed