我想附加堆积的条形图以及另一个具有不同尺寸的条形图,以便所有三个都可见,但即使使用不同的尺寸,附加的条形图也不可见。 工作fiddle
CODE.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Simple STACKBAR</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style type="text/css">
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
}
path.domain {
stroke: none;
}
.y .tick line {
stroke: #ddd;
}
.bar1 {
fill: steelblue;
opacity: 0.5;
}
.bar1:hover {
fill: brown;
}
</style>
</head>
<body>
<script type="text/javascript">
// 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: "Technical/Analytical", redDelicious: "10", mcintosh: "15", oranges: "19" },
{ year: "Problem Solving", redDelicious: "12", mcintosh: "18", oranges: "19" },
{ year: "Communication", redDelicious: "05", mcintosh: "20", oranges: "28" },
{ year: "Specialization", redDelicious: "08", mcintosh: "15", oranges: "16" },
];
//var parse = d3.time.format("%Y").parse;
// Transpose the data into layers
var dataset = d3.layout.stack()(["redDelicious", "mcintosh"].map(function(skillset) {
return data.map(function(d) {
return {x: d.year, y: +d[skillset]};
});
}));
// 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", "#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); })
.attr("width", x.rangeBand());
groups.append("rect")
.attr("class", "bar1")
.attr("x", function(d) {
return x(d.year) + 20; // center it
})
.attr("width", x.rangeBand() - 40) // make it slimmer
.attr("y", function(d) {
return y(d.oranges);
})
.attr("height", function(d) {
return height - y(d.oranges);
});
</script>
</body>
</html>
答案 0 :(得分:2)
<强>步骤1
使用地图为橙色条设置数据集,如下所示:
var orangeData = data.map(function(d) {
return {
year: d.year,
oranges: +d.oranges
}
});
<强>第二步
使堆积的条形图更加纤细并将其放在x刻度的一侧。
var rect = groups.selectAll("rect")
.data(function(d) {
return d;
})
.enter()
.append("rect")
.attr("x", function(d) {
return x(d.x) + x.rangeBand() / 4;//places stack left of the x tick
})
.attr("y", function(d) {
return y(d.y0 + d.y);
})
.attr("height", function(d) {
return y(d.y0) - y(d.y0 + d.y);
})
.attr("width", x.rangeBand() / 4);//make the stack bar thin by 4 times.
第3步
制作条形图。
svg.selectAll(".bar1").data(orangeData).enter().append("g")
.attr("class", "bar1").append("rect")
.attr("x", function(d) {
return x(d.year) + x.rangeBand() / 2; // place it right if the x tick
})
.attr("width", x.rangeBand()/4) // make it as slim as stacked bar chart
.attr("y", function(d) {
return y(d.oranges);
})
.attr("height", function(d) {
return height - y(d.oranges);
});
工作代码here
答案 1 :(得分:0)
第28行,添加&#39;橙子&#39;;
var dataset = d3.layout.stack()([&#34; redDelicious&#34;,&#34; mcintosh&#34;,&#39; oranges&#39;]。map(function(skillset){