Comments (7)
Do you ever pass data
to Datamaps? I would either put the new Datamaps
call in the CSV callback or call myMap.updateChoropleth(data)
once you've
got the data object setup.
On Sunday, March 2, 2014, xammamax [email protected] wrote:
The library is fantastic and I'm using it a lot right now! It would make
my work much easier if it was possible to read the data from a .csv file.
Did anybody try this before? What am I doing wrong?I would build a jsfiddle but I don't know how to use a csv file in these
IDEs. Sorry about that.What I'm trying to do is the following:
The csv file looks like this:
ISO,Rate,fillKey
SLE,181.6,>100
HTI,75.6,50-100
ZAF,44.6,20-50
NPL,41.6,20-50
BOL,41.4,20-50And then I'm trying to use the following:
var data;
d3.csv("data.csv", function (error, csv) {
if (error) return console.log("error loading the csv: " + error);
console.log("there are " + csv.length + " elements in my csv set");var nestFunction = d3.nest().key(function(d){return d.ISO;});
data = nestFunction.entries(
csv.map(function(d){
d.Rate = +d.Rate;d.fillKey = d.fillKey;
return d;
})
);
console.log("there are " + data.length + " elements in my data");
});
var myMap = new Datamap({
element: document.getElementById('map'),
scope: 'world',
geographyConfig: {
popupOnHover: false,
highlightOnHover: false
},
fills: {
'>100': '#1f77b4',
'50-100': '#9467bd',
'20-50': '#ff7f0e',
defaultFill: '#EDDC4E'
}
});Reply to this email directly or view it on GitHubhttps://github.com//issues/61
.
Mark DiMarco
Phone: 512.745.6267
Email: [email protected]
from datamaps.
Thank you for your answer!
I managed to read data from a .csv file now!
Here is my example: http://www.explainingprogress.com/wp-content/uploads/datamaps/uploaded_gdpPerCapita2011_PWTrgdpe/gdpPerCapita2011_PWTrgdpe.html
The data is stored in a csv file with the following structure:
ISO,gdppercapita
AGO,4214.452
ALB,7364.711
ARG,14507.62
ARM,5235.04
ATG,12908.97
AUS,38499.27
AUT,37282.53
...
And with the following code I'm reading the data, creating the fillKey and calling the map:
var bucketLimits = [1000,2000,5000,10000,20000,30000,40000,50000];
d3.csv("gdpPerCapita2011_PWTrgdpe.csv", function(error, csvdata1) {
for (var i=0;i<csvdata1.length;i++)
{
csvdata1[i].fillKey = {};
if (csvdata1[i].gdppercapita < bucketLimits[0]) { csvdata1[i].fillKey = '0'; }
if (csvdata1[i].gdppercapita >= bucketLimits[0] && csvdata1[i].gdppercapita < bucketLimits[1]) { csvdata1[i].fillKey = '1'; }
if (csvdata1[i].gdppercapita >= bucketLimits[1] && csvdata1[i].gdppercapita < bucketLimits[2]) { csvdata1[i].fillKey = '2'; }
if (csvdata1[i].gdppercapita >= bucketLimits[2] && csvdata1[i].gdppercapita < bucketLimits[3]) { csvdata1[i].fillKey = '3'; }
if (csvdata1[i].gdppercapita >= bucketLimits[3] && csvdata1[i].gdppercapita < bucketLimits[4]) { csvdata1[i].fillKey = '4'; }
if (csvdata1[i].gdppercapita >= bucketLimits[4] && csvdata1[i].gdppercapita < bucketLimits[5]) { csvdata1[i].fillKey = '5'; }
if (csvdata1[i].gdppercapita >= bucketLimits[5] && csvdata1[i].gdppercapita < bucketLimits[6]) { csvdata1[i].fillKey = '6'; }
if (csvdata1[i].gdppercapita >= bucketLimits[6] && csvdata1[i].gdppercapita < bucketLimits[7]) { csvdata1[i].fillKey = '7'; }
if (csvdata1[i].gdppercapita >= bucketLimits[7] ) { csvdata1[i].fillKey = '8'; }
csvdata1[ csvdata1[i].ISO] = csvdata1[i] ;
//console.log(csvdata1[i]);
delete csvdata1[i].ISO;
delete csvdata1[i] ;
}
myMap1.updateChoropleth(csvdata1);
}
);
Everything is working except the tooltips..
popupTemplate: function(geography, csvdata1) {
if ( !csvdata1 ) return; // makes sure that the tooltip is shown only if there is data
return '<div class="hoverinfo">' + geography.properties.name + '<br> GDP per Capita <strong>' + csvdata1.gdppercapita + '</strong> 2005 US Dollars';},
},
Could you tell me what I'm doing wrong here?
from datamaps.
Try replacing your datamaps.world.min.js file with this one and let me know if the popup issue still persists.
from datamaps.
Thanks for getting back to me Mark!
I replaced the file but unfortunately it made things worse. All countries are shown in the defaultFill color and the popup issue is not resolved either.
I also updated the datamaps.world.min.js for the map that I was linking to above.
from datamaps.
I see the issue on my side I believe: add an empty data
object when you first draw the map.
Like this:
var myMap2 = new Datamap(
{
scope: 'world',
projection: 'equirectangular',
element: document.getElementById('map'),
// projection: 'mercator',
geographyConfig: {
borderWidth: 0.2,
borderColor: '#4F4F4F',
/* highlightBorderColor: 'black',
highlightBorderWidth: 0.5,
highlightFillColor: '#FFEC38',
highlightOnHover: true,
popupOnHover: true, //disable the popup while hovering
*/
popupTemplate: function(geography, csvdata1) {
if ( !csvdata1 ) return; // makes sure that the tooltip is shown only if there is data
return '<div class="hoverinfo">' + geography.properties.name + '<br> GDP per Capita <strong>' + csvdata1.gdppercapita + '</strong> 2005 US Dollars';},
},
fills: {
'0': colorschemeOfThisMap[0],
'1': colorschemeOfThisMap[1],
'2': colorschemeOfThisMap[2],
'3': colorschemeOfThisMap[3],
'4': colorschemeOfThisMap[4],
'5': colorschemeOfThisMap[5],
'6': colorschemeOfThisMap[6],
'7': colorschemeOfThisMap[7],
'8': colorschemeOfThisMap[8],
defaultFill: 'grey'
},
data: {}
});
from datamaps.
Now it works!!
Thanks for fixing this so very quickly!!
I'm using the updated datamaps.world.min.js (the older version is not working).
And I added an an empty data object.
My online map is updated if somebody is interested.
from datamaps.
Looks great! Glad I was able to help.
from datamaps.
Related Issues (20)
- Some of Countries missing in DataMap HOT 1
- I want to draw lines/paths on india map? Can someone please guide?
- get lat lng somehow from click event
- Missing Nantucket and Marthas Vineyard in Datampas.usa.js HOT 1
- Is it possible to change border colour of specific countries using updateChoropleth?
- Unrecognizable src script HOT 3
- Function to use custom map data HOT 1
- choropleth map color not updating when highlight on hover set to true
- React Native
- Custom-map-data-multi-layer example no longer works
- Text on bubbles
- Part of Russia is coming to the left of America HOT 1
- unable to import datamaps in angular app HOT 2
- How to have 2 maps using the same source, but with different information on hover tooltip? HOT 1
- Zoom to specified latitude and longitude
- Adding datamap to Vue 3
- `animationEnd` and `animationStart` as a configurable option for `map.arcConfig`
- rendering issues in safari 16.3
- Simple installation question
- Map for switzerland (swz?) not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datamaps.