I am new to d3.js and JSON. I am learning by doing some small small visualizations. Now, I am trying to load a JSON data and visualize the details based on the country code.
我是d3.js和JSON的新手。我正在通过做一些小的可视化来学习。现在,我正在尝试加载JSON数据并根据国家/地区代码可视化详细信息。
My JSON data is like :
我的JSON数据如下:
[
{
"Id":"SWE",
"Country":"Sweden",
"Population":9592552
},
{
"Id":"NOR",
"Country":"Norway",
"Population":5084190
},
.
.
.
]
I have world countries geo JSON which I can able to visualize successfully and also able to highlight the selected country and get the selected country's id. Now I need to get the details (population and country name of that country based on the id I got from selection). Can Some one tell how can I get the values from the JSON array.
我有世界国家地理JSON,我能够成功地进行可视化,并能够突出显示所选国家/地区并获得所选国家/地区的ID。现在我需要根据我从选择中获得的ID获取详细信息(该国家/地区的人口和国家/地区名称)。有人可以告诉我如何从JSON数组中获取值。
I tried like
我试过了
population = data.map( function(d) { return d["Population"] });
but this one gives me entire populations as an array. How do I get population for SWE based on Id ?
但是这个给了我整个人口作为一个阵列。如何根据Id获得SWE的人口?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
</style>
<script type="text/javascript">
var data;
function draw(geo_data) {
"use strict";
var margin = 75,
width = 1400 - margin,
height = 600 - margin;
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class', 'map');
var projection = d3.geo.mercator()
.scale(150)
.translate( [width / 2, height / 1.5]);
var path = d3.geo.path().projection(projection);
var map = svg.selectAll('path')
.data(geo_data.features)
.enter()
.append('path')
.attr('d', path)
.style('fill', 'lightBlue')
.style('stroke', 'black')
.attr("id", function(d) {
return d.id; })
.on("click", function(d, i) {
d3.select(".selected").classed("selected", false).style('fill', 'lightBlue');
d3.select(this).classed("selected", true).style('fill', 'red');
console.log(d.id)
display(d.id)
})
.style('stroke-width', 0.5);
};
d3.json("data/wrangledData_overallScore.json", function (error, json) {
if (error) return console.warn(error);
data = json;
console.log("JSON", data);
});
function display(e){
var population = data.map( function(d) { return d["Population"] });
console.log(population)
}
</script>
</head>
<body>
<script type="text/javascript">
/*
Use D3 to load the GeoJSON file
*/
d3.json("data/world-countries.json", draw);
</script>
</body>
</html>
I can get the index of the id and use that for finding population in the population array. But I need to find based on Id. Can Some one please tell me how can I get this.
我可以获取id的索引并使用它来查找population数组中的种群。但我需要根据Id找到。有人可以告诉我怎样才能得到这个。
2 个解决方案
#1
4
First, since you are new to JSON, a little terminology correction to help you out: JSON is a string and not an object hence it's abbreviation of JavaScript Object Notation. What you have is colloquially referred to as a POJO or Plain Old Javascript Object. They are different.
首先,由于您不熟悉JSON,因此需要进行一些术语更正来帮助您:JSON是一个字符串而不是对象,因此它是JavaScript Object Notation的缩写。你所拥有的通俗地称为POJO或Plain Old Javascript Object。他们是不同的。
Now for your question. You have two approaches:
现在提出你的问题。你有两种方法:
- You can use a poly-fill for an upcoming ECMA 6 array method and future proof your answer
- 您可以使用poly-fill来实现即将推出的ECMA 6阵列方法,并为您的答案提供未来证明
- Or you can roll your own solution using ECMA 5 functionality
- 或者您可以使用ECMA 5功能推出自己的解决方案
The first solution would be to use the poly-fill provided in the documentation for find
:
第一个解决方案是使用文档中提供的poly-fill进行查找:
var countryData = data.find(function(element, index, array) {
return element.Id === 'SWE';
});
countryData.Population // 9592552
The second method is basically recreating the poly-fill in a whatever manner you choose and if you choose that option I'll leave that up to you as an exercise to learn from.
第二种方法基本上是以你选择的任何方式重新创建多重填充,如果你选择那个选项,我会把它留给你作为练习来学习。
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
var data = [{
"Id": "SWE",
"Country": "Sweden",
"Population": 9592552
}, {
"Id": "NOR",
"Country": "Norway",
"Population": 5084190
}];
function display(e) {
console.log("E", e);
var countryData = data.find(function(element, index, array) {
return element.Id === e;
});
console.log(countryData.Population);
}
display('SWE');
#2
1
Just loop thru them until you see the right id.
只需循环直到你看到正确的ID。
for (var i in data) {
if (data[i].Id == 'SWE') console.log(data[i]['Population']);
}
You could also use a while loop or regular for loop. Here every i
"key" in data is tested, so each piece of data is data[i]
.
您还可以使用while循环或常规循环。这里测试了数据中的每个“关键”,因此每个数据都是数据[i]。
#1
4
First, since you are new to JSON, a little terminology correction to help you out: JSON is a string and not an object hence it's abbreviation of JavaScript Object Notation. What you have is colloquially referred to as a POJO or Plain Old Javascript Object. They are different.
首先,由于您不熟悉JSON,因此需要进行一些术语更正来帮助您:JSON是一个字符串而不是对象,因此它是JavaScript Object Notation的缩写。你所拥有的通俗地称为POJO或Plain Old Javascript Object。他们是不同的。
Now for your question. You have two approaches:
现在提出你的问题。你有两种方法:
- You can use a poly-fill for an upcoming ECMA 6 array method and future proof your answer
- 您可以使用poly-fill来实现即将推出的ECMA 6阵列方法,并为您的答案提供未来证明
- Or you can roll your own solution using ECMA 5 functionality
- 或者您可以使用ECMA 5功能推出自己的解决方案
The first solution would be to use the poly-fill provided in the documentation for find
:
第一个解决方案是使用文档中提供的poly-fill进行查找:
var countryData = data.find(function(element, index, array) {
return element.Id === 'SWE';
});
countryData.Population // 9592552
The second method is basically recreating the poly-fill in a whatever manner you choose and if you choose that option I'll leave that up to you as an exercise to learn from.
第二种方法基本上是以你选择的任何方式重新创建多重填充,如果你选择那个选项,我会把它留给你作为练习来学习。
if (!Array.prototype.find) {
Array.prototype.find = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
return undefined;
};
}
var data = [{
"Id": "SWE",
"Country": "Sweden",
"Population": 9592552
}, {
"Id": "NOR",
"Country": "Norway",
"Population": 5084190
}];
function display(e) {
console.log("E", e);
var countryData = data.find(function(element, index, array) {
return element.Id === e;
});
console.log(countryData.Population);
}
display('SWE');
#2
1
Just loop thru them until you see the right id.
只需循环直到你看到正确的ID。
for (var i in data) {
if (data[i].Id == 'SWE') console.log(data[i]['Population']);
}
You could also use a while loop or regular for loop. Here every i
"key" in data is tested, so each piece of data is data[i]
.
您还可以使用while循环或常规循环。这里测试了数据中的每个“关键”,因此每个数据都是数据[i]。