我正在做這個專案,我有一張地圖。我正在使用 geojson 鏈接來獲取所有地圖的資料。我想要做的是當我單擊任何國家/地區時,將我帶到另一個鏈接,其中包含該特定國家/地區的 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”俄羅斯。
uj5u.com熱心網友回復:
您可以從用于地圖的資料資料中獲取國家/地區名稱,然后在 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326041.html
標籤:javascript 查询 d3.js 数据可视化
上一篇:D3面積圖未正確渲染
下一篇:如何拆分字串并向其添加額外的字串
