html - 如何在传单中制作水平图例?
问题描述
我想做一个像这样的水平图例:https ://ovrdc.github.io/gis-tutorials/leaflet/06-choropleth/#7/41.418/-82.013
我不想要所有的东西,只想要传说和颜色。我不知道如何将页面上的代码集成到我的实际代码中:
function getColor(d) {
return d > 0.7 ? '#b2182b' :
d > 0.5 ? '#d6604d' :
d > 0.3 ? '#f4a582' :
d > 0.1 ? '#fddbc7' :
d > -0.1 ? '#f7f7f7' :
d > -0.3 ? '#d1e5f0' :
d > -0.5 ? '#92c5de' :
d > -0.7 ? '#4393c3' :
'#2166ac';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.65,
fillColor: getColor(feature.properties.differentiel)
};
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.65
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
var geojson;
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
function zoomToFeature(e) {
map.fitBounds(e.target.getBounds());
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});
}
geojson = L.geoJson(statesData, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
map.attributionControl.addAttribution('later');
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [-0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7],
labels = ['test'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + getColor(from + 1) + '"></i> ' +
from + (to ? ' to ' + to : '+'));
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
我尝试替换大部分代码,但没有成功。我怎样才能用我的数据得到那个漂亮而干净的图例?谢谢
解决方案
推荐阅读
- html - 基于下拉框渲染动态表单
- android - 更新到 Android Studio 3.6 后,应用程序在运行时崩溃
- virtual-machine - 如何将“iso存档”转换为“iso”?什么是“iso存档”?
- pandas - 查找事件的发生
- python - Qt for Python:Slot() 装饰器有什么用处?
- typescript - 对可观察的对象数组进行排序
- python - Jupyter Notebook / Anaconda '内核错误'(白痴证明修复)
- java - 将 jdk 从 8u221 升级到 8u241 得到 java.net.SocketException: Broken pipe (Write failed) 异常
- javascript - 有没有办法改变 React 中输入的类型?
- r - 未选择的条目显示在轴上 - Crosstalk+Plotly 条形图