(function() { var concat; concat = function(a) { return a.reduce((function(a, o) { return a.concat(o); }), []); }; window.main = function() { /* "globals" */ var categories, colorify, container, dx, dy, height, legend, path_generator, radius, scale, svg, vis, whiten, whiteness, width, _i, _ref, _ref2, _results; vis = null; width = 960; height = 500; svg = d3.select('body').append('svg').attr('width', width).attr('height', height); /* ZOOM and PAN */ /* create container elements */ container = svg.append('g').attr('transform', 'translate(560, 54)'); container.call(d3.behavior.zoom().scaleExtent([1, 49]).on('zoom', (function() { return vis.attr('transform', "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); }))); vis = container.append('g'); /* create a rectangular overlay to catch events */ /* WARNING rect size is huge but not infinite. this is a dirty hack */ vis.append('rect').attr('class', 'overlay').attr('x', -500000).attr('y', -500000).attr('width', 1000000).attr('height', 1000000); /* END ZOOM and PAN */ /* custom projection to make hexagons appear regular (y axis is also flipped) */ radius = 6; dx = radius * 2 * Math.sin(Math.PI / 3); dy = radius * 1.5; path_generator = d3.geo.path().projection(d3.geo.transform({ point: function(x, y) { return this.stream.point(x * dx / 2, -(y - (2 - (y & 1)) / 3) * dy / 2); } })); /* depth scale */ whiteness = 0.6; whiten = function(color) { return d3.interpolateHcl(color, d3.hcl(void 0, 0, 100))(whiteness); }; colorify = d3.scale.quantize().domain([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).range(['#396353', '#0DB14B', '#6DC067', '#ABD69B', '#DAEAC1', '#DFCCE4', '#C7B2D6', '#9474B4', '#754098', '#504971'].map(whiten)); /* legend */ legend = svg.append('g').attr('id', 'legend').attr('transform', 'translate(30,20)'); scale = (function() { _results = []; for (var _i = _ref = colorify.domain()[0], _ref2 = colorify.domain()[1]; _ref <= _ref2 ? _i <= _ref2 : _i >= _ref2; _ref <= _ref2 ? _i++ : _i--){ _results.push(_i); } return _results; }).apply(this); categories = legend.selectAll('.category').data(scale).enter().append('g').attr('class', 'category').attr('transform', function(d, i) { return "translate(0," + (i * 22) + ")"; }); categories.append('rect').attr('x', 0).attr('y', 0).attr('width', 22).attr('height', 22).attr('fill', function(d) { return colorify(d); }); categories.append('text').attr('y', 11).attr('dx', -4).attr('dy', '0.35em').text(function(d) { return d; }); /* load topoJSON data */ return d3.json('regions_depth.topo.json', function(error, data) { /* write the name of the land */ var defs, depth, title; title = svg.append('text').attr('class', 'title').text(topojson.feature(data, data.objects['0']).features[0].properties.label).attr('transform', 'translate(80,40)'); /* define the level zero region (the land) */ defs = svg.append('defs'); defs.append('path').datum(topojson.feature(data, data.objects['0']).features[0]).attr('id', 'land').attr('d', path_generator); /* faux land glow (using filters takes too much resources) */ vis.append('use').attr('class', 'land-glow-outer').attr('xlink:href', '#land'); vis.append('use').attr('class', 'land-glow-inner').attr('xlink:href', '#land'); /* draw the depth levels */ vis.selectAll('.contour').data(topojson.feature(data, data.objects.depth).features).enter().append('path').attr('class', 'contour').attr('d', path_generator).attr('fill', function(d) { return colorify(d.properties.depth); }); /* draw the political boundaries */ for (depth = 1; depth <= 2; depth++) { vis.append('path').datum(topojson.mesh(data, data.objects[depth], (function(a, b) { return a !== b; }))).attr('d', path_generator).attr('class', 'boundary').style('stroke-width', "" + (1.8 / depth) + "px"); } /* draw the land border */ vis.append('use').attr('class', 'land-fill').attr('xlink:href', '#land'); /* draw the other labels */ return vis.selectAll('.label').data(concat((function() { var _results2; _results2 = []; for (depth = 2; depth >= 1; depth--) { _results2.push(topojson.feature(data, data.objects[depth]).features.map(function(d) { return { feature: d, depth: depth }; })); } return _results2; })())).enter().append('text').attr('class', 'label').attr('dy', '0.35em').attr('transform', (function(d) { var area, centroid; centroid = path_generator.centroid(d.feature); area = path_generator.area(d.feature); return "translate(" + centroid[0] + "," + centroid[1] + ") scale(" + (10 / d.depth) + ")"; })).text(function(d) { return d.feature.properties.label; }); }); }; }).call(this);