Skip to content

Instantly share code, notes, and snippets.

@niphadkarneha
Last active February 13, 2019 13:01
Show Gist options
  • Save niphadkarneha/9b22f88e5355b2156692c045fd8552ca to your computer and use it in GitHub Desktop.
Save niphadkarneha/9b22f88e5355b2156692c045fd8552ca to your computer and use it in GitHub Desktop.
HW4BarChart
license: mit

Built with blockbuilder.org

Q1. List the marks/channels used and the data/attributes they map to write an explanation of what the chart shows.
Answer: The mark used in this channel is a line to represent the bar graph. The position and length attributes tell us the Total number of students present in each country.
On the x-axis we have list of countries that are part of the European Union and on the Y-axis is the range of students. The longer the length of mark/line in relation to the y-axis the more number of students in the respective countries.

Q2. Point out 1-2 interesting insights (i.e., things you learned from the chart) include links to any examples or helpful tutorials that you used.
Answer: I selected countries who are part of the European Union and wanted to see which country has the highest number of students. For this, I categorized the universities based on Location. From the data we can see that:

  1. United Kingdom which is part of EU has the highest number of students enrolled in their University. Highest number of students comprise of both graduate and undergraduate students.

References

  1. https://bl.ocks.org/caravinden/d04238c4c9770020ff6867ee92c7dac1
  2. https://bl.ocks.org/Golodhros/6f8e6d1792416ee3770ff4ddd5c9594e
Country numStudents
Albania 800
Australia 105356
Brazil 24170
Bulgaria 1116
Canada 166134
China 6900
Denmark 33899
England 436465
Finland 34200
France 46911
India 1990
Iran 114428
Italy 53830
Japan 51942
Jordan 17849
Lebanon 6342
Mexico 48249
Netherlands 49939
Philippines 10688
Puerto Rico 15949
Romania 38140
Russia 339
South Korea 48013
Sri Lanka 22649
Sweden 27644
UK 802219
US 3329303827
Country numStudents
Australia 105356
Brazil 24170
Bulgaria 1116
Canada 166134
China 6900
Denmark 33899
England 436465
Finland 34200
France 46911
Country numStudents
Albania 800
Bulgaria 1116
Denmark 33899
Finland 34200
France 46911
Italy 53830
Netherlands 49939
Romania 38140
United Kingdom 802219
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> <!-- for color scales -->
<style>
body {font-family: calibri;}
.axis {font: 14px calibri;}
.label {font: 16px calibri;}
</style>
<body>
<h2>Bar Chart 1</h2>
<p>The bar represents the total number of students (Graduate and Undergraduate) based on the country.<br>These countries are part of Europeran Union.</p>
<div><svg id="chart1" width="700" height="300"></svg></div>
<script>
// chart 1
var svg1 = d3.select("#chart1"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg1.attr("width") - margin.left - margin.right,
height = +svg1.attr("height") - margin.top - margin.bottom;
var x1 = d3.scaleBand().rangeRound([0, width]).padding(0.2),
y1 = d3.scaleLinear().rangeRound([height, 0]);
var g1 = svg1.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// load data
d3.csv("BarChartEU.csv").then(function(data) {
data.forEach(function(d) {
d.numStudents = parseInt(d.numStudents);
console.log("numStudents:"+d.numStudents);
});
// See https://www.dashingd3js.com/d3js-scales
// maps domain of x values (letters) to range of positions on x-axis
x1.domain(data.map(function(d) { return d.Country ; }));
// maps domain of y values (frequencies 0, max freq) to range of positions on y-axis
y1.domain([0, d3.max(data, d => d.numStudents)]); // shorthand
// x-axis
g1.append("g")
.attr("class", "axis x-axis")
.attr("transform", "translate(0," + height + ")") // move axis to bottom of chart
.call(d3.axisBottom(x1));
// y-axis label
g1.append("text")
.attr("class", "label")
.attr("x", 195-margin.left) // set x position of label
.attr("y", 311-margin.bottom) // set y position of label
.style("text-anchor", "start") // left-justify
.text ("Names of Countries (European Union)")
// y-axis
g1.append("g")
.attr("class", "axis y-axis")
.call(d3.axisLeft(y1).ticks(10, "#")); // number of ticks and type
// y-axis label
g1.append("text")
.attr("class", "label")
.attr("x", 0-margin.left) // set x position of label
.attr("y", 0-margin.top/2) // set y position of label
.style("text-anchor", "start") // left-justify
.text ("Number of Students")
// bars
g1.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { console.log ("letter: " + d.Country + ", x: " + x1(d.Country )); return x1(d.Country ); })
.attr("y", function(d) { console.log ("freq: " + d.numStudents + ", y: " + d.numStudents); return y1(d.numStudents); })
.attr("width", x1.bandwidth()) // width of each band
.attr("height", function(d) { return height - y1(d.numStudents); })
.style("fill", function(d, i) { return "steelBlue"; }); // color of the bars
;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment