<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
d3.selectAll(“p”).style(“font-size”, 12);
d3.select(“body”).style(“font-size”, 12);
d3.select(“body”)
.style(“font-size”, 12)
.style(“color”, “#333333”);
<style>
#chart rect{
fill: #4aaeea;
}
#chart text{
fill: black;
font: 10px sans-serif;
text-anchor: end;
}
.axis text{
font: 10px sans-serif;
}
.axis path, .axis line{
fill: none;
stroke : #333;
shape-rendering: crispEdges;
}
body{
color : #333333;
padding-top : 50px;
} </style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin ={top:40, right:40, bottom:40, left:40},
width=900-margin.left - margin.right,
height=500-margin.top-margin.bottom;
var x = d3.scale.ordinal().rangeRoundBands([0, width], .5);
var y = d3.scale.linear().range([height, 0]);
var chart = d3.select("#chart")
.append("svg") //append svg element inside #chart
.attr("width", width+(2*margin.left)+margin.right) //set width
.attr("height", height+margin.top+margin.bottom); //set height
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
d3.json("data.json", function(error, data){
var pxAboveBar = 10;
var ymax = d3.max(d3.values(data));
x.domain(data.map(function(d){ return d.country}));
y.domain([0, ymax.gdp + 2]);
var bar = chart.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", function(d, i){
return "translate("+x(d.country)+", 0)";
});
bar.append("rect")
.attr("y", function(d) {
return y(d.gdp);
})
.attr("x", function(d,i){
return x.rangeBand();
})
.attr("height", function(d) {
return height - y(d.gdp);
})
.attr("width", x.rangeBand());
bar.append("text")
.attr("x", x.rangeBand() + (x.rangeBand()/1.5) )
.attr("y", function(d) { return y(d.gdp) - pxAboveBar ; })
.attr("dy", ".75em")
.text(function(d) { return d.gdp; });
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate("+margin.left+","+ height+")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.attr("transform", "translate("+margin.left+",0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("GDP in trillions");
});
</script>
[
{ "country":"United States",
"gdp":17.4
},
{ "country":"China",
"gdp":10.4
},
{ "country":"Japan",
"gdp":4.6
},
{ "country":"Germany",
"gdp":3.9
},
{ "country":"United Kingdom",
"gdp":2.9
},
{ "country":"France",
"gdp":2.8
},
{ "country":"Brazil",
"gdp":2.4
},
{ "country":"Italy",
"gdp":2.1
},
{ "country":"India",
"gdp":2.1
},
{ "country":"Russia",
"gdp":1.9
}
]
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin ={top:40, right:40, bottom:40, left:40},
width=900-margin.left - margin.right,
height=500-margin.top-margin.bottom;
var x = d3.scale.ordinal().rangeRoundBands([0, width], .5);
var y = d3.scale.linear().range([height, 0]);
var chart = d3.select("#chart")
.append("svg") //append svg element inside #chart
.attr("width", width+(2*margin.left)+margin.right)
.attr("height", height+margin.top+margin.bottom);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
d3.json("data.json", function(error, data){
var pxAboveBar = 10;
var ymax = d3.max(d3.values(data));
x.domain(data.map(function(d){ return d.country}));
y.domain([0, ymax.gdp + 2]);
var bar = chart.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", function(d, i){
return "translate("+x(d.country)+", 0)";
});
g is not actually a d3.js specific element. It is a methodology in svg to group shapes together. If you would like to learn about svg or g I would read through
Jacob Jenkov's thorough tutorial. Each bar we will be creating consists of a g element. Each g element contains a rect and text. We use the
.data method to populate our data results array and the
.enter() to allow us access to each one of them.
bar.append("rect")
.attr("y", function(d) {
return y(d.gdp);
})
.attr("x", function(d,i){
return x.rangeBand();
})
.attr("height", function(d) {
return height - y(d.gdp);
})
.attr("width", x.rangeBand());
bar.append("text")
.attr("x", x.rangeBand() + (x.rangeBand()/1.5) )
.attr("y", function(d) { return y(d.gdp) - pxAboveBar ; })
.attr("dy", ".75em")
.text(function(d) { return d.gdp; });
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate("+margin.left+","+ height+")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.attr("transform", "translate("+margin.left+",0)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("GDP in trillions");
We appended our rect and text as promised above. We have created our graph more or less with our rectangles sized correctly and the numbers corresponding to them. However there is no x or y axis displaying to help individuals correctly understand the graph. We will start with the x axis that we largely set up earlier through the var xAxis. We attach a class "x axis" which really just helps us keep in mind that it is the x axis, the "axis" portion of the class is what our CSS will find and use. The
.attr("transform", "translate") can provide us with the exact coordinates and the
.call() method to pass in our current selection. The y axis is almost the same except that we want to add some text or a label to the y axis, indicating that it is GDP in trillions to help the user understand each of the values.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)