Skip to content

Commit

Permalink
Fix bugs in the world map and deal with edge cases (#722)
Browse files Browse the repository at this point in the history
* Fix bugs in the world map and deal with edge cases

* Ignore countries labeled XXX
  • Loading branch information
x4base authored and mistercrunch committed Jul 11, 2016
1 parent 00970d6 commit 3c92ba9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
22 changes: 13 additions & 9 deletions caravel/assets/visualizations/world_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ function worldMapChart(slice) {

d3.json(slice.jsonEndpoint(), function (error, json) {
div.selectAll("*").remove();
var fd = json.form_data;

if (error !== null) {
slice.error(error.responseText, error);
return '';
}
var ext = d3.extent(json.data, function (d) {
var fd = json.form_data;
var data = json.data.filter(function (d) {
// Ignore XXX's to get better normalization
return d.country && d.country !== 'XXX';
});

var ext = d3.extent(data, function (d) {
return d.m1;
});
var extRadius = d3.extent(json.data, function (d) {
var extRadius = d3.extent(data, function (d) {
return d.m2;
});
var radiusScale = d3.scale.linear()
.domain([extRadius[0], extRadius[1]])
.range([1, fd.max_bubble_size]);

json.data.forEach(function (d) {
data.forEach(function (d) {
d.radius = radiusScale(d.m2);
});

Expand All @@ -40,8 +44,8 @@ function worldMapChart(slice) {
.range(["#FFF", "black"]);

var d = {};
for (var i = 0; i < json.data.length; i++) {
var country = json.data[i];
for (var i = 0; i < data.length; i++) {
var country = data[i];
country.fillColor = colorScale(country.m1);
d[country.country] = country;
}
Expand All @@ -52,7 +56,7 @@ function worldMapChart(slice) {

var map = new Datamap({
element: slice.container.get(0),
data: json.data,
data: data,
fills: {
defaultFill: '#ddd'
},
Expand Down Expand Up @@ -93,7 +97,7 @@ function worldMapChart(slice) {
map.updateChoropleth(d);

if (fd.show_bubbles) {
map.bubbles(json.data);
map.bubbles(data);
div.selectAll("circle.datamaps-bubble").style('fill', '#005a63');
}

Expand Down
13 changes: 9 additions & 4 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,17 +1479,22 @@ def get_data(self):
secondary_metric = self.form_data.get('secondary_metric')
if metric == secondary_metric:
ndf = df[cols]
ndf['m1'] = df[metric]
ndf['m2'] = df[metric]
# df[metric] will be a DataFrame
# because there are duplicate column names
ndf['m1'] = df[metric].iloc[:, 0]
ndf['m2'] = ndf['m1']
else:
cols += [metric, secondary_metric]
ndf = df[cols]
df = ndf
df.columns = ['country', 'm1', 'm2']
d = df.to_dict(orient='records')
for row in d:
country = countries.get(
self.form_data.get('country_fieldtype'), row['country'])
country = None
if isinstance(row['country'], basestring):
country = countries.get(
self.form_data.get('country_fieldtype'), row['country'])

if country:
row['country'] = country['cca3']
row['latitude'] = country['lat']
Expand Down

0 comments on commit 3c92ba9

Please sign in to comment.