diff --git a/src/schule/ngb/zm/charts/LineChart.java b/src/schule/ngb/zm/charts/LineChart.java index 94fcfa1..633e0fe 100644 --- a/src/schule/ngb/zm/charts/LineChart.java +++ b/src/schule/ngb/zm/charts/LineChart.java @@ -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 values; + protected SortedMap 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(); + 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 val = values.values(); - for( LineChartValue lcv : val ) { - if( lcv.value > yMax ) { - yMax = lcv.value; + Collection 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 ) {