e.printStackTrace();
context.getLayer().setStatus(ILayer.ERROR);
return;
}
final ViewportGraphics graphics = context.getGraphics();
/* Draw the legend. */
if (categories.size() == 0 || categories.size() != colorRules.size()) {
// draw a color ramp legend
int rulesNum = colorRules.size();
// get initially the text properties from the title
String titleString = legendStyle.titleString;
int textheight = (int) round(graphics.getStringBounds(titleString).getHeight());
if (titleString.length() == 0)
textheight = 0;
int w = round(legendStyle.legendWidth);
int h = round(legendStyle.legendHeight);
int x = round(legendStyle.xPos);
int y = round(legendStyle.yPos);
int bWidth = round(legendStyle.boxWidth);
/*
* calculate the boxheight from:
* legendHeight = 1/2 bHeight + textHeight + 1/2 bHeight + rulseNum*bHeight + 1/2 bHeight
*/
int bHeight = round((h - textheight) / (3f / 2f + rulesNum));
int yInset = bHeight / 2;
int xInset = 15;
int currentX = x + xInset;
int currentY = y + yInset + textheight;
if (textheight == 0) {
currentY = y;
h = h - yInset;
}
if (legendStyle.isRoundedRectangle) {
graphics.setColor(legendStyle.backgroundColor);
graphics.fillRoundRect(round(x), round(y), round(w), round(h), 15, 15);
graphics.setColor(legendStyle.foregroundColor);
graphics.setBackground(legendStyle.backgroundColor);
graphics.drawRoundRect(round(x), round(y), round(w), round(h), 15, 15);
} else {
graphics.setColor(legendStyle.backgroundColor);
graphics.fillRect(round(x), round(y), round(w), round(h));
graphics.setColor(legendStyle.foregroundColor);
graphics.setBackground(legendStyle.backgroundColor);
graphics.drawRect(round(x), round(y), round(w), round(h));
}
// draw the title
if (textheight != 0) {
graphics.setColor(legendStyle.fontColor);
graphics.drawString(titleString, currentX,// +horizontalMargin,
currentY,// +textVerticalOffset,
ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_LEFT);
}
currentY = currentY + yInset;
for( int i = 0; i < colorRules.size(); i++ ) {
String rule = colorRules.get(i);
double[] values = new double[2];
Color[] colors = new Color[2];
JGrassColorTable.parseColorRule(rule, values, colors);
String firstValue = String.valueOf(values[0]);
Color actualColor = colors[0];
String secondValue = String.valueOf(values[1]);
Color nextColor = colors[1];
graphics.fillGradientRectangle(currentX, currentY, bWidth, bHeight, actualColor, nextColor, true);
int tx = round(currentX + 1.5f * bWidth);
graphics.setColor(legendStyle.fontColor);
graphics.drawString(String.format("%-8.2f", Float.parseFloat(firstValue)), //$NON-NLS-1$
tx,// +horizontalMargin,
currentY, // - graphics.getFontAscent(),// +textVerticalOffset,
ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_MIDDLE);
currentY = currentY + bHeight;
if (i == colorRules.size() - 1) {
// add also last text
tx = round(currentX + 1.5f * bWidth);
graphics.setColor(legendStyle.fontColor);
graphics.drawString(String.format("%-8.2f", Float.parseFloat(secondValue)), //$NON-NLS-1$
tx,// +horizontalMargin,
currentY, // - graphics.getFontAscent(),// +textVerticalOffset,
ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_MIDDLE);
}
}
} else {
// draw a categories legend
int rulesNum = colorRules.size();
// get initially the text properties from the title
String titleString = legendStyle.titleString;
int textheight = (int) round(graphics.getStringBounds(titleString).getHeight());
if (titleString.length() == 0)
textheight = 0;
int w = round(legendStyle.legendWidth);
int h = round(legendStyle.legendHeight);
int x = round(legendStyle.xPos);
int y = round(legendStyle.yPos);
int bWidth = round(legendStyle.boxWidth);
/*
* calculate the boxheight from:
* legendHeight = 1/2 bHeight + textHeight + 1/2 bHeight + rulseNum*(bHeight + 1/3 bHeight) + 1/2 bHeight
*/
int bHeight = round((h - textheight) / (3f / 2f + 4f / 3f * rulesNum));
int yInset = bHeight / 2;
int xInset = 15;
int currentX = x + xInset;
int currentY = y + yInset + textheight;
if (textheight == 0) {
currentY = y;
h = (int) (h - yInset - 1f / 3f * bHeight);
}
if (legendStyle.isRoundedRectangle) {
graphics.setColor(legendStyle.backgroundColor);
graphics.fillRoundRect(round(x), round(y), round(w), round(h), 15, 15);
graphics.setColor(legendStyle.foregroundColor);
graphics.setBackground(legendStyle.backgroundColor);
graphics.drawRoundRect(round(x), round(y), round(w), round(h), 15, 15);
} else {
graphics.setColor(legendStyle.backgroundColor);
graphics.fillRect(round(x), round(y), round(w), round(h));
graphics.setColor(legendStyle.foregroundColor);
graphics.setBackground(legendStyle.backgroundColor);
graphics.drawRect(round(x), round(y), round(w), round(h));
}
// draw the title
if (textheight != 0) {
graphics.setColor(legendStyle.fontColor);
graphics.drawString(titleString, currentX,// +horizontalMargin,
currentY,// +textVerticalOffset,
ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_LEFT);
}
currentY = currentY + yInset;
for( int i = 0; i < colorRules.size(); i++ ) {
String rule = colorRules.get(i);
String cat = categories.get(i);
int lastColon = cat.lastIndexOf(':');
String attribute = cat.substring(lastColon + 1);
double[] values = new double[2];
Color[] colors = new Color[2];
JGrassColorTable.parseColorRule(rule, values, colors);
String firstValue = String.valueOf(values[0]);
graphics.setColor(Color.black);
graphics.drawRect(currentX, currentY, bWidth, bHeight);
graphics.setColor(colors[0]);
graphics.fillRect(currentX, currentY, bWidth, bHeight);
int tx = round(currentX + 1.5f * bWidth);
int tHeight = (int) round(graphics.getStringBounds(attribute).getHeight());
graphics.setColor(legendStyle.fontColor);
int ty = round(currentY - graphics.getFontAscent() + tHeight);// + tHeight -
// bHeight/3f);
graphics.drawString(attribute, tx,// +horizontalMargin,
ty, // - graphics.getFontAscent(),// +textVerticalOffset,
ViewportGraphics.ALIGN_LEFT, ViewportGraphics.ALIGN_MIDDLE);
currentY = round(currentY + 4f / 3f * bHeight);