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 #439: Add dataUrl error handling callback function #440

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ If the aspect ratio of your custom map is not the default `16:9` (`0.5625`), you
width: null, // If not null, datamaps will grab the width of 'element',
responsive: false, // If true, call `resize()` on the map object when it should adjust it's size
done: function() {}, // Callback when the map is done drawing
error: null, // If not null, callback when the map cannot fetch the dataUrl
fills: {
defaultFill: '#ABDDA4' // The keys in this object map to the "fillKey" of [data] or [bubbles]
},
Expand Down
22 changes: 22 additions & 0 deletions src/examples/custom-map-data-error-handling.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="/src/rel/datamaps.world.js"></script>
<h2>Custom Map Data Error Handling</h2>
<div id="errorContainer" style="color: green; padding: 10px; font-size: 16px;" />
<div id="container1" style="position: relative; width: 500px; height: 450px;"></div>
<script>
var map = new Datamap({
element: document.getElementById('container1'),
geographyConfig: {
dataUrl: 'NON_EXISTANT.json'
},
error: function(e) {
const domEl = document.getElementById('errorContainer');
domEl.innerText = `Caught error when fetching data: ${e}`;
}
});
</script>
</body>
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare interface DataMapOptions {
path: d3.geo.Path;
projection: d3.geo.Projection;
}) => void;
error?: (error: any) => void;
responsive?: boolean;
projection?: string;
height?: null | number;
Expand Down
10 changes: 9 additions & 1 deletion src/js/datamaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
dataType: 'json',
data: {},
done: function() {},
error: null,
fills: {
defaultFill: '#ABDDA4'
},
Expand Down Expand Up @@ -765,7 +766,14 @@
// If custom URL for topojson data, retrieve it and render
if ( options.geographyConfig.dataUrl ) {
d3.json( options.geographyConfig.dataUrl, function(error, results) {
if ( error ) throw new Error(error);
if ( error ) {
if ( self.options.error ) {
self.options.error(error);
return;
}

throw new Error(error);
}
self.customTopo = results;
draw( results );
});
Expand Down