d3js 在地图图表中打开一个带有国家/地区 ID 的新选项卡
d3js open a new tab with the country id in map chart
我正在做这个项目,我有一张地图。
我正在使用 geojson link 来获取所有地图的数据。
我想要做的是当我点击任何国家时,将我带到另一个 link,带有该特定国家的 ID。
我必须使用两个数据集,一个用于可视化来自 geojson 文件的地图,另一个用于我的实际数据。如何在 d3
的一个元素中使用两个数据集
截取的代码如下:
var margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var color = d3.scaleThreshold()
.domain([10000, 100000, 500000, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000, 1500000000])
.range(["rgb(247,251,255)", "rgb(222,235,247)", "rgb(198,219,239)", "rgb(158,202,225)", "rgb(107,174,214)", "rgb(66,146,198)", "rgb(33,113,181)", "rgb(8,81,156)", "rgb(8,48,107)", "rgb(3,19,43)"]);
var path = d3.geoPath();
var svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)
.append('g')
.attr('class', 'map');
var projection = d3.geoMercator()
.scale(130)
.translate([width / 2, height / 1.5]);
var path = d3.geoPath().projection(projection);
var myTable = [{
"id": "1",
"name": "China",
"population": "1330141295"
},
{
"id": "2",
"name": "India",
"population": "1173108018"
},
{
"id": "4",
"name": "United States",
"population": "310232800"
},
{
"id": "3",
"name": "Indonesia",
"population": "242968342"
},
{
"id": "5",
"name": "Russia",
"population": "201103330"
}
];
queue()
.defer(d3.json, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_countries.json")
.defer(d3.tsv, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_population.tsv")
.await(ready);
function ready(error, data, population) {
var populationById = {};
population.forEach(function(d) {
populationById[d.id] = +d.population;
});
data.features.forEach(function(d) {
d.population = populationById[d.id]
});
svg.append("g")
.attr("class", "countries")
.selectAll("#map")
.data(data.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(populationById[d.id]);
})
.style('stroke', 'white')
.style('stroke-width', 1.5)
.style("opacity", 0.8)
.on('click', function (d, i) {
var win = window.open('https://www.google.com/'+d.id , '_blank');
win.focus();
})
svg.append("path")
.datum(topojson.mesh(data.features, function(a, b) {
return a.id !== b.id;
}))
// .datum(topojson.mesh(data.features, function(a, b) { return a !== b; }))
.attr("class", "names")
.attr("d", path);
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.0/d3-tip.js"></script>
<div id="map"></div>
注意:onclick 函数现在一切正常,但如果我点击俄罗斯,例如,它会给我俄罗斯的 geojson 文件的 ID,即“RUS”,但实际上我需要 myTable 中的 ID俄罗斯是“5”。
您可以从用于地图的数据基准中获取国家/地区名称,然后在 myTable 变量中查找 ID。
那么,您的活动将是
.on('click', datum => {
//Get the name of the country
console.log(datum.properties.name);
// Look up id by the name
data = myTable.find(e => e.name === datum.properties.name)
console.log(data.id) // here is your country id
var win = window.open('https://www.google.com/'+data.id , '_blank');
win.focus();
})
编辑:添加了一个代码片段,打印了带有国家 ID 的 URL。如果你点击俄罗斯,你应该可以看到它:)
var margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var color = d3.scaleThreshold()
.domain([10000, 100000, 500000, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000, 1500000000])
.range(["rgb(247,251,255)", "rgb(222,235,247)", "rgb(198,219,239)", "rgb(158,202,225)", "rgb(107,174,214)", "rgb(66,146,198)", "rgb(33,113,181)", "rgb(8,81,156)", "rgb(8,48,107)", "rgb(3,19,43)"]);
var path = d3.geoPath();
var svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)
.append('g')
.attr('class', 'map');
var projection = d3.geoMercator()
.scale(130)
.translate([width / 2, height / 1.5]);
var path = d3.geoPath().projection(projection);
var myTable = [{
"id": "1",
"name": "China",
"population": "1330141295"
},
{
"id": "2",
"name": "India",
"population": "1173108018"
},
{
"id": "4",
"name": "United States",
"population": "310232800"
},
{
"id": "3",
"name": "Indonesia",
"population": "242968342"
},
{
"id": "5",
"name": "Russia",
"population": "201103330"
}
];
queue()
.defer(d3.json, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_countries.json")
.defer(d3.tsv, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_population.tsv")
.await(ready);
function ready(error, data, population) {
var populationById = {};
population.forEach(function(d) {
populationById[d.id] = +d.population;
});
data.features.forEach(function(d) {
d.population = populationById[d.id]
});
svg.append("g")
.attr("class", "countries")
.selectAll("#map")
.data(data.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(populationById[d.id]);
})
.style('stroke', 'white')
.style('stroke-width', 1.5)
.style("opacity", 0.8)
.on('click', datum => {
//Get the name of the country
console.log(datum.properties.name);
// Look up id by the name
data = myTable.find(e => e.name === datum.properties.name)
console.log('https://www.google.com/'+data.id) // here is your country id
})
svg.append("path")
.datum(topojson.mesh(data.features, function(a, b) {
return a.id !== b.id;
}))
// .datum(topojson.mesh(data.features, function(a, b) { return a !== b; }))
.attr("class", "names")
.attr("d", path);
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.0/d3-tip.js"></script>
<div id="map"></div>
我正在做这个项目,我有一张地图。 我正在使用 geojson link 来获取所有地图的数据。 我想要做的是当我点击任何国家时,将我带到另一个 link,带有该特定国家的 ID。 我必须使用两个数据集,一个用于可视化来自 geojson 文件的地图,另一个用于我的实际数据。如何在 d3
的一个元素中使用两个数据集截取的代码如下:
var margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var color = d3.scaleThreshold()
.domain([10000, 100000, 500000, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000, 1500000000])
.range(["rgb(247,251,255)", "rgb(222,235,247)", "rgb(198,219,239)", "rgb(158,202,225)", "rgb(107,174,214)", "rgb(66,146,198)", "rgb(33,113,181)", "rgb(8,81,156)", "rgb(8,48,107)", "rgb(3,19,43)"]);
var path = d3.geoPath();
var svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)
.append('g')
.attr('class', 'map');
var projection = d3.geoMercator()
.scale(130)
.translate([width / 2, height / 1.5]);
var path = d3.geoPath().projection(projection);
var myTable = [{
"id": "1",
"name": "China",
"population": "1330141295"
},
{
"id": "2",
"name": "India",
"population": "1173108018"
},
{
"id": "4",
"name": "United States",
"population": "310232800"
},
{
"id": "3",
"name": "Indonesia",
"population": "242968342"
},
{
"id": "5",
"name": "Russia",
"population": "201103330"
}
];
queue()
.defer(d3.json, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_countries.json")
.defer(d3.tsv, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_population.tsv")
.await(ready);
function ready(error, data, population) {
var populationById = {};
population.forEach(function(d) {
populationById[d.id] = +d.population;
});
data.features.forEach(function(d) {
d.population = populationById[d.id]
});
svg.append("g")
.attr("class", "countries")
.selectAll("#map")
.data(data.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(populationById[d.id]);
})
.style('stroke', 'white')
.style('stroke-width', 1.5)
.style("opacity", 0.8)
.on('click', function (d, i) {
var win = window.open('https://www.google.com/'+d.id , '_blank');
win.focus();
})
svg.append("path")
.datum(topojson.mesh(data.features, function(a, b) {
return a.id !== b.id;
}))
// .datum(topojson.mesh(data.features, function(a, b) { return a !== b; }))
.attr("class", "names")
.attr("d", path);
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.0/d3-tip.js"></script>
<div id="map"></div>
注意:onclick 函数现在一切正常,但如果我点击俄罗斯,例如,它会给我俄罗斯的 geojson 文件的 ID,即“RUS”,但实际上我需要 myTable 中的 ID俄罗斯是“5”。
您可以从用于地图的数据基准中获取国家/地区名称,然后在 myTable 变量中查找 ID。
那么,您的活动将是
.on('click', datum => {
//Get the name of the country
console.log(datum.properties.name);
// Look up id by the name
data = myTable.find(e => e.name === datum.properties.name)
console.log(data.id) // here is your country id
var win = window.open('https://www.google.com/'+data.id , '_blank');
win.focus();
})
编辑:添加了一个代码片段,打印了带有国家 ID 的 URL。如果你点击俄罗斯,你应该可以看到它:)
var margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var color = d3.scaleThreshold()
.domain([10000, 100000, 500000, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000, 1500000000])
.range(["rgb(247,251,255)", "rgb(222,235,247)", "rgb(198,219,239)", "rgb(158,202,225)", "rgb(107,174,214)", "rgb(66,146,198)", "rgb(33,113,181)", "rgb(8,81,156)", "rgb(8,48,107)", "rgb(3,19,43)"]);
var path = d3.geoPath();
var svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)
.append('g')
.attr('class', 'map');
var projection = d3.geoMercator()
.scale(130)
.translate([width / 2, height / 1.5]);
var path = d3.geoPath().projection(projection);
var myTable = [{
"id": "1",
"name": "China",
"population": "1330141295"
},
{
"id": "2",
"name": "India",
"population": "1173108018"
},
{
"id": "4",
"name": "United States",
"population": "310232800"
},
{
"id": "3",
"name": "Indonesia",
"population": "242968342"
},
{
"id": "5",
"name": "Russia",
"population": "201103330"
}
];
queue()
.defer(d3.json, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_countries.json")
.defer(d3.tsv, "https://gist.githubusercontent.com/micahstubbs/8e15870eb432a21f0bc4d3d527b2d14f/raw/a45e8709648cafbbf01c78c76dfa53e31087e713/world_population.tsv")
.await(ready);
function ready(error, data, population) {
var populationById = {};
population.forEach(function(d) {
populationById[d.id] = +d.population;
});
data.features.forEach(function(d) {
d.population = populationById[d.id]
});
svg.append("g")
.attr("class", "countries")
.selectAll("#map")
.data(data.features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(populationById[d.id]);
})
.style('stroke', 'white')
.style('stroke-width', 1.5)
.style("opacity", 0.8)
.on('click', datum => {
//Get the name of the country
console.log(datum.properties.name);
// Look up id by the name
data = myTable.find(e => e.name === datum.properties.name)
console.log('https://www.google.com/'+data.id) // here is your country id
})
svg.append("path")
.datum(topojson.mesh(data.features, function(a, b) {
return a.id !== b.id;
}))
// .datum(topojson.mesh(data.features, function(a, b) { return a !== b; }))
.attr("class", "names")
.attr("d", path);
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.0/d3-tip.js"></script>
<div id="map"></div>