LineChart auf ChartValue interface umgestellt

This commit is contained in:
ngb
2022-07-06 22:17:42 +02:00
parent e78ec53a53
commit f9f1cfed24

View File

@@ -1,30 +1,19 @@
package schule.ngb.zm.charts;
import schule.ngb.zm.shapes.Rectangle;
import schule.ngb.zm.util.Validator;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.TreeMap;
public class LineChart extends Rectangle {
private class LineChartValue {
double x;
double value;
public LineChartValue( double x, double value ) {
this.x = x;
this.value = value;
}
}
protected SortedMap<Double, LineChartValue> values;
protected SortedMap<Double, ChartValue> values;
protected boolean drawLines = true;
@@ -35,7 +24,7 @@ public class LineChart extends Rectangle {
public LineChart( double x, double y, double width, double height, double[] pCoordinates, double[] pValues ) {
super(x, y, width, height);
this.values = new TreeMap<Double, LineChartValue>();
this.values = new TreeMap<>();
if( pCoordinates != null || pValues != null ) {
if( pCoordinates == null ) {
pCoordinates = new double[0];
@@ -56,7 +45,7 @@ public class LineChart extends Rectangle {
}
public void addValue( double pCoordinate, double pValue ) {
values.put(pCoordinate, new LineChartValue(pCoordinate, pValue));
values.put(pCoordinate, new BasicChartValue(pCoordinate, pValue));
}
public void removeValue( double x ) {
@@ -64,11 +53,19 @@ public class LineChart extends Rectangle {
}
public double getValue( double x ) {
return values.get(x).value;
ChartValue value = values.get(x);
if( value == null )
throw new NoSuchElementException(String.format("No chart value for x-value <%f> found.", x));
return value.getValue();
}
public void setValue( double x, double pValue ) {
values.get(x).value = pValue;
ChartValue value = values.get(x);
if( value != null ) {
value.setValue(pValue);
} else {
this.addValue(x, pValue);
}
}
@Override
@@ -76,10 +73,10 @@ public class LineChart extends Rectangle {
double xMax = values.lastKey();
double yMax = Double.MIN_VALUE;
Collection<LineChartValue> val = values.values();
for( LineChartValue lcv : val ) {
if( lcv.value > yMax ) {
yMax = lcv.value;
Collection<ChartValue> val = values.values();
for( ChartValue lcv : val ) {
if( lcv.getValue() > yMax ) {
yMax = lcv.getValue();
}
}
@@ -91,12 +88,12 @@ public class LineChart extends Rectangle {
drawAxes(graphics, xUnit, yUnit);
LineChartValue lastLcv = null;
for( LineChartValue lcv : val ) {
ChartValue lastLcv = null;
for( ChartValue lcv : val ) {
if( drawLines && lastLcv != null ) {
graphics.setColor(getStrokeColor().getJavaColor());
graphics.setStroke(createStroke());
graphics.drawLine((int)(lastLcv.x*xUnit), (int)(height - lastLcv.value*yUnit), (int)(lcv.x*xUnit), (int)(height - lcv.value*yUnit));
graphics.drawLine((int)(lastLcv.getX()*xUnit), (int)(height - lastLcv.getValue()*yUnit), (int)(lcv.getX()*xUnit), (int)(height - lcv.getValue()*yUnit));
drawDot(graphics, lastLcv, xUnit, yUnit);
}
@@ -107,10 +104,10 @@ public class LineChart extends Rectangle {
graphics.setTransform(originalTransform);
}
private void drawDot( Graphics2D graphics, LineChartValue lcv, double xUnit, double yUnit ) {
private void drawDot( Graphics2D graphics, ChartValue lcv, double xUnit, double yUnit ) {
int dotSize = (int) round(strokeWeight * 2);
graphics.setColor(getFillColor().getJavaColor());
graphics.fillRect((int)(lcv.x*xUnit - dotSize), (int)(height - lcv.value*yUnit - dotSize), dotSize+dotSize, dotSize+dotSize);
graphics.fillRect((int)(lcv.getX()*xUnit - dotSize), (int)(height - lcv.getValue()*yUnit - dotSize), dotSize+dotSize, dotSize+dotSize);
}
private void drawAxes( Graphics2D graphics, double xUnit, double yUnit ) {