gpt4 book ai didi

javascript - 如何在 map 上显示/隐藏?

转载 作者:行者123 更新时间:2023-12-03 09:24:09 25 4
gpt4 key购买 nike

到目前为止,我终于创建了一个地理 map ,其中包含节点以及连接它们的线。所有节点都有持有人 ID 和母公司。但是我想要一个功能,您可以双击节点,它只会显示所选的节点及其连接的节点。我不知道该怎么做,因为我不知道如何将我创建的线连接到节点。我在 JSFiddle 上重新创建了我的示例:http://jsfiddle.net/GarrettUK/x9058g8s/2/ 。如有任何问题,请随时提出,并提前致谢!

holderID 是公司,parentCompany 是拥有所选节点的节点:

name: 'Tesco',
radius: 6,
fillKey: 'Negative',
holderID: 2,
parentCompany: 1,
balance: '$132393',
date: '11/07/2005',
latitude: 48.632909,
longitude: 35.024414

最佳答案

您可以选择 D3.js 中的节点链接:

var bnodes = d3.selectAll('.datamaps-bubble');
var blinks = d3.selectAll('.datamaps-arc');

然后您可以计算附加信息,以便在双击时更有效地执行节点和链接的选择,例如:

blinks.each(function(dl, il) {
bnodes.each(function(d,i) {
if (dl.origin.latitude == d.latitude) {
dl.origin.holderID = d.holderID;
} else if (dl.destination.longitude == d.longitude) {
dl.destination.holderID = d.holderID;
}
})
});

然后双击事件仅显示链接的节点和链接:

bnodes.on('dblclick', function(d, i) {
blinks.classed('hide', true);
bnodes.classed('hide', true);

connectedNodes = [d.holderID];

blinks.filter(function(dl,il) {
if (dl.origin.holderID == d.holderID) {
connectedNodes.push(dl.destination.holderID);
return true;
} else if (dl.destination.holderID == d.holderID) {
connectedNodes.push(dl.origin.holderID);
return true;
}
return false;
}).classed('hide', false);

bnodes.filter(function(dd,ii) {
return connectedNodes.indexOf(dd.holderID) > -1;
}).classed('hide', false);
});

最后是 fiddle :http://jsfiddle.net/x9058g8s/5/ 和片段:

//creating the map
var map = new Datamap({
element: document.getElementById('map'),
scope: 'world',
geographyConfig: {
popupOnHover: false,
highlightOnHover: false
},
fills: {
//node colour
'Positive': '#007D1C',
'Negative': '#A51000',
//map colour
defaultFill: '#9AAEBF',
},
arcConfig: {
strokeColor: 'black',
strokeWidth: 10,
arcSharpness: 1,
animationSpeed: 600
},
});

var nodes = [{
name: 'LRZ',
radius: 6,
fillKey: 'Positive',
holderID: 1,
parentCompany: 0,
balance: '$10032393',
date: '17/07/2015',
latitude: 51.727028,
longitude: -0.395508
},
{
name: 'Tesco',
radius: 6,
fillKey: 'Negative',
holderID: 2,
parentCompany: 1,
balance: '$132393',
date: '11/07/2005',
latitude: 48.632909,
longitude: 35.024414
},
{
name: 'Apple',
radius: 6,
fillKey: 'Positive',
holderID: 3,
parentCompany: 1,
balance: '$100393',
date: '01/03/2045',
latitude: 37.244200,
longitude: -115.815167
},
{
name: 'BP',
radius: 6,
fillKey: 'Negative',
holderID: 4,
parentCompany: 1,
balance: '$10032393',
date: '17/07/2015',
latitude: 62.955223,
longitude: 109.555664
},
{
name: 'Polycom',
radius: 6,
fillKey: 'Negative',
holderID: 5,
parentCompany: 2,
balance: '$10032393',
date: '17/07/2015',
latitude: 62.955223,
longitude: 109.555664
},
{
name: 'Nike',
radius: 6,
fillKey: 'Negative',
holderID: 6,
parentCompany: 0,
balance: '$10032393',
date: '17/07/2015',
latitude: -25.799891,
longitude: 142.207031
},
{
name: 'HTC',
radius: 6,
fillKey: 'Positive',
holderID: 7,
parentCompany: 3,
balance: '$10032393',
date: '17/07/2015',
latitude: -3.513421,
longitude: 24.082031
},
{
name: 'Microsoft',
radius: 6,
fillKey: 'Positive',
holderID: 8,
parentCompany: 4,
balance: '$10032393',
date: '17/07/2015',
latitude: -12.554564,
longitude: -43.769531
},
{
name: 'HP',
radius: 6,
fillKey: 'Negative',
holderID: 9,
parentCompany: 4,
balance: '$10032393',
date: '17/07/2015',
latitude: 52.696361,
longitude: -93.339844
},
{
name: 'Poundland',
radius: 6,
fillKey: 'Negative',
holderID: 10,
parentCompany: 4,
balance: '$10032393',
date: '17/07/2015',
latitude: 7.710992,
longitude: -5.097656
},
{
name: 'BBC',
radius: 6,
fillKey: 'Positive',
holderID: 11,
parentCompany: 6,
balance: '$10032393',
date: '17/07/2015',
latitude: 71.187754,
longitude: -36.035156
},
{
name: 'Sony',
radius: 6,
fillKey: 'Negative',
holderID: 12,
parentCompany: 6,
balance: '$10032393',
date: '17/07/2015',
latitude: 22.268764,
longitude: 78.574219
},
{
name: 'LG',
radius: 6,
fillKey: 'Positive',
holderID: 13,
parentCompany: 9,
balance: '$10032393',
date: '17/07/2015',
latitude: 65.072130,
longitude: 14.238281
}
];

var arcs = [
//red lines
{
origin: {
latitude: 48.632909,
longitude: 35.024414
},
destination: {
latitude: 51.727028,
longitude: -0.395508
},
},
{
origin: {
latitude: 37.244200,
longitude: -115.815167
},
destination: {
latitude: 51.727028,
longitude: -0.395508
},
},
{
origin: {
latitude: 62.955223,
longitude: 109.555664
},
destination: {
latitude: 51.727028,
longitude: -0.395508
},
},
{
origin: {
latitude: 48.632909,
longitude: 35.024414
},
destination: {
latitude: 62.955223,
longitude: 109.555664
},
},
{
origin: {
latitude: 37.244200,
longitude: -115.815167
},
destination: {
latitude: -3.513421,
longitude: 24.082031
},
},
{
origin: {
latitude: -12.554564,
longitude: -43.769531
},
destination: {
latitude: 62.955223,
longitude: 109.555664
},
},
{
origin: {
latitude: 52.696361,
longitude: -93.339844
},
destination: {
latitude: 62.955223,
longitude: 109.555664
},
},
{
origin: {
latitude: 7.710992,
longitude: -5.097656
},
destination: {
latitude: 62.955223,
longitude: 109.555664
},
},
{
origin: {
latitude: 71.187754,
longitude: -36.035156
},
destination: {
latitude: -25.799891,
longitude: 142.207031
},
},
{
origin: {
latitude: 22.268764,
longitude: 78.574219
},
destination: {
latitude: -25.799891,
longitude: 142.207031
},
},
{
origin: {
latitude: 65.072130,
longitude: 14.238281
},
destination: {
latitude: 52.696361,
longitude: -93.339844
},
},
];
map.arc(arcs, {strokeWidth: 1, arcSharpness: 1.4});

//draw bubbles for nodes
map.bubbles(nodes, {
popupTemplate: function(geo, data) {
return ['<div class="hoverinfo">' + 'Name: ' + data.name,
'<br/>HolderID: ' + data.holderID,
'<br/>Balance: ' + data.balance + '',
'<br/>Date: ' + data.date + '',
'</div>'
].join('');
}
});

var bnodes = d3.selectAll('.datamaps-bubble');
var blinks = d3.selectAll('.datamaps-arc');
blinks.each(function(dl, il) {
bnodes.each(function(d,i) {
if (dl.origin.latitude == d.latitude) {
dl.origin.holderID = d.holderID;
} else if (dl.destination.longitude == d.longitude) {
dl.destination.holderID = d.holderID;
}
})
});

bnodes.on('dblclick', function(d, i) {
blinks.classed('hide', true);
bnodes.classed('hide', true);

connectedNodes = [d.holderID];

blinks.filter(function(dl,il) {
if (dl.origin.holderID == d.holderID) {
connectedNodes.push(dl.destination.holderID);
return true;
} else if (dl.destination.holderID == d.holderID) {
connectedNodes.push(dl.origin.holderID);
return true;
}
return false;
}).classed('hide', false);

bnodes.filter(function(dd,ii) {
return connectedNodes.indexOf(dd.holderID) > -1;
}).classed('hide', false);
});
body {
padding: 0px;
margin: 0px;
background-color: #C0D8ED;
}

div#map {
position: relative;
width: 99%;
height: 200px;
margin: auto;
margin-top: -25%;
}

h1 {
color: #193369;
font-family: arial;
font-size: 30px;
text-align: center;
}

.hide {
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Intercompany Map</title>
<link rel="stylesheet" href="css/main.css" type="text/css" />
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://datamaps.github.io/scripts/datamaps.world.min.js"></script>
</head>

<body>
<h1>My D3 Node Map</h1>
<div id="map" style="height:800px"></div>
<script src="js/icm.js"></script>
</body>
</html>

关于javascript - 如何在 map 上显示/隐藏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com