mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
LineChart auf ChartValue interface umgestellt
This commit is contained in:
@@ -1,30 +1,19 @@
|
|||||||
package schule.ngb.zm.charts;
|
package schule.ngb.zm.charts;
|
||||||
|
|
||||||
import schule.ngb.zm.shapes.Rectangle;
|
import schule.ngb.zm.shapes.Rectangle;
|
||||||
|
import schule.ngb.zm.util.Validator;
|
||||||
|
|
||||||
import java.awt.BasicStroke;
|
import java.awt.BasicStroke;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class LineChart extends Rectangle {
|
public class LineChart extends Rectangle {
|
||||||
|
|
||||||
private class LineChartValue {
|
protected SortedMap<Double, ChartValue> values;
|
||||||
|
|
||||||
double x;
|
|
||||||
|
|
||||||
double value;
|
|
||||||
|
|
||||||
public LineChartValue( double x, double value ) {
|
|
||||||
this.x = x;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected SortedMap<Double, LineChartValue> values;
|
|
||||||
|
|
||||||
protected boolean drawLines = true;
|
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 ) {
|
public LineChart( double x, double y, double width, double height, double[] pCoordinates, double[] pValues ) {
|
||||||
super(x, y, width, height);
|
super(x, y, width, height);
|
||||||
|
|
||||||
this.values = new TreeMap<Double, LineChartValue>();
|
this.values = new TreeMap<>();
|
||||||
if( pCoordinates != null || pValues != null ) {
|
if( pCoordinates != null || pValues != null ) {
|
||||||
if( pCoordinates == null ) {
|
if( pCoordinates == null ) {
|
||||||
pCoordinates = new double[0];
|
pCoordinates = new double[0];
|
||||||
@@ -56,7 +45,7 @@ public class LineChart extends Rectangle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addValue( double pCoordinate, double pValue ) {
|
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 ) {
|
public void removeValue( double x ) {
|
||||||
@@ -64,11 +53,19 @@ public class LineChart extends Rectangle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public double getValue( double x ) {
|
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 ) {
|
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
|
@Override
|
||||||
@@ -76,10 +73,10 @@ public class LineChart extends Rectangle {
|
|||||||
double xMax = values.lastKey();
|
double xMax = values.lastKey();
|
||||||
double yMax = Double.MIN_VALUE;
|
double yMax = Double.MIN_VALUE;
|
||||||
|
|
||||||
Collection<LineChartValue> val = values.values();
|
Collection<ChartValue> val = values.values();
|
||||||
for( LineChartValue lcv : val ) {
|
for( ChartValue lcv : val ) {
|
||||||
if( lcv.value > yMax ) {
|
if( lcv.getValue() > yMax ) {
|
||||||
yMax = lcv.value;
|
yMax = lcv.getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,12 +88,12 @@ public class LineChart extends Rectangle {
|
|||||||
|
|
||||||
drawAxes(graphics, xUnit, yUnit);
|
drawAxes(graphics, xUnit, yUnit);
|
||||||
|
|
||||||
LineChartValue lastLcv = null;
|
ChartValue lastLcv = null;
|
||||||
for( LineChartValue lcv : val ) {
|
for( ChartValue lcv : val ) {
|
||||||
if( drawLines && lastLcv != null ) {
|
if( drawLines && lastLcv != null ) {
|
||||||
graphics.setColor(getStrokeColor().getJavaColor());
|
graphics.setColor(getStrokeColor().getJavaColor());
|
||||||
graphics.setStroke(createStroke());
|
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);
|
drawDot(graphics, lastLcv, xUnit, yUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,10 +104,10 @@ public class LineChart extends Rectangle {
|
|||||||
graphics.setTransform(originalTransform);
|
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);
|
int dotSize = (int) round(strokeWeight * 2);
|
||||||
graphics.setColor(getFillColor().getJavaColor());
|
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 ) {
|
private void drawAxes( Graphics2D graphics, double xUnit, double yUnit ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user