Skip to content

Commit

Permalink
ttools: fix legend to identify related layers
Browse files Browse the repository at this point in the history
When generating entries for the plot legend, layers representing the
same positional datasets are now grouped together into a single
legend entry.
  • Loading branch information
mbtaylor authored and mmpcn committed Nov 27, 2014
1 parent bde9c85 commit 9ac98e2
Showing 1 changed file with 41 additions and 1 deletion.
Expand Up @@ -61,6 +61,7 @@
import uk.ac.starlink.ttools.plot2.Plotter;
import uk.ac.starlink.ttools.plot2.ShadeAxis;
import uk.ac.starlink.ttools.plot2.ShadeAxisFactory;
import uk.ac.starlink.ttools.plot2.SubCloud;
import uk.ac.starlink.ttools.plot2.Surface;
import uk.ac.starlink.ttools.plot2.SurfaceFactory;
import uk.ac.starlink.ttools.plot2.config.ConfigKey;
Expand Down Expand Up @@ -1245,6 +1246,8 @@ private Icon createLegend( Environment env, Map<String,PlotLayer> layerMap,

/* Make a map from layer labels to arrays of styles.
* Each entry in this map will correspond to a legend entry. */
Map<List<SubCloud>,String> cloudMap =
new LinkedHashMap<List<SubCloud>,String>();
Map<String,List<Style>> labelMap =
new LinkedHashMap<String,List<Style>>();
for ( String suffix : suffixSeq ) {
Expand All @@ -1259,14 +1262,29 @@ private Icon createLegend( Environment env, Map<String,PlotLayer> layerMap,
.toString();
throw new ParameterValueException( legseqParam_, msg );
}

/* If a legend label has been explicitly given for this layer,
* use that. */
String label = new ParameterFinder<Parameter<String>>() {
public Parameter<String> createParameter( String sfix ) {
return createLabelParameter( sfix );
}
}.getParameter( env, suffix )
.objectValue( env );

/* Otherwise, work one out. We don't give every layer its own
* entry, but instead group layers together by their data
* coordinates, since you may well have multiple layers with the
* same data overplotted to achieve some visual effect.
* The SubCloud is an object which can tell (testing by equality)
* whether layers have the same positional coordinates. */
if ( label == null ) {
label = suffix.length() == 0 ? "data" : suffix;
List<SubCloud> dataClouds = getPointClouds( layer );
if ( ! cloudMap.containsKey( dataClouds ) ) {
String suffixLabel = suffix.length() == 0 ? "data" : suffix;
cloudMap.put( dataClouds, suffixLabel );
}
label = cloudMap.get( dataClouds );
}
if ( ! labelMap.containsKey( label ) ) {
labelMap.put( label, new ArrayList<Style>() );
Expand Down Expand Up @@ -1301,6 +1319,28 @@ public Parameter<String> createParameter( String sfix ) {
}
}

/**
* Returns a list of point clouds representing the positional coordinates
* used to plot a layer. The result is an object that can be compared
* for equality to test whether layers are representing the same
* positional data set.
*
* @return layer plot layer
* @return list of positional dataset identifiers
*/
private static List<SubCloud> getPointClouds( PlotLayer layer ) {
DataSpec dataSpec = layer.getDataSpec();
DataGeom geom = layer.getDataGeom();
CoordGroup cgrp = layer.getPlotter().getCoordGroup();
int npos = cgrp.getPositionCount();
List<SubCloud> cloudList = new ArrayList<SubCloud>( npos );
for ( int ipos = 0; ipos < npos; ipos++ ) {
int iposCoord = cgrp.getPosCoordIndex( ipos, geom );
cloudList.add( new SubCloud( geom, dataSpec, iposCoord ) );
}
return cloudList;
}

/**
* Returns a map of suffix strings to LayerType objects.
* Each suffix string is appended to parameters associated with the
Expand Down

0 comments on commit 9ac98e2

Please sign in to comment.