我正在使用leaflet并且我正在生成多边形,然后我正在使用chroma.js这样:
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: chroma.scale(['#fafa6e','#2A4858']).mode('lch').colors(6)
};
}
var geojson = L.geoJson(statesData, {
style: style,
}).addTo(map);
为什么我只为所有多边形而不是一系列颜色获得一种颜色,黑色?
答案 0 :(得分:1)
您正在将fillColor
设置为颜色列表。您需要根据feature
的属性选择特定的颜色。
这将有效(基于official Leaflet demos之一)。
var states = [{
"type": "Feature",
"properties": {"color_id": 1},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"color_id": 2},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
colors = chroma.scale(['#fafa6e','#2A4858']).mode('lch').colors(6)
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: colors[feature.properties.color_id]
};
}
layer = L.geoJSON(states, {style: style}).addTo(map);