For my first choropleth maps in Dash I created a separate layer for each area, but ran into severe performance issues as the number of layers increased. Roughly 10 were OK, more than 50 would freeze my browser for about 20 seconds.
Creating a single layer for each color in the map works well. Requires some preprocessing of data, and as empet mentioned you have to create each source as a geojson type dict. I’ll post a full example next week when I have time, but here is basically what I did. I also start my layers with an outline so you can see boundaries, totally optional:
outline=dict(
sourcetype = 'geojson',
source=geojson_converted_to_a_dict,
below="water",
type = 'line',
color = 'black',
width=5
)
layers = [outline]
# Dictionary with each color as an empty list
layers_by_color = {c: [] for c in colors}
for obj in my_objects:
color = figure_out_color_for_this_object()
layers_by_color[color].append(obj.geojson_as_object)
# obj.geojson_as_object has keys for type and coordiantes. Type is 'Pologon' or 'Multipolygon'
for color in colors:
geojson_dict = dict(type="FeatureCollection",features=layers_by_color[color])
layers.append(dict(
sourcetype = 'geojson',
source=geojson_dict,
type='fill',
opacity=1,
color=color
))