使用 d3.js 制作分组条形图
Posted
技术标签:
【中文标题】使用 d3.js 制作分组条形图【英文标题】:Making a grouped bar chart using d3.js 【发布时间】:2017-12-25 22:56:29 【问题描述】:我是 d3.js 的新手,我发现很难创建分组条形图,但我尝试了一些似乎不正确的方法。我想要 x 轴上的月份和 y 轴上的组数(火星和木星)。类似于 dis Link。
var svg = d3.select("svg"),
margin = top: 20, right: 20, bottom: 30, left: 40,
width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0,
width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [
"Group":"Mars","count":10,"months":"June",
"Group":"Jupiter","count":50,"months":"June",
"Group":"Mars","count":70,"months":"July",
"Group":"Jupiter","count":60,"months":"July"];
var ymaxdomain=d3.max(d,function(d)return d.count;);
x.domain(d.map(function(d) return d.months));
y.domain([0,ymaxdomain]);
var x1=d3.scaleBand().rangeRound([0, x.bandwidth()]);
x1.domain(d.map(function(d) return d.months;));
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("x", function(d,i) console.log(d,i); return (x(d.months))
.attr("y", function(d) return y(d.count); )
.attr("width",x1.bandwidth())
.attr("height", function(d) return height - y(d.count); )
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("count");
<style>
.bar
fill: steelblue;
stroke:black
</style>
<!DOCTYPE html>
<svg ></svg>
<body><script src="//d3js.org/d3.v4.min.js"></script></body>
【问题讨论】:
【参考方案1】:要创建分组条形图,您必须为 x 位置设置 2 个比例:
var x = d3.scaleBand().rangeRound([0, width])
.padding(0.1);
var x1 = d3.scaleBand()
.rangeRound([0, x.bandwidth()])
.padding(0.05);
然后,您使用第一个比例附加组...
var groups = g.selectAll(null)
.data(data)
.enter()
.append("g")
.attr("transform", function(d)
return "translate(" + x(d.months) + ",0)";
)
...并且,在每个组内,您使用第二个比例附加矩形。
这里是演示:
var svg = d3.select("svg"),
margin =
top: 20,
right: 20,
bottom: 30,
left: 40
,
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var color = d3.scaleOrdinal(d3.schemeCategory10);
var x = d3.scaleBand().rangeRound([0, width])
.padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [
"Group": "Mars",
"count": 10,
"months": "June"
,
"Group": "Jupiter",
"count": 50,
"months": "June"
,
"Group": "Mars",
"count": 70,
"months": "July"
,
"Group": "Jupiter",
"count": 60,
"months": "July"
];
var ymaxdomain = d3.max(data, function(d)
return d.count;
);
x.domain(data.map(function(d)
return d.months
));
y.domain([0, ymaxdomain]);
var x1 = d3.scaleBand()
.rangeRound([0, x.bandwidth()])
.padding(0.05)
.domain(data.map(function(d)
return d.Group;
));
color.domain(data.map(function(d)
return d.Group;
));
var groups = g.selectAll(null)
.data(data)
.enter()
.append("g")
.attr("transform", function(d)
return "translate(" + x(d.months) + ",0)";
)
var bars = groups.selectAll(null)
.data(function(d)
return [d]
)
.enter()
.append("rect")
.attr("x", function(d, i)
return x1(d.Group)
)
.attr("y", function(d)
return y(d.count);
)
.attr("width", x1.bandwidth())
.attr("height", function(d)
return height - y(d.count);
)
.attr("fill", function(d)
return color(d.Group)
)
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("count");
.bar
fill: steelblue;
stroke: black
<svg ></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
【讨论】:
我非常感谢您的帮助。非常感谢!:)@Gerardo Furtado 我正在学习 d3js .... 这里你调用函数 x 并传递一个 arg d.months... 你在哪里使用它在 var x = .....以上是关于使用 d3.js 制作分组条形图的主要内容,如果未能解决你的问题,请参考以下文章