Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 714: Group US territories under United States #723

Merged
merged 3 commits into from
Oct 21, 2020
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
10 changes: 9 additions & 1 deletion _includes/charts.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,18 @@ <h4>Countries</h4>
</div>
</figure>

<figure id="chart_us_and_territories"
data-block="us_and_territories"
data-source="{{ site.data_url }}/{{ data_prefix }}/top-countries-realtime.json">
<h4>United States &amp; Territories</h4>
<div class="data bar-chart">
</div>
</figure>

<figure id="chart_countries"
data-block="international_visits"
data-source="{{ site.data_url }}/{{ data_prefix }}/top-countries-realtime.json">
<h4>International &amp; Territories</h4>
<h4>International</h4>
<div class="data bar-chart">
</div>
</figure>
Expand Down
315 changes: 287 additions & 28 deletions assets/bundle.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ whenRendered(['browsers', 'ie'], () => {
// chart once they're both rendered
whenRendered(['countries', 'international_visits'], () => {
d3.select('#chart_us')
.call(nestCharts, (d) => d.key === 'International &amp; Territories', d3.select('#chart_countries'));
.call(nestCharts, (d) => d.key === 'International', d3.select('#chart_countries'));
});

whenRendered(['countries', 'us_and_territories'], () => {
d3.select('#chart_us')
.call(nestCharts, (d) => d.key === 'United States &amp; Territories', d3.select('#chart_us_and_territories'));
});
/*
* A very primitive, aria-based tab system!
*/
Expand Down
22 changes: 16 additions & 6 deletions js/lib/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import barChart from './barchart';
import buildTimeSeries from './timeseries';
import formatters from './formatters';
import transformers from './transformers';
import { isPartOfUnitedStates } from './territories';

/*
* Define block renderers for each of the different data types.
Expand Down Expand Up @@ -88,32 +89,41 @@ export default {
cityListFiltered,
(list) => list.map((x) => x.active_visitors),
);
return proportions.slice(0, 10);
return proportions.slice(0, 13);
}, 'city'),

countries: renderBlock.buildBarChart((d) => {
let totalVisits = 0;
let USVisits = 0;
d.data.forEach((c) => {
totalVisits += parseInt(c.active_visitors, 10);
if (c.country === 'United States') {
USVisits = c.active_visitors;
if (isPartOfUnitedStates(c.country)) {
USVisits += parseInt(c.active_visitors, 10);
}
});
const international = totalVisits - USVisits;
const data = {
'United States': USVisits,
'International &amp; Territories': international,
'United States &amp; Territories': USVisits,
International: international,
};
return transformers.findProportionsOfMetricFromValue(transformers.listify(data));
}),

us_and_territories: renderBlock.buildBarChartWithLabel((d) => {
let values = transformers.findProportionsOfMetric(
d.data,
(list) => list.map((x) => x.active_visitors),
);
values = values.filter((c) => isPartOfUnitedStates(c.country));
return values.slice(0, 3);
}, 'country'),

international_visits: renderBlock.buildBarChartWithLabel((d) => {
let values = transformers.findProportionsOfMetric(
d.data,
(list) => list.map((x) => x.active_visitors),
);
values = values.filter((c) => c.country !== 'United States');
values = values.filter((c) => !isPartOfUnitedStates(c.country));
return values.slice(0, 15);
}, 'country'),

Expand Down
17 changes: 17 additions & 0 deletions js/lib/territories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const US_AND_TERRITORIES = [
'United States',
'Puerto Rico',
'Guam',
'U.S. Virgin Islands',
'American Samoa',
'Northern Mariana Islands',
];

const isPartOfUnitedStates = function (name) {
return new RegExp(name).test(US_AND_TERRITORIES.join('|'));
};

export {
US_AND_TERRITORIES,
isPartOfUnitedStates,
};