如何在地图上显示/隐藏?
Posted
技术标签:
【中文标题】如何在地图上显示/隐藏?【英文标题】:How to show/hide on geo map? 【发布时间】:2015-10-22 22:26:25 【问题描述】:到目前为止,我终于创建了一个地理地图,其中包含用线连接它们的节点。所有节点都有持有人 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
【问题讨论】:
您要选择连接线还是只选择连接的节点,还是两者都选择? 两者。所以当你选择一个节点时......它也会显示它相关的线和节点,并隐藏它不相关的节点和线 【参考方案1】:您可以选择nodes
和links
宽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;
)
);
然后双击event
只显示链接的节点和链接:
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/ 和 sn-p:
//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>
【讨论】:
谢谢!!是否有一个功能,当您隐藏节点/线时...再次双击地图然后再次显示所有节点/线? 如果你在隐藏后点击地图上的任何地方,它们会重新出现? @TheGarrett 是的,很简单,只需调用 2 行代码:blinks.classed('hide', false);
和 bnodes.classed('hide', false);
。我更新了 jsFiddle 并添加了函数 showAllNodesAndLinks
jsfiddle.net/x9058g8s/7。当您第二次双击时,所有链接和节点再次可见。
@TheGarrett 当您单击地图上的国家/地区时:jsfiddle.net/x9058g8s/6
谢谢,你帮了大忙!以上是关于如何在地图上显示/隐藏?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用markerwithlabel隐藏或禁用谷歌地图上的默认标记