Commit 1b938609 authored by Nico Mack's avatar Nico Mack

Minor fixes and improvements on ColourScale rendering

parent f25320e3
......@@ -60,7 +60,6 @@ public class ColourScale<T extends Comparable<T>> {
private static final String AUTO = "auto"; //$NON-NLS-1$
private static final String DEFAULT_RANGE_SEPARATOR = "-"; //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(ColourScale.class.getSimpleName());
// ---------------------------------------------------------------------------
......@@ -237,6 +236,21 @@ public class ColourScale<T extends Comparable<T>> {
return cloned;
}
// ---------------------------------------------------------------------------
/**
*
* @return
*/
// ---------------------------------------------------------------------------
public String getTitle() {
StringBuilder titleBuilder = new StringBuilder(this.name);
if (Strings.isNotBlank(this.unit)) {
titleBuilder.append(" [").append(this.unit).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
}
return titleBuilder.toString();
}
// ---------------------------------------------------------------------------
/**
*
......
......@@ -47,6 +47,7 @@ public class ColourScaleRenderer {
private Font mappingFont;
private int horizontalGap;
private int verticalGap;
private boolean renderTitle;
// ***************************************************************************
// * Constants *
......@@ -72,6 +73,7 @@ public class ColourScaleRenderer {
this.mappingShape = DEFAULT_SHAPE;
this.mappingFont = DEFAULT_FONT;
this.fontColour = Color.BLACK;
this.renderTitle = true;
}
// ---------------------------------------------------------------------------
......@@ -80,31 +82,46 @@ public class ColourScaleRenderer {
// ***************************************************************************
// ---------------------------------------------------------------------------
private Dimension getMaxDimension(Dimension maximum, int width, int height) {
Dimension newMaximum = null;
if (maximum == null) {
newMaximum = new Dimension(width, height);
} else {
newMaximum = maximum;
newMaximum.setSize(Math.max(maximum.width, width),
Math.max(maximum.height, height));
}
return newMaximum;
}
// ---------------------------------------------------------------------------
private <T extends Comparable<T>> Dimension determineRequiredDimensions(ColourScale<T> scale) {
List<ColourMapping<T>> mappings = scale.getColourMappings();
int widest = 0;
int highest = 0;
Dimension maximum;
Rectangle2D shapeBounds = mappingShape.getBounds2D();
maximum = this.getMaxDimension(null, (int) shapeBounds.getWidth(), (int) shapeBounds.getHeight());
FontRenderContext frc = new FontRenderContext(null, false, false);
Rectangle2D shapeBounds = mappingShape.getBounds2D();
highest = (int) shapeBounds.getHeight();
if (this.renderTitle) {
GlyphVector glyphVector = mappingFont.createGlyphVector(frc, scale.getTitle());
Rectangle2D bounds = glyphVector.getLogicalBounds();
maximum = this.getMaxDimension(maximum, (int) bounds.getWidth(), (int) bounds.getHeight());
}
for (ColourMapping<T> mapping : mappings) {
String label = scale.getLabelFor(mapping);
GlyphVector glyphVector = mappingFont.createGlyphVector(frc, label);
Rectangle2D bounds = glyphVector.getLogicalBounds();
int width = (int) bounds.getWidth();
int height = (int) bounds.getHeight();
highest = Math.max(height, highest);
widest = Math.max(width, widest);
maximum = this.getMaxDimension(maximum, (int) bounds.getWidth(), (int) bounds.getHeight());
}
int height = (highest + verticalGap) * mappings.size();
int width = (int) shapeBounds.getWidth() + horizontalGap + widest;
int height = (int) (maximum.getHeight() + verticalGap) * mappings.size();
int width = (int) (shapeBounds.getWidth() + horizontalGap + maximum.getWidth());
return new Dimension(width, height);
}
......@@ -123,12 +140,18 @@ public class ColourScaleRenderer {
Rectangle2D shapeBounds = mappingShape.getBounds2D();
List<ColourMapping<T>> mappings = scale.getColourMappings();
double offset = (double) required.height / mappings.size();
double offset = (double) required.height / ((this.renderTitle) ? mappings.size() + 1 : mappings.size());
double fontBaseLineOffset = metrics.getAscent() / 2;
offscreen.translate(-shapeBounds.getX(), (offset / 2));
offscreen.setFont(mappingFont);
if (this.renderTitle) {
offscreen.setPaint(fontColour);
offscreen.drawString(scale.getTitle(), (int) (shapeBounds.getX() + shapeBounds.getWidth() + horizontalGap), (int) fontBaseLineOffset);
offscreen.translate(0, offset);
}
for (ColourMapping<T> mapping : mappings) {
offscreen.setPaint(mapping.getColour());
offscreen.fill(mappingShape);
......@@ -176,6 +199,12 @@ public class ColourScaleRenderer {
// ---------------------------------------------------------------------------
public void setRenderTitle(boolean renderIt) {
this.renderTitle = renderIt;
}
// ---------------------------------------------------------------------------
public BufferedImage render() {
return this.renderScale(this.scale);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment