-
Notifications
You must be signed in to change notification settings - Fork 0
/
bars.js
38 lines (28 loc) · 843 Bytes
/
bars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var dataset = [80,100,56,120,180,30,40,120,160];
var svgWidth = 500, svgHeight = 300, barPadding=3;
var barWidth = (svgWidth/dataset.length);
var svg = d3.select('svg').attr('width',svgWidth).attr('height',svgHeight)
var barChart = svg.selectAll("rect")
.data(dataset).enter().append("rect")
.attr("y",function(d){
return svgHeight-d;
})
.attr("height",function(d){
return d;
})
.attr("width",barWidth-barPadding)
.attr("transform",function(d,i){
var translate = [barWidth*i,0];
return "translate("+translate+")";
});
var text = svg.selectAll('text')
.data(dataset).enter().append('text').text(function(d){
return d;})
.attr("y",function(d,i){
return svgHeight-d-2;
})
.attr("x",function(d,i){
return barWidth*i;
})
.attr("fill","#A64C38")
// Translate is used to shift the bars by one position just like css transform