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

Choropleth customize #3154

Merged
merged 8 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 11 additions & 2 deletions client/app/visualizations/choropleth/choropleth-editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label>Country code column</label>
<label>Map Type</label>
<select ng-options="key as value for (key, value) in mapTypes"
ng-model="options.mapType" class="form-control"></select>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label>Key column</label>
<select ng-options="name for name in queryResult.getColumnNames()"
ng-model="options.countryCodeColumn" class="form-control"></select>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label>Country code type</label>
<label>Key type</label>
<select ng-options="key as value for (key, value) in countryCodeTypes"
ng-model="options.countryCodeType" class="form-control"></select>
</div>
Expand Down
99 changes: 76 additions & 23 deletions client/app/visualizations/choropleth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import template from './choropleth.html';
import editorTemplate from './choropleth-editor.html';

import countriesDataUrl from './countries.geo.json';
import subdivJapanDataUrl from './japan.prefectures.geo.json';

const loadCountriesData = _.bind(function loadCountriesData($http, url) {
if (!this[url]) {
Expand Down Expand Up @@ -65,6 +66,16 @@ function choroplethRenderer($sanitize, $http) {
}
}

function getDataUrl(type) {
switch (type) {
case 'countries': return countriesDataUrl;
case 'subdiv_japan': return subdivJapanDataUrl;
default: return '';
}
}

let dataUrl = getDataUrl($scope.options.mapType);

function render() {
if (map) {
map.remove();
Expand Down Expand Up @@ -118,7 +129,7 @@ function choroplethRenderer($sanitize, $http) {
layer.bindTooltip($sanitize(formatSimpleTemplate(
$scope.options.tooltip.template,
featureData,
)));
)), { sticky: true });
}

if ($scope.options.popup.enabled) {
Expand Down Expand Up @@ -163,12 +174,16 @@ function choroplethRenderer($sanitize, $http) {
setBounds({ disableAnimation: true });
}

loadCountriesData($http, countriesDataUrl).then((data) => {
if (_.isObject(data)) {
countriesData = data;
render();
}
});
function load() {
loadCountriesData($http, dataUrl).then((data) => {
if (_.isObject(data)) {
countriesData = data;
render();
}
});
}

load();

$scope.handleResize = _.debounce(() => {
if (map) {
Expand All @@ -178,14 +193,18 @@ function choroplethRenderer($sanitize, $http) {
}, 50);

$scope.$watch('queryResult && queryResult.getData()', render);
$scope.$watch(() => _.omit($scope.options, 'bounds'), render, true);
$scope.$watch(() => _.omit($scope.options, 'bounds', 'mapType'), render, true);
$scope.$watch('options.bounds', () => {
// Prevent infinite digest loop
const savedLock = updateBoundsLock;
updateBoundsLock = true;
setBounds();
updateBoundsLock = savedLock;
}, true);
$scope.$watch('options.mapType', () => {
dataUrl = getDataUrl($scope.options.mapType);
load();
}, true);
},
};
}
Expand All @@ -206,6 +225,11 @@ function choroplethEditor(ChoroplethPalette) {

$scope.colors = ChoroplethPalette;

$scope.mapTypes = {
countries: 'Countries',
subdiv_japan: 'Japan/Prefectures',
};

$scope.clusteringModes = {
q: 'quantile',
e: 'equidistant',
Expand All @@ -219,35 +243,64 @@ function choroplethEditor(ChoroplethPalette) {
'bottom-right': 'bottom / right',
};

$scope.countryCodeTypes = {
name: 'Short name',
name_long: 'Full name',
abbrev: 'Abbreviated name',
iso_a2: 'ISO code (2 letters)',
iso_a3: 'ISO code (3 letters)',
iso_n3: 'ISO code (3 digits)',
};
$scope.countryCodeTypes = {};

$scope.templateHint = `
$scope.templateHintFormatter = propDescription => `
<div class="p-b-5">All query result columns can be referenced using <code>{{ column_name }}</code> syntax.</div>
<div class="p-b-5">Use special names to access additional properties:</div>
<div><code>{{ @@value }}</code> formatted value;</div>
<div><code>{{ @@name }}</code> short country name;</div>
<div><code>{{ @@name_long }}</code> full country name;</div>
<div><code>{{ @@abbrev }}</code> abbreviated country name;</div>
<div><code>{{ @@iso_a2 }}</code> two-letter ISO country code;</div>
<div><code>{{ @@iso_a3 }}</code> three-letter ISO country code;</div>
<div><code>{{ @@iso_n3 }}</code> three-digit ISO country code.</div>
${propDescription}
<div class="p-t-5">This syntax is applicable to tooltip and popup templates.</div>
`;

function updateCountryCodeType() {
$scope.options.countryCodeType = inferCountryCodeType(
$scope.options.mapType,
$scope.queryResult.getData(),
$scope.options.countryCodeColumn,
) || $scope.options.countryCodeType;
}

function populateCountryCodeTypes() {
let propDescription = '';
switch ($scope.options.mapType) {
case 'subdiv_japan':
propDescription = `
<div><code>{{ @@name }}</code> Prefecture name in English;</div>
<div><code>{{ @@name_local }}</code> Prefecture name in Kanji;</div>
<div><code>{{ @@iso_3166_2 }}</code> five-letter ISO subdivision code (JP-xx);</div>
`;
$scope.countryCodeTypes = {
name: 'Name',
name_local: 'Name (local)',
iso_3166_2: 'ISO-3166-2',
};
break;
case 'countries':
propDescription = `
<div><code>{{ @@name }}</code> short country name;</div>
<div><code>{{ @@name_long }}</code> full country name;</div>
<div><code>{{ @@abbrev }}</code> abbreviated country name;</div>
<div><code>{{ @@iso_a2 }}</code> two-letter ISO country code;</div>
<div><code>{{ @@iso_a3 }}</code> three-letter ISO country code;</div>
<div><code>{{ @@iso_n3 }}</code> three-digit ISO country code.</div>
`;
$scope.countryCodeTypes = {
name: 'Short name',
name_long: 'Full name',
abbrev: 'Abbreviated name',
iso_a2: 'ISO code (2 letters)',
iso_a3: 'ISO code (3 letters)',
iso_n3: 'ISO code (3 digits)',
};
break;
default:
$scope.countryCodeTypes = {};
}
$scope.templateHint = $scope.templateHintFormatter(propDescription);
}

$scope.$watch('options.mapType', populateCountryCodeTypes);
$scope.$watch('options.countryCodeColumn', updateCountryCodeType);
$scope.$watch('queryResult.getData()', updateCountryCodeType);
},
Expand Down
Loading