This bar chart is implemented using d3.js and it uses TourPedia data. TourPedia contains information about accommodations, restaurants, points of interest and attractions of different places in Europe. As it is now, it provides information about Amsterdam, Barcelona, Berlin, Dubai, London, Paris, Rome and Tuscany. The bar chart shows the amount of venues grouped by city and by category. This bar chart is implemented using d3.js and it uses TourPedia data. TourPedia contains information about accommodations, restaurants, points of interest and attractions of different places in Europe. As it is now, it provides information about Amsterdam, Barcelona, Berlin, Dubai, London, Paris, Rome and Tuscany. The bar chart shows the amount of venues grouped by city and by category.
Last active
August 29, 2015 14:01
-
-
Save andreaderrico2/e5809e08fb500f35ca09 to your computer and use it in GitHub Desktop.
OpeNER - Bar Chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
rect { | |
shape-rendering: crispEdges; | |
} | |
rect:hover { | |
opacity: 0.8; | |
} | |
.x.axis path { | |
display: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<link rel="stylesheet" type="text/css" href="index.css"> | |
</head> | |
<body> | |
<script src='index.js'></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x0 = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var x1 = d3.scale.ordinal(); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var color = d3.scale.ordinal() | |
.range(["rgb(239, 142, 173)", "rgb(213, 161, 102)", "rgb(122, 186, 127)", "rgb(0, 189, 213)", "#DDD"]); | |
var xAxis = d3.svg.axis() | |
.scale(x0) | |
.orient("bottom") | |
.tickSize(0,0); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(d3.format(".2s")); | |
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 + ")"); | |
var data = []; | |
var url = 'http://tour-pedia.org/api/getPlacesStatistics'; | |
d3.json(url, function(apidata) { | |
var data = Object.keys(apidata).map(function(city_name){ | |
return { | |
name: city_name, | |
categories: Object.keys(apidata[city_name]).map(function(cat_name){ | |
return { | |
name: cat_name, | |
value: apidata[city_name][cat_name] | |
}; | |
}) | |
}; | |
}); | |
console.log(data); | |
var categories =['accommodation','attraction','restaurant','poi']; | |
x0.domain(data.map(function(d){ return d.name; })); | |
x1.domain(categories).rangeRoundBands([0, x0.rangeBand()], 0.1); | |
y.domain([0, d3.max(data, function(city) { return d3.max(city.categories, function(cat){ return cat.value; }); })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("# of Places"); | |
var cities = svg.selectAll(".city") | |
.data(data, function(d){ return d.name; }) | |
.enter().append("g") | |
.attr("class", "g") | |
.attr("transform", function(d) { return "translate(" + x0(d.name) + ",0)"; }); | |
var rect = cities.selectAll("rect") | |
.data(function(city) { return city.categories; }, function(d){ return d.name; }) | |
.enter().append("rect") | |
.attr("width", x1.rangeBand()) | |
.attr("x", function(d) { return x1(d.name); }) | |
.attr("y", function(d) { return y(d.value); }) | |
.attr("height", function(d) { return height - y(d.value); }) | |
.style("fill", function(d) { return color(d.name); }) | |
var legend = svg.selectAll(".legend") | |
.data(categories) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "end") | |
.text(function(d) { return d; }); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment