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 is horizontal variant of the vertical chart and shows the amount of venues grouped by city and by category.
Last active
August 29, 2015 14:01
-
-
Save andreaderrico2/e24d6f3a3db48aa4d8a8 to your computer and use it in GitHub Desktop.
OpeNER - Horizontal 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: lightgrey; | |
shape-rendering: crispEdges; | |
} | |
rect { | |
shape-rendering: crispEdges; | |
} | |
rect:hover { | |
opacity: 0.7; | |
} | |
.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: 100}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var y0 = d3.scale.ordinal() | |
.rangeRoundBands([height, 0], .1); | |
var y1 = d3.scale.ordinal(); | |
var x = d3.scale.linear() | |
.range([0, width-100]); | |
var color = d3.scale.ordinal() | |
.range(["rgb(239, 142, 173)", "rgb(213, 161, 102)", "rgb(122, 186, 127)", "rgb(0, 189, 213)", "#DDD"]); | |
var yAxis = d3.svg.axis() | |
.scale(y0) | |
.orient("left") | |
.tickSize(0,0); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom") | |
.tickSize(height) | |
.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']; | |
y0.domain(data.map(function(d){ return d.name; })); | |
y1.domain(categories).rangeRoundBands([0, y0.rangeBand()], 0.1); | |
x.domain([0, d3.max(data, function(city) { return d3.max(city.categories, function(cat){ return cat.value; }); })]); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
svg.append("g") | |
.attr("class", "x axis") | |
.call(xAxis) | |
.append("text") | |
.attr("x", width) | |
.attr("y", height) | |
.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(0," + y0(d.name) + ")"; }); | |
var rect = cities.selectAll("rect") | |
.data(function(city) { return city.categories; }, function(d){ return d.name; }) | |
.enter().append("rect") | |
.attr("height", y1.rangeBand()) | |
.attr("x", 2) | |
.attr("y", function(d) { return y1(d.name); }) | |
.attr("width", 0) | |
.style("fill", function(d) { return color(d.name); }); | |
rect.transition() | |
.duration(600) | |
.ease("linear") | |
.attr("width", function(d) { return x(d.value)}); | |
rect.append("title") | |
.text(function(d){ | |
return d.value | |
}); | |
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-90) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 60) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "start") | |
.text(function(d) { return d; }); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment