function usHexMap (data, {
topojson = null,
stateCodesWithNames = null,
colorRange = ['#eee','#23c17c','#4f8fe6', '#6f0043', '#eee'],
colorDomain = [0, 50, 75, 100],
f = d3.format('.1f'),
height = 600,
margin = {top: 5, right: 10, bottom: 5, left: 10}
} = {}) {
const w = width - margin.left - margin.right;
const h = height - margin.top - margin.bottom;
const svg = DOM.svg(width, height);
const sel = d3.select(svg)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const tiles = topojson.feature(tilegram, tilegram.objects.tiles);
const transform = d3.geoTransform({
point: function (x, y) { return this.stream.point(x, -y) }
});
const path = d3.geoPath().projection(transform);
const g = sel.append('g')
.attr('transform', 'translate(-350,' + (h-10) + ')');
const thresholdScale = d3.scaleThreshold()
.domain(colorDomain)
.range(colorRange);
const borders = g.selectAll('path')
.data(tiles.features)
.join('path')
.attr('d', path)
.attr('class', 'border')
.attr('fill', d => {
const state = d.properties.state;
const datum = _.find(data, o => o.code === state);
return thresholdScale(datum.value)
})
.attr('stroke', '#fff')
.attr('stroke-width', 4)
.on('click', (e, d) => console.log('d', d));
borders.on('mouseover', function (e, d) {
const state = d.properties.state;
const datum = _.find(data, o => o.code === state);
const label = _.find(stateCodesWithNames, o => o.code === state);
sel.select('text.info').text(`${label.state} ${f(datum.value)}`);
});
g.selectAll('.state-label')
.data(tiles.features)
.join('text')
.attr('class', d => 'state-label state-label-' + d.id)
.attr('transform', d => `translate(${path.centroid(d)})`)
.attr('dy', '.35em')
.text(d => d.properties.state);
sel.append('text')
.attr('y', 20)
.attr('x', -10)
.attr('class', 'info');
const legend = sel.append('g')
.attr('class', 'legend-wrap')
.attr('transform', `translate(${(w- 300)},${(h-10)})`)
.selectAll('g.legend')
.attr('class', 'legend')
.data(colorDomain)
.join('g')
.attr('class', 'legend')
legend.append('rect')
.attr('x', (d, i) => i * 80)
.attr('fill', (d, i) => thresholdScale(d) )
.attr('width', 80)
.attr('height', 10);
legend.append('text')
.attr('x', (d, i) => i * 80 + 40)
.attr('y', -5)
.attr('text-anchor', 'middle')
.text(d => `> ${d}`);
return svg;
}