Closed davelandry closed 7 years ago
hey so I'm still a little stuck (I've always had trouble with mapping). This is the code I have now to try to map the IDs and alpha codes
`featureCollection.features.map(function(row, index){
if(!row["id"])return false;
if(row["id"])
return row["id"] = d3.values(num2alpha);
});`
but obviously that's not the way to go about it, so I'm thinking there's something similar to d3.values that maybe I should be using instead?
Thanks!
You're still going to need to use featureCollection.features
as the data for the country paths, so you should create an Object to look up the data based on the country code.
You're on the right track by storing the data into a global variable (AllData
), but instead of making it an Array, make it an Object where the key is the country code. First, get rid of line 65 where you initialize it as an array, and simply initialize it as an empty object on line 56:
var AllData = {};
Then, instead of pushing into the Array on line 103, add it as a key of the object:
AllData[row['Country Code']] = {name: row['Country Name'], years: yearlyData, code: row['Country Code']};
Now, in your getCircleColor
, which gets passed the data point from featureCollection.features
, you can grab the greenhouse data like this:
var countryCode = num2alpha[d.id];
var greenhouseData = AllData[countryCode];
Hope that helps, let me know where that gets you. I'll be online tomorrow morning if you have any more questions.
hey, so new question. I'm trying to output the greenhouseData.years object into a div so that it'll look like this:
1990: somenumber
1991: somenumber
1992: somenumber
etc.
I'm trying to get it to work with this code but it's coming up undefined:
.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; var countryData = JSON.stringify(greenhouseData.years); function print(){ var printThis; for(var i = 0; i < 48; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); document.getElementById('Name').innerHTML = greenhouseData.name; document.getElementById('code').innerHTML = greenhouseData.code; document.getElementById('data').innerHTML = print(); });
What am I doing wrong?
Thanks, Bayley
The keys in the greenhouseData.years
Object are actual years, not numbers between 0 and 48. Try this out:
.on("click", function(d) {
var countryCode = num2alpha[d.id];
var greenhouseData = AllData[countryCode];
function print() {
var printThis;
for(var i = 1990; i <= 1999; i++){
printThis = "\n" + greenhouseData.years[i];
}
return printThis; // <-- to be printed to the div
}
console.log("click");
d3.select('#Name').html(greenhouseData.name);
d3.select('#code').html(greenhouseData.code);
d3.select('#data').html(print());
});
Unfortunately still not working. I just pushed all the recent changes, not sure if you want to take a look. I'm going to keep googling things.
Thanks, Bayley
On Thu, Dec 1, 2016 at 6:09 PM, Dave Landry notifications@github.com wrote:
The keys in the greenhouseData.years Object are actual years, not numbers between 0 and 48. Try this out:
.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis; for(var i = 1990; i <= 1999; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264324168, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v9kCe4Lm5_C6Cj1otVqwcBKSbxcIks5rD1O6gaJpZM4K29Xk .
I've hardcoded it in for now, it works but if you know of an easier way I'd love to know!
On Thu, Dec 1, 2016 at 8:14 PM, Bayley Meichner meichner.b@husky.neu.edu wrote:
Unfortunately still not working. I just pushed all the recent changes, not sure if you want to take a look. I'm going to keep googling things.
Thanks, Bayley
On Thu, Dec 1, 2016 at 6:09 PM, Dave Landry notifications@github.com wrote:
The keys in the greenhouseData.years Object are actual years, not numbers between 0 and 48. Try this out:
.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis; for(var i = 1990; i <= 1999; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264324168, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v9kCe4Lm5_C6Cj1otVqwcBKSbxcIks5rD1O6gaJpZM4K29Xk .
That's my bad. The code I posted was overwritting the printThis
variable each time through the for loop instead of adding to it. Try this:
.on("click", function(d) {
var countryCode = num2alpha[d.id];
var greenhouseData = AllData[countryCode];
function print() {
var printThis = "";
for(var i = 1990; i <= 1999; i++){
printThis += i + ": " + greenhouseData.years[i] + "<br/>";
}
return printThis; // <-- to be printed to the div
}
console.log(print());
d3.select('#Name').html(greenhouseData.name);
d3.select('#code').html(greenhouseData.code);
d3.select('#data').html(print());
});
Perfect! Thank you!
On Sun, Dec 4, 2016 at 9:16 PM, Dave Landry notifications@github.com wrote:
That's my bad. The code I posted was overwritting the printThis variable each time through the for loop instead of adding to it. Try this:
.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis = ""; for(var i = 1990; i <= 1999; i++){ printThis += i + ": " + greenhouseData.years[i] + "
"; } return printThis; // <-- to be printed to the div } console.log(print()); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264753221, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v3EGgsDI_vI7gTaEy0LA5BGLa9Bwks5rE3QQgaJpZM4K29Xk .
Here's a javascript object keyed by the IDs in the topojson file. Use this to get to the alpha codes in your data: