Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Add basic map tooltips. #513

Merged
merged 1 commit into from
Aug 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions static/js/modules/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var compactRules = [
['k', 3]
];

_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};

function chooseRule(value) {
return _.find(compactRules, function(rule) {
return value >= Math.pow(10, rule[1]);
Expand All @@ -30,7 +34,7 @@ function compactNumber(value, rule) {
return d3.round(value / divisor, 1).toString() + rule[0];
}

function stateMap($elm, data, width, height, max, addLegend) {
function stateMap($elm, data, width, height, max, addLegend, addTooltips) {
var svg = d3.select($elm[0])
.append('svg')
.attr('width', width)
Expand Down Expand Up @@ -63,11 +67,18 @@ function stateMap($elm, data, width, height, max, addLegend) {
return d.properties.name;
})
.attr('class', 'shape')
.attr('d', path);
.attr('d', path)
.on('mouseover', function(d) {
this.parentNode.appendChild(this);
});

if (addLegend || typeof addLegend === 'undefined') {
stateLegend(svg, scale, quantize, quantiles);
}

if (addTooltips) {
stateTooltips(svg, path, results);
}
}

function stateLegend(svg, scale, quantize, quantiles) {
Expand Down Expand Up @@ -106,6 +117,37 @@ function stateLegend(svg, scale, quantize, quantiles) {
});
}

var tooltipTemplate = _.template(
'<div>{{ name }}</div>' +
'<div>{{ total }}</div>'
);

function stateTooltips(svg, path, results) {
var tooltip = d3.select('body').append('div')
.attr('id', 'map-tooltip')
.style('position', 'absolute')
.style('text-align', 'center')
.style('pointer-events', 'none')
.style('display', 'none');
svg.selectAll('path')
.on('mouseover', function(d) {
this.parentNode.appendChild(this);
var offset = $(this).offset();
var html = tooltipTemplate({
name: d.properties.name,
total: helpers.currency(results[d.properties.name])
});
tooltip
.style('display', 'block')
.style('left', parseInt(offset.left) + 'px')
.style('top', parseInt(offset.top) + 'px')
.html(html);
})
.on('mouseout', function(d) {
tooltip.style('display', 'none');
});
}

function highlightState($parent, state) {
var rule = '[data-state="' + state + '"]';
$parent.find('path:not(' + rule + ')').each(function(idx, elm) {
Expand Down
2 changes: 1 addition & 1 deletion static/js/pages/committee-single.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ $(document).ready(function() {
var $map = $('.state-map');
var url = buildStateUrl($map);
$.getJSON(url).done(function(data) {
maps.stateMap($map, data, 400, 400);
maps.stateMap($map, data, 400, 400, null, true, false);
});
events.on('state.table', function(params) {
maps.highlightState($map, params.state);
Expand Down
2 changes: 1 addition & 1 deletion static/js/pages/elections.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function drawStateMap($container, candidateId, cached) {
cached[candidateId] = results;
updateColorScale($container, cached);
var max = mapMax(cached);
maps.stateMap($map, data, 400, 300, max, false);
maps.stateMap($map, data, 400, 300, max, false, true);
});
}

Expand Down