我試圖在圖例javascript中替換很多字符串以使第一個顏色字段與數據的no值相同。我假設我需要爲第一條規則添加一個例外,但我無法弄清楚。我從小冊子開始爲choropleth地圖,並已得到這一點。這是我需要使地圖充分發揮功能的最後一件事。基本上希望沒有數據值,未着色或突出顯示的國家和圖例中的第一個字段顯示爲灰色,而不是顯示的綠色第二個值。傳單需要爲無數據提取值
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 0.00001, 0.7300001, 2.9900001, 6.700001],
labels = [],
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 : '–43.89303638'));
} //changed '1' to get right colors on the legend but first field is still second green, not sure why??
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
這是按照現在設置的功能正確填充國家顏色的一塊。
function getColor(d) {
return d > 6.700001 ? '#D7191C' :
d > 2.9900001 ? '#FDAE61' :
d > 0.7300001 ? '#A6D96A' :
d > 0.00001 ? '#1A9641' :
'#E9E9E9';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: getColor(feature.properties.UNdata_CO2_20151106_191526521_Value)//changed value from value above in blue
};
}
這裏是鏈接到my webpage!
我說關於我的最後變化的音符,它顯示了兩個灰色的領域,然後深綠色,淺綠色,橙色,而不顯示紅色爲最高值......
這工作,很容易解決,我在這個例子學到了很多東西。我知道應該有一種方法來添加一個表達式來創建一個沒有數據字段,但我的努力沒有任何工作。謝謝。 – Staley