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

Added enumeration axis option to spider chart.

refs 2605
parent b298d113
No related branches found
No related tags found
No related merge requests found
Showing
with 278 additions and 106 deletions
...@@ -31,14 +31,17 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.OLIVE; ...@@ -31,14 +31,17 @@ 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 java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Comparator;
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;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
import org.fortiss.tooling.spiderchart.model.Axis;
import org.fortiss.tooling.spiderchart.model.DataSeries; import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.DoubleAxis;
import org.fortiss.tooling.spiderchart.model.EnumerationAxis;
import org.fortiss.tooling.spiderchart.model.SpiderChart; import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.AxisStyle; import org.fortiss.tooling.spiderchart.style.AxisStyle;
import org.fortiss.tooling.spiderchart.style.ChartStyle; import org.fortiss.tooling.spiderchart.style.ChartStyle;
...@@ -67,12 +70,23 @@ public final class Sample { ...@@ -67,12 +70,23 @@ 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, 4.0); DoubleAxis battery = new DoubleAxis("Battery", 0.0, 4.0);
Axis camera = new Axis("Camera", 0.0, 7.0); DoubleAxis camera = new DoubleAxis("Camera", 0.0, 7.0);
Axis display = new Axis("Display", 0.0, 5.0); DoubleAxis display = new DoubleAxis("Display", 0.0, 5.0);
Axis memory = new Axis("Memory", 0.0, 5.0); DoubleAxis memory = new DoubleAxis("Memory", 0.0, 5.0);
Axis brand = new Axis("Brand", 0.0, 5.0); DoubleAxis brand = new DoubleAxis("Brand", 0.0, 5.0);
Axis screen = new Axis("Screen", 0.0, 5.0);
Screen sc0 = new Screen(0); // unused dummy element for spider chart center point
Screen sc100 = new Screen(100);
Screen sc240 = new Screen(240);
Screen sc320 = new Screen(320);
Screen sc640 = new Screen(640);
Screen sc1024 = new Screen(1024);
Screen[] screens = new Screen[] {sc0, sc100, sc240, sc320, sc640, sc1024};
EnumerationAxis<Screen> screen =
new EnumerationAxis<Screen>("Screen Width", screenComparator,
Arrays.asList(screens));
spiderChart.addAxis(battery); spiderChart.addAxis(battery);
spiderChart.addAxis(camera); spiderChart.addAxis(camera);
spiderChart.addAxis(display); spiderChart.addAxis(display);
...@@ -86,7 +100,7 @@ public final class Sample { ...@@ -86,7 +100,7 @@ public final class Sample {
iPhoneData.setPoint(display, 4.0); iPhoneData.setPoint(display, 4.0);
iPhoneData.setPoint(memory, 4.6); iPhoneData.setPoint(memory, 4.6);
iPhoneData.setPoint(brand, 5.0); iPhoneData.setPoint(brand, 5.0);
iPhoneData.setPoint(screen, 3.8); iPhoneData.setPoint(screen, sc240);
spiderChart.addData(iPhoneData); spiderChart.addData(iPhoneData);
DataSeries nexusData = new DataSeries("Nexus 6"); DataSeries nexusData = new DataSeries("Nexus 6");
...@@ -95,7 +109,7 @@ public final class Sample { ...@@ -95,7 +109,7 @@ public final class Sample {
nexusData.setPoint(display, 3.0); nexusData.setPoint(display, 3.0);
nexusData.setPoint(memory, 3.6); nexusData.setPoint(memory, 3.6);
nexusData.setPoint(brand, 4.0); nexusData.setPoint(brand, 4.0);
nexusData.setPoint(screen, 2.8); nexusData.setPoint(screen, sc640);
spiderChart.addData(nexusData); spiderChart.addData(nexusData);
ChartStyle chartStyle = new ChartStyle(true, true, false); ChartStyle chartStyle = new ChartStyle(true, true, false);
...@@ -159,4 +173,32 @@ public final class Sample { ...@@ -159,4 +173,32 @@ public final class Sample {
viewer.dispose(); viewer.dispose();
display.dispose(); display.dispose();
} }
private static class Screen implements Comparable<Screen> {
public int width = 0;
/** Constructor. */
public Screen(int width) {
this.width = width;
}
/** {@inheritDoc} */
@Override
public int compareTo(Screen o) {
return width - o.width;
}
/** {@inheritDoc} */
@Override
public String toString() {
return width + "px";
}
}
private static Comparator<Screen> screenComparator = new Comparator<Sample.Screen>() {
@Override
public int compare(Screen o1, Screen o2) {
return o1.width - o2.width;
}
};
} }
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2016 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.model;
import static java.util.Objects.requireNonNull;
/**
* Base class for different axis implementations.
*
* @author hoelzl
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating RED Hash:
*/
public abstract class AxisBase {
/** The name of the axis. */
private final String name;
/** Constructor. */
public AxisBase(String name) {
this.name = requireNonNull(name);
}
/** Returns name. */
public final String getName() {
return name;
}
/** Returns whether the given value is between minimum and maximum of this axis. */
public abstract boolean isValidValue(Object d);
}
...@@ -34,7 +34,7 @@ public final class DataSeries { ...@@ -34,7 +34,7 @@ public final class DataSeries {
/** The name of the data series. */ /** The name of the data series. */
private String name; private String name;
/** The points for each axis. */ /** The points for each axis. */
private Map<Axis, Double> points = new HashMap<Axis, Double>(); private Map<AxisBase, Object> points = new HashMap<AxisBase, Object>();
/** Constructor. */ /** Constructor. */
public DataSeries(String name) { public DataSeries(String name) {
...@@ -52,12 +52,12 @@ public final class DataSeries { ...@@ -52,12 +52,12 @@ public final class DataSeries {
} }
/** Sets the point for the given axis. */ /** Sets the point for the given axis. */
public void setPoint(Axis axis, Double value) { public <T extends Object> void setPoint(AxisBase axis, T value) {
points.put(requireNonNull(axis), requireNonNull(value)); points.put(requireNonNull(axis), requireNonNull(value));
} }
/** Returns the point for the given axis. */ /** Returns the point for the given axis. */
public Double getAxisValue(Axis a) { public Object getAxisValue(AxisBase a) {
return points.get(a); return points.get(a);
} }
} }
...@@ -17,7 +17,7 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ ...@@ -17,7 +17,7 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+--------------------------------------------------------------------------*/ +--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.model; package org.fortiss.tooling.spiderchart.model;
import static java.util.Objects.requireNonNull; import java.util.Comparator;
/** /**
* Class representing an axis of the spider chart. * Class representing an axis of the spider chart.
...@@ -27,29 +27,22 @@ import static java.util.Objects.requireNonNull; ...@@ -27,29 +27,22 @@ import static java.util.Objects.requireNonNull;
* @version $Rev: 18709 $ * @version $Rev: 18709 $
* @ConQAT.Rating YELLOW Hash: 6C5390A8B21E0877312C9EB36F009A6B * @ConQAT.Rating YELLOW Hash: 6C5390A8B21E0877312C9EB36F009A6B
*/ */
public final class Axis { public final class DoubleAxis extends AxisBase {
/** The name of the axis. */
private String name;
/** The minimum value of the axis (at the center of the spider chart). */ /** The minimum value of the axis (at the center of the spider chart). */
private Double minimumValue = 0.0; private Double minimumValue = 0.0;
/** The maximum value of this axis. */ /** The maximum value of this axis. */
private Double maximumValue = 1.0; private Double maximumValue = 1.0;
/** Constructor. */ /** Constructor. */
public Axis(Double min, Double max) { public DoubleAxis(Double min, Double max) {
this("", min, max); this("", min, max);
} }
/** Constructor. */ /** Constructor. */
public Axis(String name, Double min, Double max) { public DoubleAxis(String name, Double min, Double max) {
this.name = requireNonNull(name); super(name);
this.minimumValue = requireNonNull(min); this.minimumValue = min;
this.maximumValue = requireNonNull(max); this.maximumValue = max;
}
/** Returns maximum value. */
public Double getMaximumValue() {
return maximumValue;
} }
/** Returns minimum value. */ /** Returns minimum value. */
...@@ -57,9 +50,9 @@ public final class Axis { ...@@ -57,9 +50,9 @@ public final class Axis {
return minimumValue; return minimumValue;
} }
/** Returns name. */ /** Returns maximum value. */
public String getName() { public Double getMaximumValue() {
return name; return maximumValue;
} }
/** Returns the value for the given ratio of this axis. */ /** Returns the value for the given ratio of this axis. */
...@@ -72,4 +65,25 @@ public final class Axis { ...@@ -72,4 +65,25 @@ public final class Axis {
public Double getAxisRatio(Double value) { public Double getAxisRatio(Double value) {
return (value - minimumValue) / (maximumValue - minimumValue); return (value - minimumValue) / (maximumValue - minimumValue);
} }
/** Public comparator for value of DoubleAxis. */
public static final Comparator<Double> DOUBLE_COMPARATOR = new Comparator<Double>() {
@Override
public int compare(Double o1, Double o2) {
double delta = o1 - o2;
if(delta < 0.0) {
return -1;
} else if(delta > 0.0) {
return 1;
}
return 0;
}
};
/** {@inheritDoc} */
@Override
public boolean isValidValue(Object o) {
Double d = (Double)o;
return minimumValue <= d && d <= maximumValue;
}
} }
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2016 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.spiderchart.model;
import static java.util.Collections.sort;
import java.util.Comparator;
import java.util.List;
/**
* An axis that represents an enumeration.
*
* @author hoelzl
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating RED Hash:
*/
public final class EnumerationAxis<T extends Object> extends AxisBase {
/** The enumeration values of this axis. */
private final List<T> values;
/** Constructor. */
public EnumerationAxis(String name, Comparator<T> comparator, List<T> values) {
super(name);
this.values = values;
sort(this.values, comparator);
}
/** Returns the member at the given index. */
public T getEnumerationMemberAtIndex(int index) {
if(index >= 0 && index < values.size()) {
return values.get(index);
}
return null;
}
/** {@inheritDoc} */
@Override
public boolean isValidValue(Object d) {
return values.contains(d);
}
/** Returns the ratio for the given value. */
public Double getAxisRatio(T value) {
int index = values.indexOf(value);
if(index == -1) {
return 0.0;
}
return (double)index / (double)values.size();
}
}
...@@ -32,7 +32,7 @@ import java.util.List; ...@@ -32,7 +32,7 @@ import java.util.List;
*/ */
public final class SpiderChart { public final class SpiderChart {
/** The axes contained in this chart. */ /** The axes contained in this chart. */
private List<Axis> axes = new ArrayList<Axis>(); private List<AxisBase> axes = new ArrayList<>();
/** The data sets contained in this chart. */ /** The data sets contained in this chart. */
private List<DataSeries> dataSeries = new ArrayList<DataSeries>(); private List<DataSeries> dataSeries = new ArrayList<DataSeries>();
/** The title of the spider chart. */ /** The title of the spider chart. */
...@@ -46,7 +46,7 @@ public final class SpiderChart { ...@@ -46,7 +46,7 @@ public final class SpiderChart {
} }
/** Adds the given axis. */ /** Adds the given axis. */
public void addAxis(Axis a) { public void addAxis(AxisBase a) {
axes.add(a); axes.add(a);
} }
...@@ -66,12 +66,11 @@ public final class SpiderChart { ...@@ -66,12 +66,11 @@ public final class SpiderChart {
*/ */
public boolean checkDataValidity() { public boolean checkDataValidity() {
for(DataSeries d : dataSeries) { for(DataSeries d : dataSeries) {
for(Axis a : axes) { for(AxisBase a : axes) {
if(d.getAxisValue(a) == null) { if(d.getAxisValue(a) == null) {
return false; return false;
} }
Double v = d.getAxisValue(a); if(!a.isValidValue(d.getAxisValue(a))) {
if(v < a.getMinimumValue() || v > a.getMaximumValue()) {
return false; return false;
} }
} }
...@@ -80,7 +79,7 @@ public final class SpiderChart { ...@@ -80,7 +79,7 @@ public final class SpiderChart {
} }
/** Returns the unmodifiable list of axes. */ /** Returns the unmodifiable list of axes. */
public List<Axis> getAxes() { public List<AxisBase> getAxes() {
return asUnmodifiable(axes); return asUnmodifiable(axes);
} }
...@@ -89,42 +88,6 @@ public final class SpiderChart { ...@@ -89,42 +88,6 @@ public final class SpiderChart {
return asUnmodifiable(dataSeries); return asUnmodifiable(dataSeries);
} }
/** */
public double[] maxScales() {
double[] result = new double[axes.size()];
for(int i = 0; i < result.length; i++) {
result[i] = axes.get(i).getMaximumValue();
}
return result;
}
/** */
public double[] minScales() {
double[] result = new double[axes.size()];
for(int i = 0; i < result.length; i++) {
result[i] = axes.get(i).getMinimumValue();
}
return result;
}
/** */
public String[] axesNames() {
String[] result = new String[axes.size()];
for(int i = 0; i < result.length; i++) {
result[i] = axes.get(i).getName();
}
return result;
}
/** */
public String[] axesScalingLabelFormats() {
String[] result = new String[axes.size()];
for(int i = 0; i < result.length; i++) {
result[i] = "#.#";
}
return result;
}
/** Returns the legend label. */ /** Returns the legend label. */
public String getLegendLabel() { public String getLegendLabel() {
return legendLabel; return legendLabel;
......
...@@ -24,10 +24,10 @@ import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT; ...@@ -24,10 +24,10 @@ import static org.fortiss.tooling.spiderchart.style.LineStyle.SOLID_BLACK_1PT;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import org.fortiss.tooling.spiderchart.model.Axis; import org.fortiss.tooling.spiderchart.model.DoubleAxis;
/** /**
* Class for all style attributes for {@link Axis spider chart axes}. * Class for all style attributes for {@link DoubleAxis spider chart axes}.
* <P> * <P>
* If {@link #segmentStyle} is non-null then indicators will be drawn. * If {@link #segmentStyle} is non-null then indicators will be drawn.
* *
......
...@@ -23,7 +23,7 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.getBrighterColo ...@@ -23,7 +23,7 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.getBrighterColo
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.fortiss.tooling.spiderchart.model.Axis; import org.fortiss.tooling.spiderchart.model.AxisBase;
import org.fortiss.tooling.spiderchart.model.DataSeries; import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.SpiderChart; import org.fortiss.tooling.spiderchart.model.SpiderChart;
...@@ -37,7 +37,7 @@ import org.fortiss.tooling.spiderchart.model.SpiderChart; ...@@ -37,7 +37,7 @@ import org.fortiss.tooling.spiderchart.model.SpiderChart;
*/ */
public final class ChartStyle { public final class ChartStyle {
/** The map for style information of axes. */ /** The map for style information of axes. */
private Map<Axis, AxisStyle> axisStyle = new HashMap<Axis, AxisStyle>(); private Map<AxisBase, AxisStyle> axisStyle = new HashMap<AxisBase, AxisStyle>();
/** The map for style information of data series. */ /** The map for style information of data series. */
private Map<DataSeries, DataSeriesStyle> dataSeriesStyle = private Map<DataSeries, DataSeriesStyle> dataSeriesStyle =
new HashMap<DataSeries, DataSeriesStyle>(); new HashMap<DataSeries, DataSeriesStyle>();
...@@ -146,12 +146,12 @@ public final class ChartStyle { ...@@ -146,12 +146,12 @@ public final class ChartStyle {
} }
/** Returns the style for the given axis. */ /** Returns the style for the given axis. */
public AxisStyle getAxisStyle(Axis axis) { public AxisStyle getAxisStyle(AxisBase axis) {
return axisStyle.get(axis); return axisStyle.get(axis);
} }
/** Sets the style for the given axis. */ /** Sets the style for the given axis. */
public void setAxisStyle(Axis axis, AxisStyle style) { public void setAxisStyle(AxisBase axis, AxisStyle style) {
axisStyle.put(axis, style); axisStyle.put(axis, style);
} }
......
...@@ -23,7 +23,10 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.LIGHT_GRAY; ...@@ -23,7 +23,10 @@ import static org.fortiss.tooling.spiderchart.util.RGBColorUtils.LIGHT_GRAY;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import org.fortiss.tooling.spiderchart.model.AxisBase;
import org.fortiss.tooling.spiderchart.model.DataSeries; import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.DoubleAxis;
import org.fortiss.tooling.spiderchart.model.EnumerationAxis;
/** /**
* Class for all style attributes of {@link DataSeries data series}. * Class for all style attributes of {@link DataSeries data series}.
...@@ -102,4 +105,15 @@ public final class DataSeriesStyle { ...@@ -102,4 +105,15 @@ public final class DataSeriesStyle {
public DecimalFormat getDecimalFormat() { public DecimalFormat getDecimalFormat() {
return decimalFormat; return decimalFormat;
} }
/** Returns the formatted label for the given axis and value. */
public String getFormattedLabel(AxisBase axis, Object value) {
if(axis instanceof DoubleAxis && value instanceof Double) {
return getDecimalFormat().format(value);
}
if(axis instanceof EnumerationAxis) {
return value.toString();
}
return null;
}
} }
...@@ -18,11 +18,11 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $ ...@@ -18,11 +18,11 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
package org.fortiss.tooling.spiderchart.util; package org.fortiss.tooling.spiderchart.util;
import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Point;
import org.fortiss.tooling.spiderchart.model.Axis; import org.fortiss.tooling.spiderchart.model.DoubleAxis;
import org.fortiss.tooling.spiderchart.style.AxisStyle; import org.fortiss.tooling.spiderchart.style.AxisStyle;
/** /**
* Utility methods related to {@link Axis} and {@link AxisStyle}. * Utility methods related to {@link DoubleAxis} and {@link AxisStyle}.
* *
* @author hoelzl * @author hoelzl
* @author $Author: hoelzl $ * @author $Author: hoelzl $
......
...@@ -29,8 +29,10 @@ import org.eclipse.swt.graphics.Path; ...@@ -29,8 +29,10 @@ import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.fortiss.tooling.base.ui.utils.GCUtils.GCStateManager; import org.fortiss.tooling.base.ui.utils.GCUtils.GCStateManager;
import org.fortiss.tooling.spiderchart.model.Axis; import org.fortiss.tooling.spiderchart.model.AxisBase;
import org.fortiss.tooling.spiderchart.model.DataSeries; import org.fortiss.tooling.spiderchart.model.DataSeries;
import org.fortiss.tooling.spiderchart.model.DoubleAxis;
import org.fortiss.tooling.spiderchart.model.EnumerationAxis;
import org.fortiss.tooling.spiderchart.model.SpiderChart; import org.fortiss.tooling.spiderchart.model.SpiderChart;
import org.fortiss.tooling.spiderchart.style.AxisStyle; import org.fortiss.tooling.spiderchart.style.AxisStyle;
import org.fortiss.tooling.spiderchart.style.ChartStyle; import org.fortiss.tooling.spiderchart.style.ChartStyle;
...@@ -51,7 +53,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -51,7 +53,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
/** The center point of the chart. */ /** The center point of the chart. */
private Point center = new Point(0, 0); private Point center = new Point(0, 0);
/** The end points of the axes. */ /** The end points of the axes. */
private Map<Axis, Point> axesEnds = new HashMap<Axis, Point>(); private Map<AxisBase, Point> axesEnds = new HashMap<AxisBase, Point>();
/** The angle in degree between two axes. */ /** The angle in degree between two axes. */
private double segmentDegree = 0.0; private double segmentDegree = 0.0;
...@@ -82,7 +84,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -82,7 +84,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
/** Computes the end points of the axes. */ /** Computes the end points of the axes. */
private void computeAxisEndPoints() { private void computeAxisEndPoints() {
// compute the axis points // compute the axis points
List<Axis> axes = chart.getAxes(); List<AxisBase> axes = chart.getAxes();
int size = axes.size(); int size = axes.size();
segmentDegree = 360.0 / size; segmentDegree = 360.0 / size;
double currentAngleDegree = style.getStartAngleDegree(); double currentAngleDegree = style.getStartAngleDegree();
...@@ -98,7 +100,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -98,7 +100,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
int extent = Math.min(wExtent, hExtent); 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); AxisBase axis = axes.get(i);
// compute end point // compute end point
double startAngleRad = Math.PI * currentAngleDegree / 180.0; double startAngleRad = Math.PI * currentAngleDegree / 180.0;
int extentX = (int)(Math.cos(startAngleRad) * extent); int extentX = (int)(Math.cos(startAngleRad) * extent);
...@@ -113,7 +115,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -113,7 +115,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
private void drawBackground(GCStateManager gcState) { private void drawBackground(GCStateManager gcState) {
Path path = new Path(Display.getCurrent()); Path path = new Path(Display.getCurrent());
boolean first = true; boolean first = true;
for(Axis axis : chart.getAxes()) { for(AxisBase axis : chart.getAxes()) {
Point p = axesEnds.get(axis); Point p = axesEnds.get(axis);
if(first) { if(first) {
path.moveTo(p.x, p.y); path.moveTo(p.x, p.y);
...@@ -147,7 +149,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -147,7 +149,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
double ratio = (double)i / (double)style.getAxisSegments(); double ratio = (double)i / (double)style.getAxisSegments();
boolean first = true; boolean first = true;
Path p = new Path(Display.getCurrent()); Path p = new Path(Display.getCurrent());
for(Axis axis : chart.getAxes()) { for(AxisBase axis : chart.getAxes()) {
Point outer = axesEnds.get(axis); Point outer = axesEnds.get(axis);
int xExtent = outer.x - center.x; int xExtent = outer.x - center.x;
int yExtent = outer.y - center.y; int yExtent = outer.y - center.y;
...@@ -169,7 +171,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -169,7 +171,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
DataSeriesStyle dStyle = style.getDataSeriesStyle(data); DataSeriesStyle dStyle = style.getDataSeriesStyle(data);
Path path = new Path(Display.getCurrent()); Path path = new Path(Display.getCurrent());
boolean first = true; boolean first = true;
for(Axis axis : chart.getAxes()) { for(AxisBase axis : chart.getAxes()) {
Point p = computeDataPoint(axis, data); Point p = computeDataPoint(axis, data);
if(first) { if(first) {
path.moveTo(p.x, p.y); path.moveTo(p.x, p.y);
...@@ -181,7 +183,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -181,7 +183,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
drawIndicator(gcState, dStyle, p); drawIndicator(gcState, dStyle, p);
} }
if(dStyle.isShowIndicatorLabels()) { if(dStyle.isShowIndicatorLabels()) {
drawIndicatorLabel(gcState, dStyle, p, data.getAxisValue(axis)); drawIndicatorLabel(gcState, dStyle, p, axis, data.getAxisValue(axis));
} }
} }
path.close(); path.close();
...@@ -203,12 +205,12 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -203,12 +205,12 @@ 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(GCStateManager gcState, DataSeriesStyle dStyle, Point p, private void drawIndicatorLabel(GCStateManager gcState, DataSeriesStyle dStyle, Point p,
Double value) { AxisBase axis, Object value) {
gcState.fontStoreAndCreate(dStyle.getIndicatorLabelStyle().getFontData()); gcState.fontStoreAndCreate(dStyle.getIndicatorLabelStyle().getFontData());
gcState.foregroundStoreAndCreate(dStyle.getIndicatorLabelStyle().getRGBColor()); gcState.foregroundStoreAndCreate(dStyle.getIndicatorLabelStyle().getRGBColor());
GC gc = gcState.GC(); GC gc = gcState.GC();
String label = dStyle.getDecimalFormat().format(value); String label = dStyle.getFormattedLabel(axis, value);
Point textExtent = gc.textExtent(label); Point textExtent = gc.textExtent(label);
// data series indicator labels are always to the right of the point // data series indicator labels are always to the right of the point
int x = p.x + 4; int x = p.x + 4;
...@@ -234,8 +236,17 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -234,8 +236,17 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
} }
/** Computes the point on the given axis for the given data. */ /** Computes the point on the given axis for the given data. */
private Point computeDataPoint(Axis axis, DataSeries data) { @SuppressWarnings("unchecked")
Double ratio = axis.getAxisRatio(data.getAxisValue(axis)); private Point computeDataPoint(AxisBase axis, DataSeries data) {
Double ratio = 0.0;
if(axis instanceof DoubleAxis) {
DoubleAxis da = (DoubleAxis)axis;
ratio = da.getAxisRatio((Double)data.getAxisValue(axis));
}
if(axis instanceof EnumerationAxis) {
EnumerationAxis<Object> ea = (EnumerationAxis<Object>)axis;
ratio = ea.getAxisRatio(data.getAxisValue(ea));
}
Point outer = axesEnds.get(axis); Point outer = axesEnds.get(axis);
int deltaX = (int)(ratio * (outer.x - center.x)); int deltaX = (int)(ratio * (outer.x - center.x));
int deltaY = (int)(ratio * (outer.y - center.y)); int deltaY = (int)(ratio * (outer.y - center.y));
...@@ -243,8 +254,8 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -243,8 +254,8 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
} }
/** Draws the axes of the spider chart. */ /** Draws the axes of the spider chart. */
private Map<Axis, Point> drawAxes(GCStateManager gcState) { private Map<AxisBase, Point> drawAxes(GCStateManager gcState) {
List<Axis> axes = chart.getAxes(); List<AxisBase> axes = chart.getAxes();
int size = axes.size(); int size = axes.size();
double segmentDegree = 360.0 / size; double segmentDegree = 360.0 / size;
double angleDegree = 90.0; double angleDegree = 90.0;
...@@ -255,7 +266,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -255,7 +266,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
int centerY = getY() + halfHeight; int centerY = getY() + halfHeight;
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
Axis axis = axes.get(i); AxisBase axis = axes.get(i);
Point outer = axesEnds.get(axis); Point outer = axesEnds.get(axis);
// draw line // draw line
AxisStyle aStyle = style.getAxisStyle(axis); AxisStyle aStyle = style.getAxisStyle(axis);
...@@ -264,23 +275,38 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -264,23 +275,38 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
drawAxisLabel(gcState, axis, aStyle, centerX, centerY, outer.x, outer.y); drawAxisLabel(gcState, axis, aStyle, centerX, centerY, outer.x, outer.y);
if(aStyle.isUseSegmentIndicators()) { if(aStyle.isUseSegmentIndicators()) {
// draw segment indicators drawSegmentIndicators(gcState, centerX, centerY, axis, outer, aStyle);
int segments =
style.isUseIndividualAxisSegments() ? aStyle.getSegments() : style
.getAxisSegments();
for(int s = 1; s <= segments; s++) {
double ratio = (double)s / (double)segments;
String lbl = aStyle.getDecimalFormat().format(axis.getAxisValue(ratio));
Point segmentPoint =
getSegmentPoint(segments, s, outer.x - center.x, outer.y - center.y);
drawSegmentIndicator(gcState, lbl, aStyle, centerX, centerY, segmentPoint);
}
} }
angleDegree = (angleDegree + segmentDegree) % 360.0; angleDegree = (angleDegree + segmentDegree) % 360.0;
} }
return axesEnds; return axesEnds;
} }
/** Draws the indicators of the given axis. */
private void drawSegmentIndicators(GCStateManager gcState, int centerX, int centerY,
AxisBase axis, Point outer, AxisStyle aStyle) {
// draw segment indicators
int segments =
style.isUseIndividualAxisSegments() ? aStyle.getSegments() : style
.getAxisSegments();
for(int s = 1; s <= segments; s++) {
String lbl = "";
if(axis instanceof DoubleAxis) {
double ratio = (double)s / (double)segments;
Double value = ((DoubleAxis)axis).getAxisValue(ratio);
lbl = aStyle.getDecimalFormat().format(value);
} else if(axis instanceof EnumerationAxis) {
Object value = ((EnumerationAxis<?>)axis).getEnumerationMemberAtIndex(s);
if(value != null) {
lbl = value.toString();
}
}
Point segmentPoint =
getSegmentPoint(segments, s, outer.x - center.x, outer.y - center.y);
drawSegmentIndicator(gcState, lbl, aStyle, centerX, centerY, segmentPoint);
}
}
/** Draws the indicator on the axis. */ /** Draws the indicator on the axis. */
private void drawSegmentIndicator(GCStateManager gcState, String lbl, AxisStyle axisStyle, private void drawSegmentIndicator(GCStateManager gcState, String lbl, AxisStyle axisStyle,
int centerX, int centerY, Point segment) { int centerX, int centerY, Point segment) {
...@@ -298,7 +324,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase { ...@@ -298,7 +324,7 @@ public final class SpiderChartWidget extends SpiderChartWidgetBase {
} }
/** Draws the axis label at the correct position. */ /** Draws the axis label at the correct position. */
private void drawAxisLabel(GCStateManager gcState, Axis axis, AxisStyle style, int centerX, private void drawAxisLabel(GCStateManager gcState, AxisBase axis, AxisStyle style, int centerX,
int centerY, int outerX, int outerY) { int centerY, int outerX, int outerY) {
String axisLabel = axis.getName(); String axisLabel = axis.getName();
FontData fontData = style.getLabelStyle().getFontData(); FontData fontData = style.getLabelStyle().getFontData();
......
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