HI!
Recently I have switched from 1.x to 2.x and I have some problems implementing some views styles I had working with 1.x
here's an example:
I need to fill several polygons with different colors according to a views field value (an integer). the polygons are drawn from a geo table, i am not using nodes.
in 1.x i set my views' Feature Styles field like this:
(bear with me for coding ingenuities or mistakes, i'm not a coder at all)

$var = $record->feeds_data_some_field;
switch($var) {
    case ($var <= 10):
	   $fill_color = '#FFFF00';
	   break;
	case (10 > $var || $var <= 30):
	   $fill_color = '#CCFE00';
	   break; 
	case (30 > $var || $var <= 50):
	   $fill_color = '#3FFE00';
	   break;  	   
	case (50 > $var || $var <= 70):
	   $fill_color = '#00DD66';
	   break; 	   
	case (70 > $var || $var <= 100):
	   $fill_color = '#0484A7';
	   break; 	
	case ($var > 100):
	   $fill_color = '#0C0183';
	   break; 	 
}
$style = array();
$style['fillColor'] =  $fill_color;
$style['fillOpacity'] =  .75;
$style['strokeColor'] = '#000000';
$style['strokeWidth'] = .5;
return $style;	   

With 2.x I can't find a similar way to use PHP logic to determine the FillColor property according to a range of values.
Is something like this possible with Openlayers 2.x?

Thanks a lot

Comments

bohz’s picture

For the time being I have worked around this issue using Views Custom Field phpcode field.
...still interested knowing a proper solution, though

Thanks

tmcw’s picture

In openlayers 2.x, you can either write a style plugin (there's an example in the docs), or you can create a behavior that applies openlayers style rules to a layer, like is done in the scalepoints behavior. The previous technique was abandoned because it cannot be made performant.

bohz’s picture

Thanks for the info tmcw.
The 1.x Feature Styles approach is indeed quite heavy, as well as my workaround.
I'll try to manage it with one of the methods you suggested.

Best

florisla’s picture

@tmcw, are your referring to this documentation page about writing a style plugin?
http://drupal.org/node/620602

I am trying to expand the documentation on 'Variable Styling' (http://drupal.org/node/754480) and could use some more pointers.

Specifically, I currently don't know how one can lookup attribute values from within hook_openlayers_styles() to influence styling dynamically.

tmcw’s picture

Specifically, I currently don't know how one can lookup attribute values from within hook_openlayers_styles() to influence styling dynamically.

You can't: OpenLayers 2.x doesn't support that type of styling. Style plugins, though, can, and there's documentation and examples within the module.

ptitb’s picture

I'm looking for the same functionality. I've made a custom module and implemented the "ctools_plugin_api" and the "openlayers_styles" functions. I now want to switch styles based upon fields associated with the feature.

tmcw wrote that you can't get attribute values in the openlayers_styles function and that it is possible to write a style plugin. But isn't this what I am doing? Or do I need to write a views style plugin.
I can't find any examples in the module or documentation.

Any help is appreciated! Thanks in advance!

zzolo’s picture

Styles are different than Style Plugins. Styles are just sets of configurations, while plugins are functional objects that actually process the styles on the fly. Make sure you are using the dev version. There should be an example in the openlayers_test module.

naught101’s picture

I'm also looking for the functionality described in the original post.

#7 zzolo: I don't quite follow. Are the "styles" discussed at http://drupal.org/node/620602 the same as what you call "styles"? If so, what are "style plugins", and where can I find documentation about them? #5 tmcw says there are docs and examples in the module, but I can't see anything except views style plugins, or is this what you're talking about? If so, any hints as to where to start (I guess just make a views display plugin that extends openlayers_views_plugin_display_openlayers?)

This describes a more direct method, which requires writing JS, but I don't know if this method is even compatible with Drupal OpenLayers:
http://osgeo-org.1803224.n2.nabble.com/Random-Colors-on-Vector-Features-...

zzolo’s picture

Component: OpenLayers API » OL API

Please see STYLE_PLUGINS.txt in the docs folder in the module. Also there are examples in the openlayers_test module. (still needs to be ported to D7)

ken-g’s picture

Issue summary: View changes
Status: Active » Closed (outdated)