Deck.gl 具有标准的 HexagonLayer 工具提示 Javascript
Deck.gl HexagonLayer tooltip with standard Javascript
我有以下代码将纬度和经度分组到 hexbin 中,并使用 mapbox 底图将它们绘制在地图上,上面有一个 deck.gl 层:
<!doctype html>
<html lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<!-- deck.gl standalone bundle -->
<script src="https://cdn.jsdelivr.net/npm/deck.gl@8.1.5/dist.min.js"></script>
<!-- d3 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js"></script>
<!-- Mapbox dependencies -->
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.0/mapbox-gl.js"></script>
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.0/mapbox-gl.css" rel="stylesheet" />
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
}
#control-panel {
font-family: Helvetica, Arial, sans-serif;
position: absolute;
background: #fff;
top: 0;
left: 0;
margin: 12px;
padding: 20px;
z-index: 1;
}
label {
display: inline-block;
width: 140px;
}
</style>
</head>
<body>
<div id="control-panel">
<div>
<label>Radius</label>
<input id="radius" type="range" min="1000" max="20000" step="1000" value="1000"></input>
<span id="radius-value"></span>
</div>
<div>
<label>Coverage</label>
<input id="coverage" type="range" min="0" max="1" step="0.1" value="1"></input>
<span id="coverage-value"></span>
</div>
<div>
<label>Upper Percentile</label>
<input id="upperPercentile" type="range" min="90" max="100" step="1" value="100"></input>
<span id="upperPercentile-value"></span>
</div>
</div>
<script>
const deckgl = new deck.DeckGL({
mapboxApiAccessToken: '<mapbox_pk_token>',
mapStyle: 'mapbox://styles/mapbox/dark-v9',
initialViewState: {
longitude: -122.413756,
latitude: 37.779528,
zoom: 6,
minZoom: 0,
maxZoom: 21,
pitch: 40.5
},
controller: true
});
//const data = d3.csv('https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv');
const data = [
{name: 'Colma1', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Colma2', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Colma3', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Civic Center1', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center2', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center3', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center4', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center5', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
];
const OPTIONS = ['radius', 'coverage', 'upperPercentile'];
const COLOR_RANGE = [
[1, 152, 189],
[73, 227, 206],
[216, 254, 181],
[254, 237, 177],
[254, 173, 84],
[209, 55, 78]
];
OPTIONS.forEach(key => {
document.getElementById(key).oninput = renderLayer;
});
renderLayer();
function renderLayer () {
const options = {};
OPTIONS.forEach(key => {
const value = +document.getElementById(key).value;
document.getElementById(key + '-value').innerHTML = value;
options[key] = value;
});
const hexagonLayer = new deck.HexagonLayer({
id: 'heatmap',
colorRange: COLOR_RANGE,
data,
elevationRange: [0, 1000],
elevationScale: 250,
extruded: true,
getPosition: d => [Number(d.lng), Number(d.lat)],
opacity: 1,
pickable: true,
onHover: ({object, x, y}) => {
const tooltip = `Count: 12`;
},
...options
});
deckgl.setProps({
layers: [hexagonLayer],
tooltip: {
'html': '<b>Elevation Value:</b>',
'style': {
'color': 'white'
}
}
});
}
</script>
</body>
</html>
我想为每个 hexbin(地图顶部的条形图)添加一个工具提示,但我不确定如何完成此操作。我能找到的关于工具提示的唯一例子是 this,所以它看起来应该是可能的。我尝试使用这部分代码将其添加到图层上:
tooltip: {
'html': '<b>Elevation Value:</b>',
'style': {
'color': 'white'
}
}
但是没有用。有谁知道我该怎么做?我在 data
字段中有一些额外的数据,即 name
和 passengers
,我更愿意说创建第一个 hexbin 工具提示作为 data.name
值的逗号分隔列表。例如,该示例似乎是指使用 {colorValue}
的变量。或者,每个 hexbin 的工具提示也可能是可以接受的。我只是 运行 在本地计算机上作为静态页面,所以不确定这是否会有所不同。
编辑:在 this 的基础上添加了以下代码,但这也不起作用:
pickable: true,
onHover: ({object, x, y}) => {
const tooltip = `Count: 12`;
},
它也提到了 ${object.centroid.join(', ')}\nCount: ${object.points.length}
但它给出了一个错误。
最简单的方法是 getTooltip property. If you want to use the onHover
property, which gives you a bit more control, you have to create and manage the tooltip DOM element 你自己。
我有以下代码将纬度和经度分组到 hexbin 中,并使用 mapbox 底图将它们绘制在地图上,上面有一个 deck.gl 层:
<!doctype html>
<html lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<!-- deck.gl standalone bundle -->
<script src="https://cdn.jsdelivr.net/npm/deck.gl@8.1.5/dist.min.js"></script>
<!-- d3 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js"></script>
<!-- Mapbox dependencies -->
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.0/mapbox-gl.js"></script>
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.0/mapbox-gl.css" rel="stylesheet" />
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
}
#control-panel {
font-family: Helvetica, Arial, sans-serif;
position: absolute;
background: #fff;
top: 0;
left: 0;
margin: 12px;
padding: 20px;
z-index: 1;
}
label {
display: inline-block;
width: 140px;
}
</style>
</head>
<body>
<div id="control-panel">
<div>
<label>Radius</label>
<input id="radius" type="range" min="1000" max="20000" step="1000" value="1000"></input>
<span id="radius-value"></span>
</div>
<div>
<label>Coverage</label>
<input id="coverage" type="range" min="0" max="1" step="0.1" value="1"></input>
<span id="coverage-value"></span>
</div>
<div>
<label>Upper Percentile</label>
<input id="upperPercentile" type="range" min="90" max="100" step="1" value="100"></input>
<span id="upperPercentile-value"></span>
</div>
</div>
<script>
const deckgl = new deck.DeckGL({
mapboxApiAccessToken: '<mapbox_pk_token>',
mapStyle: 'mapbox://styles/mapbox/dark-v9',
initialViewState: {
longitude: -122.413756,
latitude: 37.779528,
zoom: 6,
minZoom: 0,
maxZoom: 21,
pitch: 40.5
},
controller: true
});
//const data = d3.csv('https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/3d-heatmap/heatmap-data.csv');
const data = [
{name: 'Colma1', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Colma2', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Colma3', passengers: 4214, lng: '-122.466233', lat: '37.684638'},
{name: 'Civic Center1', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center2', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center3', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center4', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
{name: 'Civic Center5', passengers: 24798, lng: '-122.413756', lat: '37.779528'},
];
const OPTIONS = ['radius', 'coverage', 'upperPercentile'];
const COLOR_RANGE = [
[1, 152, 189],
[73, 227, 206],
[216, 254, 181],
[254, 237, 177],
[254, 173, 84],
[209, 55, 78]
];
OPTIONS.forEach(key => {
document.getElementById(key).oninput = renderLayer;
});
renderLayer();
function renderLayer () {
const options = {};
OPTIONS.forEach(key => {
const value = +document.getElementById(key).value;
document.getElementById(key + '-value').innerHTML = value;
options[key] = value;
});
const hexagonLayer = new deck.HexagonLayer({
id: 'heatmap',
colorRange: COLOR_RANGE,
data,
elevationRange: [0, 1000],
elevationScale: 250,
extruded: true,
getPosition: d => [Number(d.lng), Number(d.lat)],
opacity: 1,
pickable: true,
onHover: ({object, x, y}) => {
const tooltip = `Count: 12`;
},
...options
});
deckgl.setProps({
layers: [hexagonLayer],
tooltip: {
'html': '<b>Elevation Value:</b>',
'style': {
'color': 'white'
}
}
});
}
</script>
</body>
</html>
我想为每个 hexbin(地图顶部的条形图)添加一个工具提示,但我不确定如何完成此操作。我能找到的关于工具提示的唯一例子是 this,所以它看起来应该是可能的。我尝试使用这部分代码将其添加到图层上:
tooltip: {
'html': '<b>Elevation Value:</b>',
'style': {
'color': 'white'
}
}
但是没有用。有谁知道我该怎么做?我在 data
字段中有一些额外的数据,即 name
和 passengers
,我更愿意说创建第一个 hexbin 工具提示作为 data.name
值的逗号分隔列表。例如,该示例似乎是指使用 {colorValue}
的变量。或者,每个 hexbin 的工具提示也可能是可以接受的。我只是 运行 在本地计算机上作为静态页面,所以不确定这是否会有所不同。
编辑:在 this 的基础上添加了以下代码,但这也不起作用:
pickable: true,
onHover: ({object, x, y}) => {
const tooltip = `Count: 12`;
},
它也提到了 ${object.centroid.join(', ')}\nCount: ${object.points.length}
但它给出了一个错误。
最简单的方法是 getTooltip property. If you want to use the onHover
property, which gives you a bit more control, you have to create and manage the tooltip DOM element 你自己。