// Generated by CoffeeScript 1.10.0 (function() { var CELL_RADIUS, SIMPLIFICATION, boundary_color, boundary_thickness, draw_boundaries, dx, dy, height, mesh, projection, smooth, straight, svg, vis, width, zoom, zoomable_layer; svg = d3.select('svg'); width = svg.node().getBoundingClientRect().width; height = svg.node().getBoundingClientRect().height; svg.attr({ viewBox: (-width / 2) + " " + (-height / 2) + " " + width + " " + height }); zoomable_layer = svg.append('g'); zoom = d3.behavior.zoom().scaleExtent([0.5, 40]).on('zoom', function() { return zoomable_layer.attr({ transform: "translate(" + (zoom.translate()) + ")scale(" + (zoom.scale()) + ")" }); }); svg.call(zoom); vis = zoomable_layer.append('g').attr({ transform: 'translate(160, -170) rotate(-60)' }); smooth = d3_shape.line().curve(d3_shape.cardinal, 0.5); straight = d3_shape.line().curve(d3_shape.cardinal, 0.5); /* custom projection to make hexagons appear regular (y axis is also flipped) */ CELL_RADIUS = 0.12; dx = CELL_RADIUS * 2 * Math.sin(Math.PI / 3); dy = CELL_RADIUS * 1.5; SIMPLIFICATION = 20000; projection = function(p) { var x, y; x = p[0]; y = p[1]; return [x * dx / 2, -(y - (2 - (y & 1)) / 3) * dy / 2]; }; mesh = function(data, cmp) { var points; points = []; topojson.mesh(data, data.objects.leaf_regions, cmp).coordinates.forEach(function(c) { if (c.length < 1) { c = c[0]; } return points.push(c.filter(function(d, i) { return d[2] >= SIMPLIFICATION; }).map(function(d) { return projection(d); })); }); return points; }; boundary_color = d3.scale.ordinal().domain([1, 2, 3, 4, 5, 6]).range(['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666']); boundary_thickness = d3.scale.sqrt().domain([1, 6]).range([1.2, 0.01]); draw_boundaries = function(data, level, layer) { var boundaries, boundaries_data, boundaries_underline, level_color; boundaries_data = mesh(data, function(a, b) { return a.properties.path[level - 1] === b.properties.path[level - 1] && a.properties.path[level] !== b.properties.path[level]; }); level_color = d3.hcl(boundary_color(level)); level_color.c = 10; level_color.l = 97; boundaries_underline = layer.selectAll(".boundary_underline_" + level).data(boundaries_data); boundaries_underline.enter().append('path').attr({ "class": "boundary_underline boundary_underline_" + level, d: smooth, 'stroke-width': 8 * boundary_thickness(level), stroke: level_color }); boundaries = layer.selectAll(".boundary_" + level).data(boundaries_data); return boundaries.enter().append('path').attr({ "class": "boundary boundary_" + level, d: smooth, 'stroke-width': boundary_thickness(level) }); }; d3.json('leaf_regions.topo.json', function(data) { /* presimplify the topologies (compute the effective area (z) of each point) */ var boundaries_layer, coast, island, land; topojson.presimplify(data); /* parse paths into arrays, and extract the class of each leaf region */ topojson.feature(data, data.objects.leaf_regions).features.forEach(function(f) { f.properties.path = JSON.parse(f.properties.path); return f.properties["class"] = f.properties.path[f.properties.path.length - 1]; }); island = mesh(data, function(a, b) { return a === b; }); land = vis.selectAll('.land').data(island); land.enter().append('path').attr({ "class": 'land', d: straight }); vis.append('clipPath').attr({ id: 'island' }).selectAll('path').data(island).enter().append('path').attr({ d: straight }); boundaries_layer = vis.append('g').attr({ 'clip-path': 'url(#island)' }); d3.range(6, 0, -1).forEach(function(level) { return draw_boundaries(data, level, boundaries_layer); }); coast = vis.selectAll('.coast').data(island); return coast.enter().append('path').attr({ "class": 'coast', d: straight }); }); }).call(this);