I have a GeoJSON object with ~1000 polygons and a number of numeric attributes on each polygon. I want to be able to style the polygons based on these attributes using dynamically calculated class breaks. I already have a method for calculating the class breaks.
I can create style functions for the polygon layer if I know the class breaks ahead of time, eg:
var bg_style = function(feature, resolution){
var medinc = feature.get('medinc');
if (medinc<36000) {
fillColor='#ffffcc';
}
else if (medinc>=36000 && medinc<62000){
fillColor='#c2e699';
}
else if (medinc>=62000 && medinc<92000){
fillColor='#78c679';
}
else if (medinc>=92000 && medinc<140000){
fillColor='#31a354';
}
else if (medinc>=140000){
fillColor='#006837';
}
style = new ol.style.Style({
fill: new ol.style.Fill({
color: fillColor
}),
stroke: new ol.style.Stroke({
color: [0,0,0,1],
width: 0.5
})
});
return style;
};
but if I don't know the class breaks ahead of time (they depend on the data), I'm at a bit of a loss as to how to use the dynamically calculated breaks in the style function.
It seems like if I want to use variables for the class max/mins & color hex codes, they're out of scope since the style function is not taking any arguments beyond (feature, resolution). I could possibly attach them to the data as additional attributes, but this seems sleazy and creates a few thousand pieces of redundant information.
Basically what I want to do is replace the min/max and hex color for each range in the code snippet above with the class breaks generated at run time. Also no guarantee the number of classes is fixed. Any suggestions???