Skip to content
Snippets Groups Projects
Commit 8dcb1ddd authored by Florian Hölzl's avatar Florian Hölzl
Browse files

Vertical legend now working properly within its bounds.

refs 2589
parent c80af774
No related branches found
No related tags found
No related merge requests found
......@@ -73,12 +73,22 @@ public final class Sample {
iPhoneData.setPoint(screen, 3.8);
spiderChart.addData(iPhoneData);
DataSeries nexusData = new DataSeries("Nexus 6");
nexusData.setPoint(battery, 3.0);
nexusData.setPoint(camera, 4.5);
nexusData.setPoint(display, 3.0);
nexusData.setPoint(memory, 5.6);
nexusData.setPoint(brand, 4.0);
nexusData.setPoint(screen, 2.8);
spiderChart.addData(nexusData);
SpiderChartTitle title = new SpiderChartTitle("Smartphone Comparison Scale", spiderChart);
title.setSwtForegroundColor(SWT.COLOR_DARK_BLUE);
SpiderChartLegend legend = new SpiderChartLegend(spiderChart);
legend.addItem(iPhoneData, SWT.COLOR_GREEN);
legend.setOffset(320);
legend.setLabel("Legend");
legend.addItem(iPhoneData, SWT.COLOR_RED);
legend.addItem(nexusData, SWT.COLOR_BLUE);
legend.setVerticalLayout(true);
legend.setSwtBackgroundColor(SWT.COLOR_WHITE);
legend.setSwtForegroundColor(SWT.COLOR_BLUE);
......
......@@ -140,10 +140,20 @@ public abstract class SpiderChartComponentBase {
}
/** Creates a new font instance. ALWAYS dispose this afterwards. */
public Font createFont() {
protected final Font createFont() {
return createFont(fontData);
}
/** Creates a new font instance. ALWAYS dispose this afterwards. */
protected final Font createFont(FontData fontData) {
return new Font(Display.getCurrent(), fontData);
}
/** Returns the system color for the given SWT color. */
protected final Color getSystemColor(int swtColor) {
return Display.getCurrent().getSystemColor(swtColor);
}
/** Returns x. */
public final int getX() {
return x;
......
......@@ -17,17 +17,23 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.plotter;
import static java.util.Objects.requireNonNull;
import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_10PT;
import static org.fortiss.tooling.spiderchart.style.LineStyle.NORMAL_LINE;
import java.util.ArrayList;
import java.util.List;
import org.conqat.lib.commons.collections.Pair;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.fortiss.tooling.spiderchart.gc.swt.SpiderChartSwtGraphics;
import org.fortiss.tooling.spiderchart.label.SpiderChartLabel;
import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.FontStyle;
import org.fortiss.tooling.spiderchart.style.LineStyle;
/**
......@@ -40,23 +46,36 @@ import org.fortiss.tooling.spiderchart.style.LineStyle;
*/
public final class SpiderChartLegend extends SpiderChartComponentBase {
/** The size of the color icon of data series. */
private static final int ICON_SIZE = 10;
/** Constructor. */
public SpiderChartLegend(SpiderChart chart) {
this(chart, "", BLACK_VERDANA_10PT, 5, true);
}
/** Constructor. */
public SpiderChartLegend(SpiderChart chart, String label, FontStyle style, int margin,
boolean verticalLayout) {
super(chart);
this.labelText = requireNonNull(label);
this.labelStyle = requireNonNull(style);
this.margin = Math.max(5, margin);
this.verticalLayout = verticalLayout;
}
/** The items in the legend. */
private final List<Pair<DataSeries, LineStyle>> items = new ArrayList<>();
/** The legend label text. */
private String labelText = "";
private String labelText;
/** The legend label font style. */
private FontStyle labelStyle;
/** The inner margins. */
private int margin = 10;
private int margin = 5;
/** The offset used to position the legend on vertical. */
private int verticalOffset = 250;
/** Flag layouting the legend vertically. */
/** Flag for vertical layout. */
private boolean verticalLayout = true;
/** */
......@@ -67,31 +86,40 @@ public final class SpiderChartLegend extends SpiderChartComponentBase {
/** Draws the provided graphics */
public void draw(SpiderChartSwtGraphics g) {
if(labelText != null && labelText.length() > 0) {
SpiderChartLabel cl = new SpiderChartLabel(labelText, "", false, true);
cl.paint(g, x, y, width, height);
return;
}
GC gc = g.getGraphics();
// FIXME: debug rectangle
gc.drawRectangle(getX(), getY(), getWidth(), getHeight());
Font oldFont = gc.getFont();
Color oldForeground = gc.getForeground();
Font ontheflyFont = createFont();
gc.setFont(ontheflyFont);
gc.setForeground(getForegroundColor());
if(verticalLayout) {
drawVertical(g, ontheflyFont);
drawVertical(g);
} else {
drawHorizontal(g, ontheflyFont);
drawHorizontal(g);
}
gc.setForeground(oldForeground);
gc.setFont(oldFont);
ontheflyFont.dispose();
}
/** draws the graphics horizontally */
public void drawHorizontal(SpiderChartSwtGraphics g, Font ontheflyFont) {
public void drawHorizontal(SpiderChartSwtGraphics g) {
GC gc = g.getGraphics();
int iconWidth = 0;
int totalWidth = 0;
int iconHeight = 0;
int w = 0;
int h = 0;
int textHeight = getFontData().getHeight();
int textWidth = 0;
for(Pair<DataSeries, LineStyle> pair : items) {
Point extent = null; // FIXME
}
int textHeight = g.getFontHeight();
int textWidth = computeTextWidth(g);
int iconSeparator = 3;
int textSeparator = 5;
......@@ -128,8 +156,8 @@ public final class SpiderChartLegend extends SpiderChartComponentBase {
int offset = 0;
for(int i = 1; i <= items.size(); i++) {
Pair<DataSeries, LineStyle> pair = items.get(i - 1);
g.drawText(pair.getFirst().getName(), toCenterX + offset + iconWidth + iconSeparator +
x, toCenterY + y + itemHeight, getForegroundColor(), ontheflyFont);
gc.drawString(pair.getFirst().getName(), toCenterX + offset + iconWidth +
iconSeparator + x, toCenterY + y + itemHeight, true);
offset = offset + iconWidth + iconSeparator + textWidth + textSeparator;
}
offset = 0;
......@@ -157,67 +185,67 @@ public final class SpiderChartLegend extends SpiderChartComponentBase {
}
/** draws the graphics vertically */
public void drawVertical(SpiderChartSwtGraphics g, Font ontheflyFont) {
int iconWidth = 0;
int iconHeight = 0;
int h = 0;
int textHeight = g.getFontHeight();
int textWidth = computeTextWidth(g);
public void drawVertical(SpiderChartSwtGraphics g) {
GC gc = g.getGraphics();
for(Pair<DataSeries, LineStyle> pair : items) {
if(pair.getSecond() != null) {
iconWidth = Math.max(iconWidth, 10);
iconHeight = Math.max(iconHeight, 10);
}
int yDelta = 0;
if(labelText != null && labelText.trim().length() > 0) {
yDelta = drawVerticalLabel(gc);
}
int itemHeight = Math.max(textHeight, iconHeight);
int toCenterX = Math.max(0, (width - (iconWidth + textWidth)) / 2);
int toCenterY = Math.max(0, (height - items.size() * itemHeight) / 2);
int legendX1 = x + toCenterX;
int legendY1 = y + toCenterY;
int legendX2 = x + toCenterX + iconWidth + textWidth;
int legendY2 = y + toCenterY + items.size() * itemHeight;
int x1 = legendX1 - margin;
int y1 = legendY1 - margin;
int x2 = legendX2 + margin;
int y2 = legendY2 + margin;
int textWidth = 0;
int textHeight = g.getFontHeight();
if(getBackgroundFillStyle() != null) {
getBackgroundFillStyle().draw(g, x1, y1 + verticalOffset, x2, y2 + verticalOffset);
}
if(getLineStyle() != null) {
getLineStyle().drawRect(g, x1, y1 + verticalOffset, x2, y2 + verticalOffset);
}
for(int i = 1; i <= items.size(); i++) {
Pair<DataSeries, LineStyle> pair = items.get(i - 1);
g.drawText(pair.getFirst().getName(), toCenterX + iconWidth + x, toCenterY + y + i *
itemHeight + verticalOffset, getForegroundColor(), ontheflyFont);
}
for(int i = 1; i <= items.size(); i++) {
// FIXME: Object
Pair<DataSeries, LineStyle> pair = items.get(i - 1);
if(pair.getSecond() != null) {
LineStyle l = pair.getSecond();
l.setWidth(10);
l.draw(g, toCenterX + x, toCenterY + y + iconHeight / 2 + (i - 1) * itemHeight +
verticalOffset, toCenterX + x + iconWidth - 2, toCenterY + y + iconHeight /
2 + (i - 1) * itemHeight + verticalOffset);
}
for(Pair<DataSeries, LineStyle> pair : items) {
Point extent = gc.textExtent(pair.getFirst().getName());
textWidth = Math.max(textWidth, extent.x);
textHeight = Math.max(textHeight, extent.y);
}
}
/** */
private int computeTextWidth(SpiderChartSwtGraphics g) {
int textWidth = 0;
// FIXME: feature disabled
// if(getBackgroundFillStyle() != null) {
// getBackgroundFillStyle().draw(g, x1, y1, x2, y2);
// }
// if(getLineStyle() != null) {
// getLineStyle().drawRect(g, x1, y1, x2, y2);
// }
Color oldColor = gc.getForeground();
int cnt = 0;
for(Pair<DataSeries, LineStyle> pair : items) {
int w = g.getFontWidth(pair.getFirst().getName());
if(w > textWidth) {
textWidth = w;
}
int dY = yDelta + cnt * (margin + ICON_SIZE);
int dX = ICON_SIZE + 2 * margin;
LineStyle l = pair.getSecond();
l.setWidth(ICON_SIZE);
gc.setForeground(getSystemColor(l.getSwtColor()));
// FIXME: replace g with call to gc directly
int lY = dY + textHeight / 2;
l.draw(g, getX() + margin, getY() + lY, getX() + margin + ICON_SIZE, getY() + lY);
gc.drawText(pair.getFirst().getName(), getX() + dX, getY() + dY, true);
cnt++;
}
return textWidth;
gc.setForeground(oldColor);
}
/** Draws the header label for the vertical legend layout. */
private int drawVerticalLabel(GC gc) {
Font oldFont = gc.getFont();
Color oldColor = gc.getForeground();
Font font = createFont(labelStyle.getFontData());
gc.setFont(font);
gc.setForeground(getSystemColor(labelStyle.getSwtColor()));
gc.setClipping(getX(), getY(), getWidth(), getHeight());
Point textExtent = gc.textExtent(labelText);
int x = getX() + margin + (getWidth() - margin - textExtent.x) / 2;
gc.drawString(labelText, x, getY() + margin, true);
gc.setClipping((Rectangle)null);
gc.setForeground(oldColor);
gc.setFont(oldFont);
font.dispose();
return textExtent.y + margin;
}
/** */
......@@ -235,11 +263,6 @@ public final class SpiderChartLegend extends SpiderChartComponentBase {
this.margin = margin;
}
/** Sets the vertical offset. */
public void setOffset(int offset) {
this.verticalOffset = offset;
}
/** Sets the vertical layout flag. */
public void setVerticalLayout(boolean verticalLayout) {
this.verticalLayout = verticalLayout;
......
......@@ -23,6 +23,7 @@ import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.fortiss.tooling.spiderchart.gc.swt.SpiderChartSwtGraphics;
import org.fortiss.tooling.spiderchart.model.SpiderChart;
......@@ -60,10 +61,11 @@ public final class SpiderChartTitle extends SpiderChartComponentBase {
Color oldColor = gc.getForeground();
gc.setFont(ontheflyFont);
int toCenterX = (getWidth() + gc.textExtent(text).x) / 2;
int toCenterY = (getHeight() + getFontData().getHeight()) / 2;
Point extent = gc.textExtent(text);
int x = getX() + getWidth() / 2 - extent.x / 2;
int y = getY() + getHeight() / 2 - extent.y / 2;
gc.drawString(text, getX() + toCenterX, getY() + toCenterY + g.getFontHeight(), true);
gc.drawString(text, x, y, true);
gc.setForeground(oldColor);
gc.setFont(oldFont);
......
......@@ -18,8 +18,6 @@ $Id$
package org.fortiss.tooling.spiderchart.style;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.fortiss.tooling.spiderchart.gc.swt.SpiderChartSwtGraphics;
/**
......@@ -78,7 +76,7 @@ public final class LineStyle {
/** Draws the line */
public void draw(SpiderChartSwtGraphics g, final int x1, final int y1, final int x2,
final int y2) {
this.setGraphicsProperties(g);
setGraphicsProperties(g);
g.drawLineWithStyle(x1, y1, x2, y2);
}
......@@ -150,8 +148,8 @@ public final class LineStyle {
}
/** Getter for line color instance */
public Color getColor() {
return Display.getCurrent().getSystemColor(swtColor);
public int getSwtColor() {
return swtColor;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment