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

Spider chart values printed using decimal formatter.

refs 2589
parent 77ceb135
Branches
Tags
No related merge requests found
...@@ -28,6 +28,8 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.NAVY; ...@@ -28,6 +28,8 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.NAVY;
import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.OLIVE; import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.OLIVE;
import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.getDarkerColor; import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.getDarkerColor;
import java.text.DecimalFormat;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
...@@ -68,8 +70,8 @@ public final class Sample { ...@@ -68,8 +70,8 @@ public final class Sample {
spiderChart.setTitle("Smartphone Comparison Scale"); spiderChart.setTitle("Smartphone Comparison Scale");
spiderChart.setLegendLabel("Legend"); spiderChart.setLegendLabel("Legend");
Axis battery = new Axis("Battery", 0.0, 5.0); Axis battery = new Axis("Battery", 0.0, 4.0);
Axis camera = new Axis("Camera", 0.0, 5.0); Axis camera = new Axis("Camera", 0.0, 7.0);
Axis display = new Axis("Display", 0.0, 5.0); Axis display = new Axis("Display", 0.0, 5.0);
Axis memory = new Axis("Memory", 0.0, 5.0); Axis memory = new Axis("Memory", 0.0, 5.0);
Axis brand = new Axis("Brand", 0.0, 5.0); Axis brand = new Axis("Brand", 0.0, 5.0);
...@@ -104,24 +106,36 @@ public final class Sample { ...@@ -104,24 +106,36 @@ public final class Sample {
LegendStyle legendStyle = new LegendStyle(BLUE, true, 5, BLACK_VERDANA_12PT); LegendStyle legendStyle = new LegendStyle(BLUE, true, 5, BLACK_VERDANA_12PT);
chartStyle.setLegendStyle(legendStyle); chartStyle.setLegendStyle(legendStyle);
AxisStyle aStyle = new AxisStyle(SOLID_BLACK_1PT, BLACK_VERDANA_14PT, 4, BLACK_VERDANA_8PT); AxisStyle aStyle3Segs =
chartStyle.setAxisStyle(battery, aStyle); new AxisStyle(SOLID_BLACK_1PT, BLACK_VERDANA_14PT, 3, BLACK_VERDANA_8PT,
chartStyle.setAxisStyle(camera, aStyle); new DecimalFormat("#.##"));
chartStyle.setAxisStyle(display, aStyle); chartStyle.setAxisStyle(battery, aStyle3Segs);
chartStyle.setAxisStyle(memory, aStyle); AxisStyle aStyle6Segs =
chartStyle.setAxisStyle(brand, aStyle); new AxisStyle(SOLID_BLACK_1PT, BLACK_VERDANA_14PT, 6, BLACK_VERDANA_8PT,
chartStyle.setAxisStyle(screen, aStyle); new DecimalFormat("#.##"));
chartStyle.setAxisStyle(camera, aStyle6Segs);
AxisStyle aStyle4Segs =
new AxisStyle(SOLID_BLACK_1PT, BLACK_VERDANA_14PT, 4, BLACK_VERDANA_8PT,
new DecimalFormat("#.##"));
chartStyle.setAxisStyle(display, aStyle4Segs);
chartStyle.setAxisStyle(memory, aStyle4Segs);
chartStyle.setAxisStyle(brand, aStyle4Segs);
chartStyle.setAxisStyle(screen, aStyle4Segs);
chartStyle.setUseIndividualAxisSegments(true);
chartStyle.setAxisSegments(4);
LineStyle olive1pt = new LineStyle(OLIVE); LineStyle olive1pt = new LineStyle(OLIVE);
FillStyle oliveFill = new FillStyle(OLIVE, 128); FillStyle oliveFill = new FillStyle(OLIVE, 128);
DataSeriesStyle iphoneStyle = DataSeriesStyle iphoneStyle =
new DataSeriesStyle(olive1pt, oliveFill, true, true, BLACK_VERDANA_10PT, 7); new DataSeriesStyle(olive1pt, oliveFill, true, true, BLACK_VERDANA_10PT, 7,
new DecimalFormat("#.#"));
chartStyle.setDataSeriesStyle(iPhoneData, iphoneStyle); chartStyle.setDataSeriesStyle(iPhoneData, iphoneStyle);
LineStyle navy1pt = new LineStyle(NAVY); LineStyle navy1pt = new LineStyle(NAVY);
FillStyle navyFill = new FillStyle(NAVY, 128); FillStyle navyFill = new FillStyle(NAVY, 128);
DataSeriesStyle nexusStyle = DataSeriesStyle nexusStyle =
new DataSeriesStyle(navy1pt, navyFill, true, true, BLACK_VERDANA_10PT, 7); new DataSeriesStyle(navy1pt, navyFill, true, true, BLACK_VERDANA_10PT, 7,
new DecimalFormat("#.#"));
chartStyle.setDataSeriesStyle(nexusData, nexusStyle); chartStyle.setDataSeriesStyle(nexusData, nexusStyle);
viewer = new SpiderChartViewer(shell, spiderChart, chartStyle); viewer = new SpiderChartViewer(shell, spiderChart, chartStyle);
......
...@@ -22,6 +22,8 @@ import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_12PT ...@@ -22,6 +22,8 @@ import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_12PT
import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_8PT; import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_8PT;
import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT; import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT;
import java.text.DecimalFormat;
import org.fortiss.tooling.spiderchart.model.Axis; import org.fortiss.tooling.spiderchart.model.Axis;
/** /**
...@@ -48,19 +50,28 @@ public final class AxisStyle { ...@@ -48,19 +50,28 @@ public final class AxisStyle {
private int segments; private int segments;
/** The font style of the segment labels. */ /** The font style of the segment labels. */
private FontStyle segmentStyle; private FontStyle segmentStyle;
/** The decimal formatter of the segment labels of the axis. */
private DecimalFormat decimalFormat;
/** Constructor. */ /** Constructor. */
public AxisStyle(LineStyle lineStyle, FontStyle labelStyle) { public AxisStyle(LineStyle lineStyle, FontStyle labelStyle) {
this(lineStyle, labelStyle, 0, null); this(lineStyle, labelStyle, 0, null, new DecimalFormat("#.##"));
} }
/** Constructor. */ /** Constructor. */
public AxisStyle(LineStyle lineStyle, FontStyle labelStyle, int numSegments, public AxisStyle(LineStyle lineStyle, FontStyle labelStyle, int numSegments,
FontStyle segmentStyle) { FontStyle segmentStyle) {
this(lineStyle, labelStyle, numSegments, segmentStyle, new DecimalFormat("#.##"));
}
/** Constructor. */
public AxisStyle(LineStyle lineStyle, FontStyle labelStyle, int numSegments,
FontStyle segmentStyle, DecimalFormat decimalFormat) {
this.lineStyle = requireNonNull(lineStyle); this.lineStyle = requireNonNull(lineStyle);
this.labelStyle = requireNonNull(labelStyle); this.labelStyle = requireNonNull(labelStyle);
this.segments = Math.max(1, numSegments); this.segments = Math.max(1, numSegments);
this.segmentStyle = segmentStyle; this.segmentStyle = segmentStyle;
this.decimalFormat = decimalFormat;
} }
/** Returns the line style. */ /** Returns the line style. */
...@@ -82,4 +93,9 @@ public final class AxisStyle { ...@@ -82,4 +93,9 @@ public final class AxisStyle {
public FontStyle getSegmentStyle() { public FontStyle getSegmentStyle() {
return segmentStyle; return segmentStyle;
} }
/** Returns the decimal format. */
public DecimalFormat getDecimalFormat() {
return decimalFormat;
}
} }
...@@ -21,6 +21,8 @@ import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_10PT ...@@ -21,6 +21,8 @@ import static org.fortiss.tooling.spiderchart.style.FontStyle.BLACK_VERDANA_10PT
import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT; import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT;
import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.LIGHT_GRAY; import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.LIGHT_GRAY;
import java.text.DecimalFormat;
import org.fortiss.tooling.spiderchart.model.DataSeries; import org.fortiss.tooling.spiderchart.model.DataSeries;
/** /**
...@@ -45,21 +47,26 @@ public final class DataSeriesStyle { ...@@ -45,21 +47,26 @@ public final class DataSeriesStyle {
private final boolean showIndicators; private final boolean showIndicators;
/** The flag to show indicator labels. */ /** The flag to show indicator labels. */
private final boolean showIndicatorLabels; private final boolean showIndicatorLabels;
/** The decimal formatter of the segment labels of the axis. */
private DecimalFormat decimalFormat;
/** Constructor. */ /** Constructor. */
public DataSeriesStyle(LineStyle lineStyle, FillStyle fillStyle, boolean withIndicators, public DataSeriesStyle(LineStyle lineStyle, FillStyle fillStyle, boolean withIndicators,
boolean withIndicatorLabels, FontStyle indicatorStyle, int indicatorSize) { boolean withIndicatorLabels, FontStyle indicatorStyle, int indicatorSize,
DecimalFormat decimalFormat) {
this.lineStyle = lineStyle; this.lineStyle = lineStyle;
this.fillStyle = fillStyle; this.fillStyle = fillStyle;
this.showIndicators = withIndicators; this.showIndicators = withIndicators;
this.showIndicatorLabels = withIndicatorLabels; this.showIndicatorLabels = withIndicatorLabels;
this.indicatorLabelStyle = indicatorStyle; this.indicatorLabelStyle = indicatorStyle;
this.indicatorSize = indicatorSize; this.indicatorSize = indicatorSize;
this.decimalFormat = decimalFormat;
} }
/** Constructor. */ /** Constructor. */
public DataSeriesStyle() { public DataSeriesStyle() {
this(SOLID_BLACK_1PT, new FillStyle(LIGHT_GRAY), true, true, BLACK_VERDANA_10PT, 7); this(SOLID_BLACK_1PT, new FillStyle(LIGHT_GRAY), true, true, BLACK_VERDANA_10PT, 7,
new DecimalFormat("#.##"));
} }
/** Returns line style. */ /** Returns line style. */
...@@ -91,4 +98,9 @@ public final class DataSeriesStyle { ...@@ -91,4 +98,9 @@ public final class DataSeriesStyle {
public int getIndicatorSize() { public int getIndicatorSize() {
return indicatorSize; return indicatorSize;
} }
/** Returns the decimal format. */
public DecimalFormat getDecimalFormat() {
return decimalFormat;
}
} }
...@@ -32,15 +32,15 @@ import org.fortiss.tooling.spiderchart.style.AxisStyle; ...@@ -32,15 +32,15 @@ import org.fortiss.tooling.spiderchart.style.AxisStyle;
public class AxisUtils { public class AxisUtils {
/** Returns the segment point on the axis if the the axis displayed with the given extents. */ /** Returns the segment point on the axis if the the axis displayed with the given extents. */
public static Point getSegmentPoint(AxisStyle style, int segment, int extentX, int extentY) { public static Point
int segments = style.getSegments(); getSegmentPoint(int numberOfSegments, int segment, int extentX, int extentY) {
int segmentX = extentX / segments; int segmentX = extentX / numberOfSegments;
int segmentY = extentY / segments; int segmentY = extentY / numberOfSegments;
return new Point(segmentX * segment, segmentY * segment); return new Point(segmentX * segment, segmentY * segment);
} }
/** Returns the segment point on the axis if the the axis displayed with the given extents. */ /** Returns the segment point on the axis if the the axis displayed with the given extents. */
public static Point getSegmentPoint(AxisStyle style, int segment, Point extent) { public static Point getSegmentPoint(AxisStyle style, int segment, Point extent) {
return getSegmentPoint(style, segment, extent.x, extent.y); return getSegmentPoint(style.getSegments(), segment, extent.x, extent.y);
} }
} }
...@@ -126,7 +126,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -126,7 +126,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
drawIndicator(gc, dStyle, p); drawIndicator(gc, dStyle, p);
} }
if(dStyle.isShowIndicatorLabels()) { if(dStyle.isShowIndicatorLabels()) {
drawIndicatorLabel(gc, dStyle.getIndicatorLabelStyle(), p, data.getAxisValue(axis)); drawIndicatorLabel(gc, dStyle, p, data.getAxisValue(axis));
} }
} }
path.close(); path.close();
...@@ -154,16 +154,16 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -154,16 +154,16 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
} }
/** Draws the indicator labels next to the chart point. */ /** Draws the indicator labels next to the chart point. */
private void drawIndicatorLabel(GC gc, FontStyle indicatorLabelStyle, Point p, Double value) { private void drawIndicatorLabel(GC gc, DataSeriesStyle dStyle, Point p, Double value) {
Font oldFont = gc.getFont(); Font oldFont = gc.getFont();
Font font = indicatorLabelStyle.createSWTFont(); Font font = dStyle.getIndicatorLabelStyle().createSWTFont();
gc.setFont(font); gc.setFont(font);
Color oldColor = gc.getForeground(); Color oldColor = gc.getForeground();
Color color = indicatorLabelStyle.createSWTColor(); Color color = dStyle.getIndicatorLabelStyle().createSWTColor();
gc.setForeground(color); gc.setForeground(color);
String label = value.toString(); String label = dStyle.getDecimalFormat().format(value);
Point textExtent = gc.textExtent(label); Point textExtent = gc.textExtent(label);
int x = p.x; int x = p.x;
if(x > center.x) { if(x > center.x) {
...@@ -221,11 +221,6 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -221,11 +221,6 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
int centerX = getX() + halfWidth; int centerX = getX() + halfWidth;
int centerY = getY() + halfHeight; int centerY = getY() + halfHeight;
double relativeMargin = 1.0 - style.getRelativeMargin();
int wExtent = (int)(relativeMargin * halfWidth);
int hExtent = (int)(relativeMargin * halfHeight);
int extent = Math.min(wExtent, hExtent);
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
Axis axis = axes.get(i); Axis axis = axes.get(i);
Point outer = axesEnds.get(axis); Point outer = axesEnds.get(axis);
...@@ -235,13 +230,14 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -235,13 +230,14 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
// draw label // draw label
drawAxisLabel(gc, axis, aStyle, centerX, centerY, outer.x, outer.y); drawAxisLabel(gc, axis, aStyle, centerX, centerY, outer.x, outer.y);
// draw segment indicators // draw segment indicators
int segments = aStyle.getSegments(); int segments =
style.isUseIndividualAxisSegments() ? aStyle.getSegments() : style
.getAxisSegments();
for(int s = 1; s <= segments; s++) { for(int s = 1; s <= segments; s++) {
double ratio = (double)s / (double)segments; double ratio = (double)s / (double)segments;
// FIXME: use locale info here String lbl = aStyle.getDecimalFormat().format(axis.getAxisValue(ratio));
String lbl = axis.getAxisValue(ratio).toString();
Point segmentPoint = Point segmentPoint =
getSegmentPoint(aStyle, s, outer.x - center.x, outer.y - center.y); getSegmentPoint(segments, s, outer.x - center.x, outer.y - center.y);
drawSegmentIndicator(gc, lbl, aStyle, centerX, centerY, segmentPoint); drawSegmentIndicator(gc, lbl, aStyle, centerX, centerY, segmentPoint);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment