我正在尝试使用d3.js创建堆积条形图,但我遇到了问题。 问题是当在10,000高度Y轴上有一个值为1的条形时,条形高度为0px并且它不会出现在图表上。
导致问题的数据示例:
[{green: 1, yellow: 9400, white: 999999}]
“绿色”栏会很小,就像它不存在一样。
以下是相关代码:
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 1),
y = d3.scale.linear()
.rangeRound([height + 2, margin.top]);
var d3Elm = d3.select(element[0]);
var dataStackLayout = d3.layout.stack()(data);
x.domain(dataStackLayout[0].map(function(d){
return d3StackedBarCtrl.labelEllipsis(d.displayName, 10);
}));
y.domain([0,
d3.max(dataStackLayout[dataStackLayout.length - 1],
function (d) {
return d.y0 + d.y;
})
]).nice();
var layer = svg.selectAll('.stack')
.data(dataStackLayout)
.enter().append('g')
.attr('class', 'stack');
layer.selectAll('rect')
.data(function(d){
return d;
})
.enter().append('rect')
.attr('x', function(d){
return x(d.displayName);
})
.attr('y', function(d){
return y(d.y + d.y0);
})
.attr('width', 24)
.attr('height', function(d){
return y(d.y0) - y(d.y + d.y0);
})
.attr('class', 'rect');
如何设置条形的最小高度和让其他条形图在延长条形图上减少它们的高度?!
感谢。
答案 0 :(得分:0)
在其他任何事情之前,您正在做的是撒谎数据。如果绿色比白色小几个数量级,那就让它成为图表中某个地方的读者。但我知道你只想让相应的矩形可见。
有两种方法可以做到这一点,更改堆叠数据的矩阵或更改矩形本身。由于你只是想让酒吧几乎看不见,我选择第二个选项,这并不涉及更改数据(又名说谎!),创建一个规则集酒吧的最小高度。由于此栏会在下一栏重叠一点,我们赢了需要更改其他栏值(与您的要求相反);除此之外,它处理的是像素,而不是数据。
规则可以是这样简单的事情:
.attr("height", function(d) {
return y(d.y0) - y(d.y0 + d.y) < 2 ? 2 : y(d.y0) - y(d.y0 + d.y);
})
其中说:&#34;检查计算的高度。是不到2像素?如果是,请将高度设置为2像素。如果没有,请设置计算的高度&#34;。
检查这个演示(这段代码不是我的,我只是在这个小提琴中找到它:http://jsfiddle.net/xavipolo/q5q6331p/)。我做了#34;橘子&#34;在2007年具有较小的值。但是,相应的条的最小高度为4像素:
// Setup svg using Bostock's margin convention
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body")
.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 + ")");
/* Data in strings like it would be if imported from a csv */
var data = [
{ year: "2006", redDelicious: "100", mcintosh: "150", oranges: "90", pears: "60" },
{ year: "2007", redDelicious: "120", mcintosh: "180", oranges: "1", pears: "40" },
{ year: "2008", redDelicious: "050", mcintosh: "200", oranges: "80", pears: "20" },
{ year: "2009", redDelicious: "010", mcintosh: "150", oranges: "50", pears: "40" },
{ year: "2010", redDelicious: "020", mcintosh: "100", oranges: "40", pears: "20" },
{ year: "2011", redDelicious: "030", mcintosh: "120", oranges: "60", pears: "30" },
{ year: "2012", redDelicious: "040", mcintosh: "150", oranges: "80", pears: "10" },
{ year: "2013", redDelicious: "060", mcintosh: "110", oranges: "90", pears: "40" },
{ year: "2014", redDelicious: "100", mcintosh: "130", oranges: "90", pears: "50" },
{ year: "2015", redDelicious: "160", mcintosh: "190", oranges: "60", pears: "90" },
{ year: "2016", redDelicious: "190", mcintosh: "170", oranges: "50", pears: "70" },
];
var parse = d3.time.format("%Y").parse;
// Transpose the data into layers
var dataset = d3.layout.stack()(["redDelicious", "mcintosh", "oranges", "pears"].map(function(fruit) {
return data.map(function(d) {
return {x: parse(d.year), y: +d[fruit]};
});
}));
// Set x, y and colors
var x = d3.scale.ordinal()
.domain(dataset[0].map(function(d) { return d.x; }))
.rangeRoundBands([10, width-10], 0.02);
var y = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d3.max(d, function(d) { return d.y0 + d.y; }); })])
.range([height, 0]);
var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574"];
// Define and draw axes
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width, 0, 0)
.tickFormat( function(d) { return d } );
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%Y"));
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Create groups for each series, rects for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.style("fill", function(d, i) { return colors[i]; });
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y) < 4 ? 4 : y(d.y0) - y(d.y0 + d.y); })
.attr("width", x.rangeBand())
.on("mouseover", function() { tooltip.style("display", null); })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3.mouse(this)[0] - 15;
var yPosition = d3.mouse(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text(d.y);
});
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Anjou pears";
case 1: return "Naval oranges";
case 2: return "McIntosh apples";
case 3: return "Red Delicious apples";
}
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;