Skip to content

Commit

Permalink
Merge pull request #723 from 18F/714-add-us-territories
Browse files Browse the repository at this point in the history
Issue 714: Group US territories under United States
  • Loading branch information
tdlowden authored Oct 21, 2020
2 parents ce46d29 + f18a227 commit 92fe548
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 36 deletions.
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,
};

0 comments on commit 92fe548

Please sign in to comment.