Skip to content

Instantly share code, notes, and snippets.

@roveo
Last active January 4, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roveo/8634701 to your computer and use it in GitHub Desktop.
Save roveo/8634701 to your computer and use it in GitHub Desktop.
D3 map training
<html>
<head>
<style>
path {
stroke: white;
stroke-width: .1px
}
#loading {
font-family: Verdana;
font-size: 72px
}
text.city {
font-family: Verdana;
font-size: 10px
}
</style>
</head>
<body>
<script src='//d3js.org/d3.v3.js'></script>
<script src='//d3js.org/topojson.v1.js'></script>
<script src='//d3js.org/queue.v1.js'></script>
<script src='//d3js.org/colorbrewer.v1.js'></script>
<script>
var width = 960,
height = 500;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
svg.append('text')
.attr('id', 'loading')
.attr('x', 100)
.attr('y', 100)
.text('Loading...');
var projection = d3.geo.albers()
.rotate([-105, 0])
.center([-10, 65])
.parallels([52, 64])
.scale(720)
.translate([width / 2, height / 2]);
queue()
.defer(d3.json, '//nemetz.devg.ru/d3/russia_1e-7sr.json')
.defer(d3.csv, '//nemetz.devg.ru/d3/Accidents.csv')
.defer(d3.tsv, '//nemetz.devg.ru/d3/cities.tsv')
.await(ready);
function ready(error, map, data, cities) {
d3.select('#loading').remove();
var color = d3.scale.linear()
.domain(d3.extent(data, function (d) { return d.Deaths }))
//.domain([0, 50, 150, 350, 750, 7500])
.range(colorbrewer['YlOrRd'][8]);
var rateById = {};
data.forEach(function (d) { rateById[d.RegionCode] = +d.Deaths });
var path = d3.geo.path().projection(projection);
svg.append('g')
.attr('class', 'region')
.selectAll('path')
.data(topojson.feature(map, map.objects.russia).features)
.enter().append('path')
.attr('d', path)
.attr('fill', function (d) {
return color(rateById[d.properties.region])
})
.on('mouseover', function (d, i) {
var reg = d3.select(this).datum().properties.region;
d3.selectAll('.region path')
.attr('fill', function (d, i) {
return d.properties.region == reg ? 'gray' : color(rateById[d.properties.region])
});
})
.on('mouseout', function (d, i) {
d3.selectAll('.region path')
.attr('fill', function (d, i) {
return color(rateById[d.properties.region])
});
});
var city = svg.selectAll('g.city')
.data(cities)
.enter()
.append('g')
.attr('class', 'city')
.attr('transform', function (d) { return 'translate(' + projection([d.lon, d.lat]) + ')' });
city.append('circle')
.attr('r', 5)
.attr('stroke', 'white')
.attr('fill', 'none')
.attr('opacity', .7)
.attr('stroke-width', 3);
city.append('text')
.attr('class', 'city')
.attr('x', 5)
.attr('y', -5)
//.attr('text-anchor', 'middle')
.attr('stroke', 'white')
.attr('fill', 'none')
.attr('opacity', .7)
.attr('stroke-width', 3)
.text(function (d) { return d.City });
city.append('text')
.attr('class', 'city')
.attr('x', 5)
.attr('y', -5)
//.attr('text-anchor', 'middle')
.attr('fill', '#333')
.text(function (d) { return d.City });
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment