diff --git a/README.md b/README.md index 910c6884dfc6d980afb285e8e7465412435b880d..25844674b61d240fac9d2498b881817fa796e396 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Add this to dependencies in pom.xml: <dependency> <groupId>com.xeiam</groupId> <artifactId>xchart</artifactId> - <version>1.2.0-SNAPSHOT</version> + <version>1.1.0</version> </dependency> Building diff --git a/src/main/java/com/xeiam/xchart/Axis.java b/src/main/java/com/xeiam/xchart/Axis.java index e7722f69ea28f63032fc4d5bd5d9bd1d18862a3d..eb7a5c0b889946639f7729ab03750b4beda29688 100644 --- a/src/main/java/com/xeiam/xchart/Axis.java +++ b/src/main/java/com/xeiam/xchart/Axis.java @@ -19,6 +19,7 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; +import java.math.BigDecimal; import com.xeiam.xchart.interfaces.IChartPart; @@ -29,9 +30,17 @@ import com.xeiam.xchart.interfaces.IChartPart; */ public class Axis implements IChartPart { + public enum AxisType { + + NUMBER, DATE; + } + /** the chart */ private Chart chart; + /** the seriesType */ + private AxisType axisType; + /** the axisPair */ private AxisPair axisPair; @@ -41,15 +50,12 @@ public class Axis implements IChartPart { /** the axis tick */ private AxisTick axisTick; - /** the grid */ - private AxisLine axisLine; - /** the axis direction */ private Direction direction; - private Double min = null; + private BigDecimal min = null; - private Double max = null; + private BigDecimal max = null; /** the bounds */ private Rectangle bounds = new Rectangle(); // default all-zero rectangle @@ -81,22 +87,20 @@ public class Axis implements IChartPart { axisTitle = new AxisTitle(this); axisTick = new AxisTick(this); - axisLine = new AxisLine(this); } /** * @param min * @param max */ - public void addMinMax(double min, double max) { + public void addMinMax(BigDecimal min, BigDecimal max) { // System.out.println(min); // System.out.println(max); - - if (this.min == null || min < this.min) { + if (this.min == null || min.compareTo(this.min) < 0) { this.min = min; } - if (this.max == null || max > this.max) { + if (this.max == null || max.compareTo(this.max) > 0) { this.max = max; } @@ -104,6 +108,19 @@ public class Axis implements IChartPart { // System.out.println(this.max); } + public void setAxisType(AxisType axisType) { + + if (this.axisType != null && this.axisType != axisType) { + throw new IllegalArgumentException("Date and Number Axes cannot be mixed on the same chart!! "); + } + this.axisType = axisType; + } + + public AxisType getAxisType() { + + return axisType; + } + public Direction getDirection() { return direction; @@ -140,12 +157,12 @@ public class Axis implements IChartPart { return axisTick; } - public double getMin() { + public BigDecimal getMin() { return min; } - public double getMax() { + public BigDecimal getMax() { return max; } @@ -158,7 +175,7 @@ public class Axis implements IChartPart { if (direction == Direction.X) { // X-Axis // Axis title - double titleHeight = 0; + double titleHeight = 0.0; if (axisTitle.isVisible) { TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTitle.getFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); @@ -166,12 +183,13 @@ public class Axis implements IChartPart { } // Axis tick labels - TextLayout textLayout = new TextLayout("0", axisTick.getAxisTickLabels().getFont(), new FontRenderContext(null, true, false)); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - double axisTickLabelsHeight = rectangle.getHeight(); - - double gridStrokeWidth = axisLine.getStroke().getLineWidth(); - return (int) (titleHeight + axisTickLabelsHeight + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + gridStrokeWidth + Plot.PLOT_PADDING); + double axisTickLabelsHeight = 0.0; + if (axisTick.isVisible) { + TextLayout textLayout = new TextLayout("0", axisTick.getAxisTickLabels().getFont(), new FontRenderContext(null, true, false)); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + axisTickLabelsHeight = rectangle.getHeight() + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + Plot.PLOT_PADDING; + } + return (int) (titleHeight + axisTickLabelsHeight); } else { // Y-Axis return 0; // We layout the yAxis first depending in the xAxis height hint. We don't care about the yAxis height hint } @@ -202,11 +220,10 @@ public class Axis implements IChartPart { // fill in Axis with sub-components axisTitle.paint(g); axisTick.paint(g); - axisLine.paint(g); xOffset = (int) paintZone.getX(); yOffset = (int) paintZone.getY(); - width = (int) (axisTitle.isVisible ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth() + (int) axisLine.getBounds().getWidth(); + width = (int) (axisTitle.isVisible ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth(); height = (int) paintZone.getHeight(); bounds = new Rectangle(xOffset, yOffset, width, height); // g.setColor(Color.yellow); @@ -217,9 +234,9 @@ public class Axis implements IChartPart { // calculate paint zone // |____________________| - int xOffset = (int) (axisPair.getYAxis().getBounds().getWidth() + Plot.PLOT_PADDING + Chart.CHART_PADDING - 1); + int xOffset = (int) (axisPair.getYAxis().getBounds().getWidth() + (axisPair.getYAxis().getAxisTick().isVisible ? Plot.PLOT_PADDING : 0) + Chart.CHART_PADDING); int yOffset = (int) (axisPair.getYAxis().getBounds().getY() + axisPair.getYAxis().getBounds().getHeight()); - int width = (int) (chart.getWidth() - axisPair.getYAxis().getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - 3 * Chart.CHART_PADDING); + int width = (int) (chart.getWidth() - axisPair.getYAxis().getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (chart.getLegend().isVisible ? 3 : 2) * Chart.CHART_PADDING); int height = this.getSizeHint(); Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); this.paintZone = xAxisRectangle; @@ -228,12 +245,11 @@ public class Axis implements IChartPart { axisTitle.paint(g); axisTick.paint(g); - axisLine.paint(g); xOffset = (int) paintZone.getX(); yOffset = (int) paintZone.getY(); - width = ((int) paintZone.getWidth()); - height = (int) (axisTitle.isVisible ? axisTitle.getBounds().getHeight() : 0 + axisTick.getBounds().getHeight() + axisLine.getBounds().getHeight()); + width = (int) paintZone.getWidth(); + height = (int) ((axisTitle.isVisible ? axisTitle.getBounds().getHeight() : 0) + (int) axisTick.getBounds().getHeight()); bounds = new Rectangle(xOffset, yOffset, width, height); bounds = new Rectangle(xOffset, yOffset, width, height); // g.setColor(Color.yellow); diff --git a/src/main/java/com/xeiam/xchart/AxisLine.java b/src/main/java/com/xeiam/xchart/AxisLine.java deleted file mode 100644 index 54a8761c5334c0cd3223cb4152d156c86a7e561a..0000000000000000000000000000000000000000 --- a/src/main/java/com/xeiam/xchart/AxisLine.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Copyright 2011-2012 Xeiam LLC. - * - * 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 com.xeiam.xchart; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.Rectangle; - -import com.xeiam.xchart.interfaces.IChartPart; - - -/** - * AxisLine - */ -public class AxisLine implements IChartPart { - - /** the axis */ - private Axis axis; - - /** the visibility state of grid */ - protected boolean isVisible = true; // default to true - - /** the foreground color */ - private Color foreground = ChartColor.getAWTColor(ChartColor.DARK_GREY); // default foreground color - - /** the line style */ - private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); - - /** the bounds */ - private Rectangle bounds = new Rectangle(); // default all-zero rectangle - - /** - * Constructor - * - * @param axis the axis - */ - public AxisLine(Axis axis) { - - this.axis = axis; - - } - - public BasicStroke getStroke() { - - return stroke; - } - - @Override - public void paint(Graphics2D g) { - - g.setColor(foreground); - - if (axis.getDirection() == Axis.Direction.Y) { - - int xOffset = (int) (axis.getAxisTick().getBounds().getX() + axis.getAxisTick().getBounds().getWidth()); - int yOffset = (int) (axis.getPaintZone().getY()); - - g.setColor(foreground); - g.setStroke(stroke); - - g.drawLine(xOffset, yOffset, xOffset, (int) (yOffset + axis.getPaintZone().getHeight())); - - // bounds - bounds = new Rectangle(xOffset, yOffset, (int) stroke.getLineWidth(), (int) axis.getPaintZone().getHeight()); - // g.setColor(Color.green); - // g.draw(bounds); - - } else { - - int xOffset = (int) (axis.getPaintZone().getX()); - int yOffset = (int) (axis.getAxisTick().getBounds().getY() - stroke.getLineWidth()); - - g.setColor(foreground); - g.setStroke(stroke); - - g.drawLine(xOffset, yOffset, (int) (xOffset + axis.getPaintZone().getWidth()), yOffset); - - // bounds - bounds = new Rectangle(xOffset, yOffset, (int) axis.getPaintZone().getWidth(), (int) stroke.getLineWidth()); - // g.setColor(Color.green); - // g.draw(bounds); - - } - } - - @Override - public Rectangle getBounds() { - - // TODO Auto-generated method stub - return bounds; - } -} diff --git a/src/main/java/com/xeiam/xchart/AxisPair.java b/src/main/java/com/xeiam/xchart/AxisPair.java index d332384c261b82562543d5599735f4650cd3c9b7..2a7e988693aa64f8ff1f30fcdcfd41821f241419 100644 --- a/src/main/java/com/xeiam/xchart/AxisPair.java +++ b/src/main/java/com/xeiam/xchart/AxisPair.java @@ -19,9 +19,12 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import com.xeiam.xchart.Axis.AxisType; import com.xeiam.xchart.interfaces.IChartPart; import com.xeiam.xchart.series.Series; @@ -55,10 +58,11 @@ public class AxisPair implements IChartPart { } /** + * @param <T> * @param xData * @param yData */ - public Series addSeries(String seriesName, Collection<Number> xData, Collection<Number> yData, Collection<Number> errorBars) { + public <T> Series addSeries(String seriesName, Collection<T> xData, Collection<Number> yData, Collection<Number> errorBars) { // Sanity checks if (seriesName == null) { @@ -73,22 +77,30 @@ public class AxisPair implements IChartPart { if (xData != null && xData.size() == 0) { throw new IllegalArgumentException("X-Axis data cannot be empty!!!"); } - if (xData != null && xData.size() == 1 && Double.isNaN(xData.iterator().next().doubleValue())) { - throw new IllegalArgumentException("X-Axis data cannot contain a single NaN value!!!"); - } if (yData.size() == 1 && Double.isNaN(yData.iterator().next().doubleValue())) { throw new IllegalArgumentException("Y-Axis data cannot contain a single NaN value!!!"); } - Series series; + Series series = null; if (xData != null) { - series = new Series(seriesName, xData, yData, errorBars); + // Check if xAxis series contains Number or Date data + Iterator<?> itr = xData.iterator(); + Object dataPoint = itr.next(); + if (dataPoint instanceof Number) { + xAxis.setAxisType(AxisType.NUMBER); + } else if (dataPoint instanceof Date) { + xAxis.setAxisType(AxisType.DATE); + } + yAxis.setAxisType(AxisType.NUMBER); + series = new Series(seriesName, xData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars); } else { // generate xData Collection<Number> generatedXData = new ArrayList<Number>(); for (int i = 1; i < yData.size(); i++) { generatedXData.add(i); } - series = new Series(seriesName, generatedXData, yData, errorBars); + xAxis.setAxisType(AxisType.NUMBER); + yAxis.setAxisType(AxisType.NUMBER); + series = new Series(seriesName, generatedXData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars); } // Sanity check diff --git a/src/main/java/com/xeiam/xchart/AxisTick.java b/src/main/java/com/xeiam/xchart/AxisTick.java index f307131b199712d50182f289838062ff34180329..63f5858773463ce0ce6d4091fb5c8120aa23e35b 100644 --- a/src/main/java/com/xeiam/xchart/AxisTick.java +++ b/src/main/java/com/xeiam/xchart/AxisTick.java @@ -20,15 +20,18 @@ import java.awt.Rectangle; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.Format; +import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; +import com.xeiam.xchart.Axis.AxisType; import com.xeiam.xchart.interfaces.IChartPart; +import com.xeiam.xchart.interfaces.IHideable; /** * An axis tick. */ -public class AxisTick implements IChartPart { +public class AxisTick implements IChartPart, IHideable { /** the axis */ private Axis axis; @@ -50,6 +53,7 @@ public class AxisTick implements IChartPart { /** the default tick mark step hint */ private static final int DEFAULT_TICK_MARK_STEP_HINT = 64; + /** the padding between the tick labels and the tick marks */ protected final static int AXIS_TICK_PADDING = 4; /** the normal format for tick labels */ @@ -58,9 +62,15 @@ public class AxisTick implements IChartPart { /** the scientific format for tick labels */ private Format scientificFormat = new DecimalFormat("0.###E0"); + // private SimpleDateFormat simpleDateformat = new SimpleDateFormat("dd.MM.yyyy"); + private SimpleDateFormat simpleDateformat = new SimpleDateFormat("MM-dd"); + /** the bounds */ private Rectangle bounds = new Rectangle(); // default all-zero rectangle + /** the visibility state of axistick */ + protected boolean isVisible = true; // default to true + /** * Constructor. * @@ -126,19 +136,21 @@ public class AxisTick implements IChartPart { // System.out.println(label); // } - axisTickLabels.paint(g); - axisTickMarks.paint(g); + if (isVisible) { + axisTickLabels.paint(g); + axisTickMarks.paint(g); - if (axis.getDirection() == Axis.Direction.Y) { - bounds = new Rectangle((int) axisTickLabels.getBounds().getX(), (int) (axisTickLabels.getBounds().getY()), (int) (axisTickLabels.getBounds().getWidth() + AXIS_TICK_PADDING + axisTickMarks.getBounds().getWidth()), - (int) (axisTickMarks.getBounds().getHeight())); - // g.setColor(Color.red); - // g.draw(bounds); - } else { - bounds = new Rectangle((int) axisTickMarks.getBounds().getX(), (int) (axisTickMarks.getBounds().getY()), (int) axisTickLabels.getBounds().getWidth(), (int) (axisTickMarks.getBounds().getHeight() - + AXIS_TICK_PADDING + axisTickLabels.getBounds().getHeight())); - // g.setColor(Color.red); - // g.draw(bounds); + if (axis.getDirection() == Axis.Direction.Y) { + bounds = new Rectangle((int) axisTickLabels.getBounds().getX(), (int) (axisTickLabels.getBounds().getY()), + (int) (axisTickLabels.getBounds().getWidth() + AXIS_TICK_PADDING + axisTickMarks.getBounds().getWidth()), (int) (axisTickMarks.getBounds().getHeight())); + // g.setColor(Color.red); + // g.draw(bounds); + } else { + bounds = new Rectangle((int) axisTickMarks.getBounds().getX(), (int) (axisTickMarks.getBounds().getY()), (int) axisTickLabels.getBounds().getWidth(), (int) (axisTickMarks.getBounds().getHeight() + + AXIS_TICK_PADDING + axisTickLabels.getBounds().getHeight())); + // g.setColor(Color.red); + // g.draw(bounds); + } } } @@ -161,7 +173,7 @@ public class AxisTick implements IChartPart { tickLocations.add((int) (margin + tickSpace / 2.0)); } else { - final BigDecimal MIN = new BigDecimal(new Double(axis.getMin()).toString()); + final BigDecimal MIN = new BigDecimal(axis.getMin().doubleValue()); BigDecimal firstPosition; BigDecimal gridStep = getGridStep(tickSpace); @@ -172,11 +184,11 @@ public class AxisTick implements IChartPart { firstPosition = MIN.subtract(MIN.remainder(gridStep)).add(gridStep); } - for (BigDecimal b = firstPosition; b.doubleValue() <= axis.getMax(); b = b.add(gridStep)) { + for (BigDecimal b = firstPosition; b.compareTo(axis.getMax()) <= 0; b = b.add(gridStep)) { // System.out.println("b= " + b); - tickLabels.add(format(b.doubleValue())); - int tickLabelPosition = (int) (margin + ((b.doubleValue() - axis.getMin()) / (axis.getMax() - axis.getMin()) * tickSpace)); + tickLabels.add(format(b)); + int tickLabelPosition = (int) (margin + ((b.subtract(axis.getMin())).doubleValue() / (axis.getMax().subtract(axis.getMin())).doubleValue() * tickSpace)); // System.out.println("tickLabelPosition= " + tickLabelPosition); tickLocations.add(tickLabelPosition); @@ -186,7 +198,7 @@ public class AxisTick implements IChartPart { private BigDecimal getGridStep(int tickSpace) { - double length = Math.abs(axis.getMax() - axis.getMin()); + double length = Math.abs(axis.getMax().subtract(axis.getMin()).doubleValue()); // System.out.println(axis.getMax()); // System.out.println(axis.getMin()); // System.out.println(length); @@ -246,13 +258,23 @@ public class AxisTick implements IChartPart { return value; } - private String format(double value) { + private String format(BigDecimal value) { - if (Math.abs(value) < 9999 && Math.abs(value) > .0001 || value == 0) { - return this.normalFormat.format(value); + if (axis.getAxisType() == AxisType.NUMBER) { + if (Math.abs(value.doubleValue()) < 9999 && Math.abs(value.doubleValue()) > .0001 || value.doubleValue() == 0) { + return normalFormat.format(value.doubleValue()); + } else { + return scientificFormat.format(value.doubleValue()); + } } else { - return this.scientificFormat.format(value); + return simpleDateformat.format(value.longValueExact()); } + } + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } } diff --git a/src/main/java/com/xeiam/xchart/AxisTickLabels.java b/src/main/java/com/xeiam/xchart/AxisTickLabels.java index 65dc0dc233daf87389cde0fa618a9ba9388ee583..d9332c720cefb997dec37c1765bdb1dca56079e1 100644 --- a/src/main/java/com/xeiam/xchart/AxisTickLabels.java +++ b/src/main/java/com/xeiam/xchart/AxisTickLabels.java @@ -24,7 +24,6 @@ import java.awt.font.TextLayout; import com.xeiam.xchart.interfaces.IChartPart; - /** * Axis tick labels */ diff --git a/src/main/java/com/xeiam/xchart/AxisTickMarks.java b/src/main/java/com/xeiam/xchart/AxisTickMarks.java index 064932c81cdfc70470547439296fed13a21588c5..f21f78f7508fe1e48e184ea632a8c7a17ba3b46d 100644 --- a/src/main/java/com/xeiam/xchart/AxisTickMarks.java +++ b/src/main/java/com/xeiam/xchart/AxisTickMarks.java @@ -23,7 +23,6 @@ import java.awt.Stroke; import com.xeiam.xchart.interfaces.IChartPart; - /** * Axis tick marks. */ @@ -73,6 +72,8 @@ public class AxisTickMarks implements IChartPart { int xOffset = (int) (axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + AxisTick.AXIS_TICK_PADDING); int yOffset = (int) (axis.getPaintZone().getY()); + + // tick marks for (int i = 0; i < axisTick.getTickLabels().size(); i++) { int tickLocation = axisTick.getTickLocations().get(i); @@ -83,16 +84,20 @@ public class AxisTickMarks implements IChartPart { g.drawLine(xOffset, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation), xOffset + TICK_LENGTH, yOffset + (int) (axis.getPaintZone().getHeight() - tickLocation)); } + // Line + g.drawLine(xOffset + TICK_LENGTH, yOffset, xOffset + TICK_LENGTH, yOffset + (int) axis.getPaintZone().getHeight()); // bounds bounds = new Rectangle(xOffset, yOffset, TICK_LENGTH, (int) axis.getPaintZone().getHeight()); - // g.setColor(Color.blue); + // g.setColor(Color.yellow); // g.draw(bounds); } else { // X-Axis int xOffset = (int) (axis.getPaintZone().getX()); int yOffset = (int) (axisTick.getAxisTickLabels().getBounds().getY() - AxisTick.AXIS_TICK_PADDING); + + // tick marks for (int i = 0; i < axisTick.getTickLabels().size(); i++) { int tickLocation = axisTick.getTickLocations().get(i); @@ -102,12 +107,13 @@ public class AxisTickMarks implements IChartPart { g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - TICK_LENGTH); } + // Line + g.drawLine(xOffset, yOffset - TICK_LENGTH, xOffset + (int) axis.getPaintZone().getWidth(), yOffset - TICK_LENGTH); // bounds bounds = new Rectangle(xOffset, yOffset - TICK_LENGTH, (int) axis.getPaintZone().getWidth(), TICK_LENGTH); - // g.setColor(Color.blue); + // g.setColor(Color.yellow); // g.draw(bounds); } } - } diff --git a/src/main/java/com/xeiam/xchart/Chart.java b/src/main/java/com/xeiam/xchart/Chart.java index 315215ce8edf766bca603f3293b5723c95ac8193..0693f0f5685caa080a3f045c70673f2d75d1c7e6 100644 --- a/src/main/java/com/xeiam/xchart/Chart.java +++ b/src/main/java/com/xeiam/xchart/Chart.java @@ -15,10 +15,12 @@ */ package com.xeiam.xchart; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import com.xeiam.xchart.series.Series; import com.xeiam.xchart.series.SeriesColor; @@ -46,10 +48,24 @@ public class Chart { * @param pWidth * @param pHeight */ - public Chart(final int pWidth, final int pHeight) { + public Chart(int width, int height) { - width = pWidth; - height = pHeight; + this.width = width; + this.height = height; + } + + /** + * @param g + */ + public void paint(final Graphics2D g, int width, int height) { + + g.setColor(Color.white); + g.fillRect(0, 0, width, height); + + this.width = width; + this.height = height; + + paint(g); } /** @@ -114,6 +130,21 @@ public class Chart { // PUBLIC SETTERS + /** + * @param seriesName + * @param xData + * @param yData + */ + public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<Number> yData) { + + return axisPair.addSeries(seriesName, xData, yData, null); + } + + public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<Number> yData, Collection<Number> errorBars) { + + return axisPair.addSeries(seriesName, xData, yData, errorBars); + } + /** * Add series data as Collection<Number> * @@ -183,11 +214,6 @@ public class Chart { this.chartTitle.setText(title); } - public void setChartTitleVisible(boolean isVisible) { - - this.chartTitle.setVisible(isVisible); - } - public void setXAxisTitle(String title) { this.axisPair.getXAxis().setAxisTitle(title); @@ -198,14 +224,52 @@ public class Chart { this.axisPair.getYAxis().setAxisTitle(title); } + // ChartPart visibility //////////////////////////////// + + public void setChartTitleVisible(boolean isVisible) { + + this.chartTitle.setVisible(isVisible); + } + public void setAxisTitlesVisible(boolean isVisible) { this.axisPair.getXAxis().getAxisTitle().setVisible(isVisible); this.axisPair.getYAxis().getAxisTitle().setVisible(isVisible); } + public void setXAxisTitleVisible(boolean isVisible) { + + this.axisPair.getXAxis().getAxisTitle().setVisible(isVisible); + } + + public void setYAxisTitleVisible(boolean isVisible) { + + this.axisPair.getYAxis().getAxisTitle().setVisible(isVisible); + } + public void setChartLegendVisible(boolean isVisible) { this.chartLegend.setVisible(isVisible); } + + public void setAxisTicksVisible(boolean isVisible) { + + this.axisPair.getXAxis().getAxisTick().setVisible(isVisible); + this.axisPair.getYAxis().getAxisTick().setVisible(isVisible); + } + + public void setXAxisTicksVisible(boolean isVisible) { + + this.axisPair.getXAxis().getAxisTick().setVisible(isVisible); + } + + public void setYAxisTicksVisible(boolean isVisible) { + + this.axisPair.getYAxis().getAxisTick().setVisible(isVisible); + } + + public void setChartGridlinesVisible(boolean isVisible) { + + this.plot.getPlotSurface().setVisible(isVisible); + } } diff --git a/src/main/java/com/xeiam/xchart/ChartLegend.java b/src/main/java/com/xeiam/xchart/ChartLegend.java index cda6d9f2184bdbc923ff1b766b15f6866d716600..6e49361b6579261e7acb78189980ff7d900a55e6 100644 --- a/src/main/java/com/xeiam/xchart/ChartLegend.java +++ b/src/main/java/com/xeiam/xchart/ChartLegend.java @@ -27,7 +27,6 @@ import com.xeiam.xchart.interfaces.IHideable; import com.xeiam.xchart.series.Series; import com.xeiam.xchart.series.markers.Marker; - /** * @author timmolter */ @@ -37,7 +36,7 @@ public class ChartLegend implements IHideable { private Chart chart; /** the visibility state of legend */ - private boolean isVisible = true; // default to true + protected boolean isVisible = true; // default to true /** the font */ private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font @@ -104,7 +103,7 @@ public class ChartLegend implements IHideable { // Draw Legend Box int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING; int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING; - int xOffset = (chart.getWidth() - legendBoxWidth - Chart.CHART_PADDING); + int xOffset = chart.getWidth() - legendBoxWidth - Chart.CHART_PADDING; int yOffset = (int) ((chart.getHeight() - legendBoxHeight) / 2.0 + chart.getTitle().getBounds().getY() + chart.getTitle().getBounds().getHeight()); g.setColor(border); diff --git a/src/main/java/com/xeiam/xchart/ChartTitle.java b/src/main/java/com/xeiam/xchart/ChartTitle.java index aa11c5174570818ad70563ede50c465835d1faf6..415b5d407af48bf2fc5f553e4a6021bef31bf147 100644 --- a/src/main/java/com/xeiam/xchart/ChartTitle.java +++ b/src/main/java/com/xeiam/xchart/ChartTitle.java @@ -24,7 +24,6 @@ import java.awt.font.TextLayout; import com.xeiam.xchart.interfaces.IHideable; - /** * Chart Title */ @@ -75,21 +74,19 @@ public class ChartTitle implements IHideable { @Override public void paint(Graphics2D g) { - if (isVisible) { + FontRenderContext frc = g.getFontRenderContext(); + TextLayout textLayout = new TextLayout(this.text, this.font, frc); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + int xOffset = (int) ((chart.getWidth() - rectangle.getWidth()) / 2.0); + int yOffset = (int) ((isVisible ? (Chart.CHART_PADDING - rectangle.getY()) : 0)); - FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(this.text, this.font, frc); - Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); - // System.out.println(rectangle); - int xOffset = (int) ((chart.getWidth() - rectangle.getWidth()) / 2.0); - int yOffset = (int) (Chart.CHART_PADDING - rectangle.getY()); + bounds = new Rectangle(xOffset, yOffset + (isVisible ? (int) rectangle.getY() : 0), (int) rectangle.getWidth(), (int) (isVisible ? rectangle.getHeight() : 0)); + // g.setColor(Color.green); + // g.draw(bounds); + if (isVisible) { g.setColor(foreground); textLayout.draw(g, xOffset, yOffset); - - bounds = new Rectangle(xOffset, (int) (yOffset + rectangle.getY()), (int) rectangle.getWidth(), (int) rectangle.getHeight()); - // g.setColor(Color.green); - // g.draw(bounds); } } diff --git a/src/main/java/com/xeiam/xchart/Plot.java b/src/main/java/com/xeiam/xchart/Plot.java index b0908b9d2e55e9e1817889733167199d2abe3db9..d57d8ddc9e0f40a678517f7c3e8c27868a57da1a 100644 --- a/src/main/java/com/xeiam/xchart/Plot.java +++ b/src/main/java/com/xeiam/xchart/Plot.java @@ -31,7 +31,7 @@ public class Plot implements IChartPart { private PlotContent plotContent; - public static final int PLOT_PADDING = 5; + public static final int PLOT_PADDING = 3; /** the bounds */ private Rectangle bounds = new Rectangle(); // default all-zero rectangle @@ -53,7 +53,7 @@ public class Plot implements IChartPart { public void paint(Graphics2D g) { // calculate bounds - int xOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getX() + chart.getAxisPair().getYAxis().getBounds().getWidth() + PLOT_PADDING); + int xOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getX() + chart.getAxisPair().getYAxis().getBounds().getWidth() + (chart.getAxisPair().getYAxis().getAxisTick().isVisible ? (Plot.PLOT_PADDING + 1) : 0)); int yOffset = (int) (chart.getAxisPair().getYAxis().getBounds().getY()); int width = (int) chart.getAxisPair().getXAxis().getBounds().getWidth(); int height = (int) chart.getAxisPair().getYAxis().getBounds().getHeight(); @@ -66,4 +66,12 @@ public class Plot implements IChartPart { } + /** + * @return the plotSurface + */ + public PlotSurface getPlotSurface() { + + return plotSurface; + } + } diff --git a/src/main/java/com/xeiam/xchart/PlotContent.java b/src/main/java/com/xeiam/xchart/PlotContent.java index 12d12363ef0bd41ebedf1a269712a9f6f5e5b955..e1d7d7c75acdb065ee81da9b7866f8329a1eb46c 100644 --- a/src/main/java/com/xeiam/xchart/PlotContent.java +++ b/src/main/java/com/xeiam/xchart/PlotContent.java @@ -17,10 +17,13 @@ package com.xeiam.xchart; import java.awt.Graphics2D; import java.awt.Rectangle; +import java.math.BigDecimal; import java.util.Collection; +import java.util.Date; import java.util.Iterator; import java.util.Map; +import com.xeiam.xchart.Axis.AxisType; import com.xeiam.xchart.interfaces.IChartPart; import com.xeiam.xchart.series.Series; import com.xeiam.xchart.series.SeriesLineStyle; @@ -65,18 +68,18 @@ public class PlotContent implements IChartPart { int yTopMargin = AxisPair.getMargin((int) bounds.getHeight(), yTickSpace); // data points - Collection<Number> xData = series.getxData(); - double xMin = chart.getAxisPair().getXAxis().getMin(); - double xMax = chart.getAxisPair().getXAxis().getMax(); + Collection<?> xData = series.getxData(); + BigDecimal xMin = chart.getAxisPair().getXAxis().getMin(); + BigDecimal xMax = chart.getAxisPair().getXAxis().getMax(); Collection<Number> yData = series.getyData(); - double yMin = chart.getAxisPair().getYAxis().getMin(); - double yMax = chart.getAxisPair().getYAxis().getMax(); + BigDecimal yMin = chart.getAxisPair().getYAxis().getMin(); + BigDecimal yMax = chart.getAxisPair().getYAxis().getMax(); Collection<Number> errorBars = series.getErrorBars(); int previousX = Integer.MIN_VALUE; int previousY = Integer.MIN_VALUE; - Iterator<Number> xItr = xData.iterator(); + Iterator<?> xItr = xData.iterator(); Iterator<Number> yItr = yData.iterator(); Iterator<Number> ebItr = null; if (errorBars != null) { @@ -84,29 +87,42 @@ public class PlotContent implements IChartPart { } while (xItr.hasNext()) { - double x = xItr.next().doubleValue(); - double y = yItr.next().doubleValue(); + BigDecimal x = null; + if (chart.getAxisPair().getXAxis().getAxisType() == AxisType.NUMBER) { + x = new BigDecimal(((Number) xItr.next()).doubleValue()); + } + if (chart.getAxisPair().getXAxis().getAxisType() == AxisType.DATE) { + x = new BigDecimal(((Date) xItr.next()).getTime()); + System.out.println(x); + } + + BigDecimal y = new BigDecimal(yItr.next().doubleValue()); + // System.out.println(y); double eb = 0.0; if (errorBars != null) { eb = ebItr.next().doubleValue(); } - if (!Double.isNaN(x) && !Double.isNaN(y)) { + if (!Double.isNaN(x.doubleValue()) && !Double.isNaN(y.doubleValue())) { - int xTransform = (int) (xLeftMargin + ((x - xMin) / (xMax - xMin) * xTickSpace)); - int yTransform = (int) (bounds.getHeight() - (yTopMargin + (y - yMin) / (yMax - yMin) * yTickSpace)); + // int xTransform = (int) (xLeftMargin + ((x - xMin) / (xMax - xMin) * xTickSpace)); + int xTransform = (int) (xLeftMargin + (x.subtract(xMin).doubleValue() / xMax.subtract(xMin).doubleValue() * xTickSpace)); + // int yTransform = (int) (bounds.getHeight() - (yTopMargin + (y - yMin) / (yMax - yMin) * yTickSpace)); + int yTransform = (int) (bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace)); // a check if all y data are the exact same values - if (Math.abs(xMax - xMin) / 5 == 0.0) { + if (Math.abs(xMax.subtract(xMin).doubleValue()) / 5 == 0.0) { xTransform = (int) (bounds.getWidth() / 2.0); } // a check if all y data are the exact same values - if (Math.abs(yMax - yMin) / 5 == 0.0) { + if (Math.abs(yMax.subtract(yMin).doubleValue()) / 5 == 0.0) { yTransform = (int) (bounds.getHeight() / 2.0); } int xOffset = (int) (bounds.getX() + xTransform - 1); int yOffset = (int) (bounds.getY() + yTransform); + // System.out.println(yOffset); + // System.out.println(yTransform); // paint line if (series.getLineStyle() != null) { @@ -129,14 +145,17 @@ public class PlotContent implements IChartPart { if (errorBars != null) { g.setColor(ChartColor.getAWTColor(ChartColor.DARK_GREY)); g.setStroke(SeriesLineStyle.getBasicStroke(SeriesLineStyle.SOLID)); - int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax - yMin)); - int top = (int) (bounds.getHeight() * eb / (yMax - yMin)); + int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); + int top = (int) (bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); g.drawLine(xOffset, yOffset + bottom, xOffset, yOffset + top); g.drawLine(xOffset - 3, yOffset + bottom, xOffset + 3, yOffset + bottom); g.drawLine(xOffset - 3, yOffset + top, xOffset + 3, yOffset + top); } } } + } + } + } diff --git a/src/main/java/com/xeiam/xchart/PlotSurface.java b/src/main/java/com/xeiam/xchart/PlotSurface.java index a8f092d831d7ace8dfb1f909ea2e26ca4bbc7362..0b1ee3c11a290c619b3332a127ffe0dd87af06fc 100644 --- a/src/main/java/com/xeiam/xchart/PlotSurface.java +++ b/src/main/java/com/xeiam/xchart/PlotSurface.java @@ -22,12 +22,12 @@ import java.awt.Rectangle; import java.util.List; import com.xeiam.xchart.interfaces.IChartPart; - +import com.xeiam.xchart.interfaces.IHideable; /** * @author timmolter */ -public class PlotSurface implements IChartPart { +public class PlotSurface implements IChartPart, IHideable { private Chart chart; @@ -42,6 +42,15 @@ public class PlotSurface implements IChartPart { /** the line style */ private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f); + /** the visibility state of PlotSurface */ + protected boolean isVisible = true; // default to true + + /** + * Constructor + * + * @param chart + * @param plot + */ public PlotSurface(Chart chart, Plot plot) { this.chart = chart; @@ -68,28 +77,37 @@ public class PlotSurface implements IChartPart { g.draw(borderRectangle); // paint grid lines - // horizontal - List<Integer> yAxisTickLocations = chart.getAxisPair().getYAxis().getAxisTick().getTickLocations(); - for (int i = 0; i < yAxisTickLocations.size(); i++) { + if (isVisible) { + // horizontal + List<Integer> yAxisTickLocations = chart.getAxisPair().getYAxis().getAxisTick().getTickLocations(); + for (int i = 0; i < yAxisTickLocations.size(); i++) { - int tickLocation = yAxisTickLocations.get(i); + int tickLocation = yAxisTickLocations.get(i); - g.setColor(foreground); - g.setStroke(stroke); - // System.out.println("bounds.getY()= " + bounds.getY()); - g.drawLine((int) bounds.getX(), (int) (bounds.getY() + bounds.getHeight() - tickLocation), (int) (bounds.getX() + bounds.getWidth() - 2), (int) (bounds.getY() + bounds.getHeight() - tickLocation)); - } + g.setColor(foreground); + g.setStroke(stroke); + // System.out.println("bounds.getY()= " + bounds.getY()); + g.drawLine((int) bounds.getX(), (int) (bounds.getY() + bounds.getHeight() - tickLocation), (int) (bounds.getX() + bounds.getWidth() - 2), (int) (bounds.getY() + bounds.getHeight() - tickLocation)); + } - // vertical - List<Integer> xAxisTickLocations = chart.getAxisPair().getXAxis().getAxisTick().getTickLocations(); - for (int i = 0; i < xAxisTickLocations.size(); i++) { + // vertical + List<Integer> xAxisTickLocations = chart.getAxisPair().getXAxis().getAxisTick().getTickLocations(); + for (int i = 0; i < xAxisTickLocations.size(); i++) { - int tickLocation = xAxisTickLocations.get(i); + int tickLocation = xAxisTickLocations.get(i); - g.setColor(foreground); - g.setStroke(stroke); + g.setColor(foreground); + g.setStroke(stroke); - g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + 1), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1)); + g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + 1), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1)); + } } } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + + } } diff --git a/src/main/java/com/xeiam/xchart/interfaces/IHideable.java b/src/main/java/com/xeiam/xchart/interfaces/IHideable.java index 2f68d6d2d65be0990a42918ff9d5ad560788298a..77fc22e830049a55cfb37716d777816aa70373d8 100644 --- a/src/main/java/com/xeiam/xchart/interfaces/IHideable.java +++ b/src/main/java/com/xeiam/xchart/interfaces/IHideable.java @@ -13,6 +13,6 @@ package com.xeiam.xchart.interfaces; */ public interface IHideable extends IChartPart { - public void setVisible(boolean visible); + public void setVisible(boolean isVisible); } \ No newline at end of file diff --git a/src/main/java/com/xeiam/xchart/series/Series.java b/src/main/java/com/xeiam/xchart/series/Series.java index ac974c6b659ab6e6a4407a0b2000fe0a20e6c58e..287b6ff95e6c8b15cc2f58ee62087f5286c56452 100644 --- a/src/main/java/com/xeiam/xchart/series/Series.java +++ b/src/main/java/com/xeiam/xchart/series/Series.java @@ -17,9 +17,12 @@ package com.xeiam.xchart.series; import java.awt.BasicStroke; import java.awt.Color; +import java.math.BigDecimal; import java.util.Collection; +import java.util.Date; import java.util.Iterator; +import com.xeiam.xchart.Axis.AxisType; import com.xeiam.xchart.series.markers.Marker; /** @@ -29,23 +32,25 @@ public class Series { private String name = ""; - protected Collection<Number> xData; + // private AxisType seriesType; + + protected Collection<?> xData; protected Collection<Number> yData; protected Collection<Number> errorBars; /** the minimum value of axis range */ - private double xMin; + private BigDecimal xMin; /** the maximum value of axis range */ - private double xMax; + private BigDecimal xMax; /** the minimum value of axis range */ - private double yMin; + private BigDecimal yMin; /** the maximum value of axis range */ - private double yMax; + private BigDecimal yMax; /** Line Style */ private BasicStroke stroke; @@ -62,11 +67,12 @@ public class Series { /** * Constructor * + * @param <?> * @param name * @param xData * @param yData */ - public Series(String name, Collection<Number> xData, Collection<Number> yData, Collection<Number> errorBars) { + public Series(String name, Collection<?> xData, AxisType xAxisType, Collection<Number> yData, AxisType yAxisType, Collection<Number> errorBars) { this.name = name; this.xData = xData; @@ -74,28 +80,28 @@ public class Series { this.errorBars = errorBars; // xData - double[] xMinMax = findMinMax(xData); - this.xMin = xMinMax[0]; - this.xMax = xMinMax[1]; + BigDecimal[] xMinMax = findMinMax(xData, xAxisType); + xMin = xMinMax[0]; + xMax = xMinMax[1]; // yData - double[] yMinMax = null; + BigDecimal[] yMinMax = null; if (errorBars == null) { - yMinMax = findMinMax(yData); + yMinMax = findMinMax(yData, yAxisType); } else { yMinMax = findMinMaxWithErrorBars(yData, errorBars); } - this.yMin = yMinMax[0]; - this.yMax = yMinMax[1]; + yMin = yMinMax[0]; + yMax = yMinMax[1]; // System.out.println(yMin); // System.out.println(yMax); Color color = SeriesColor.getNextAWTColor(); - this.strokeColor = color; - this.markerColor = color; + strokeColor = color; + markerColor = color; - this.marker = SeriesMarker.getNextMarker(); - this.stroke = SeriesLineStyle.getNextBasicStroke(); + marker = SeriesMarker.getNextMarker(); + stroke = SeriesLineStyle.getNextBasicStroke(); } @@ -105,24 +111,39 @@ public class Series { * @param data * @return */ - private double[] findMinMax(Collection<Number> data) { - - Double min = null; - Double max = null; - for (Number number : data) { - verify(number.doubleValue()); - if (min == null || number.doubleValue() < min) { - if (!Double.isNaN(number.doubleValue())) { - min = number.doubleValue(); + private BigDecimal[] findMinMax(Collection<?> data, AxisType axisType) { + + BigDecimal min = null; + BigDecimal max = null; + + for (Object dataPoint : data) { + + BigDecimal bigDecimal = null; + + if (axisType == AxisType.NUMBER) { + bigDecimal = new BigDecimal(((Number) dataPoint).doubleValue()); + verify(bigDecimal); + + } else if (axisType == AxisType.DATE) { + Date date = (Date) dataPoint; + bigDecimal = new BigDecimal(date.getTime()); + verify(bigDecimal); + } + // if (min == null || bigDecimal < min) { + if (min == null || bigDecimal.compareTo(min) < 0) { + if (!Double.isNaN(bigDecimal.doubleValue())) { + min = bigDecimal; } } - if (max == null || number.doubleValue() > max) { - if (!Double.isNaN(number.doubleValue())) { - max = number.doubleValue(); + // if (max == null || bigDecimal > max) { + if (max == null || bigDecimal.compareTo(max) > 0) { + if (!Double.isNaN(bigDecimal.doubleValue())) { + max = bigDecimal; } } } - return new double[] { min, max }; + + return new BigDecimal[] { min, max }; } /** @@ -131,29 +152,29 @@ public class Series { * @param data * @return */ - private double[] findMinMaxWithErrorBars(Collection<Number> data, Collection<Number> errorBars) { + private BigDecimal[] findMinMaxWithErrorBars(Collection<Number> data, Collection<Number> errorBars) { - Double min = null; - Double max = null; + BigDecimal min = null; + BigDecimal max = null; Iterator<Number> itr = data.iterator(); Iterator<Number> ebItr = errorBars.iterator(); while (itr.hasNext()) { - double number = itr.next().doubleValue(); - double eb = ebItr.next().doubleValue(); - verify(number); - if (min == null || (number - eb) < min) { - if (!Double.isNaN(number)) { - min = number - eb; + BigDecimal bigDecimal = new BigDecimal(itr.next().doubleValue()); + BigDecimal eb = new BigDecimal(ebItr.next().doubleValue()); + verify(bigDecimal); + if (min == null || (bigDecimal.subtract(eb)).compareTo(min) < 0) { + if (!Double.isNaN(bigDecimal.doubleValue())) { + min = bigDecimal.subtract(eb); } } - if (max == null || (number + eb) > max) { - if (!Double.isNaN(number)) { - max = number + eb; + if (max == null || (bigDecimal.add(eb)).compareTo(max) > 0) { + if (!Double.isNaN(bigDecimal.doubleValue())) { + max = bigDecimal.add(eb); } } } - return new double[] { min, max }; + return new BigDecimal[] { min, max }; } /** @@ -161,13 +182,23 @@ public class Series { * * @param data */ - private void verify(double value) { + private void verify(BigDecimal value) { - if (value == Double.POSITIVE_INFINITY) { + // TODO get rid of this if not a Number axis type + double doubleValue = value.doubleValue(); + if (doubleValue == Double.POSITIVE_INFINITY) { throw new RuntimeException("Axis data cannot contain Double.POSITIVE_INFINITY!!!"); - } else if (value == Double.NEGATIVE_INFINITY) { + } else if (doubleValue == Double.NEGATIVE_INFINITY) { throw new RuntimeException("Axis data cannot contain Double.NEGATIVE_INFINITY!!!"); } + // TODO get rid of this if not a Date axis type + long longValue = value.longValue(); + if (longValue == Long.MAX_VALUE) { + throw new RuntimeException("Axis data cannot be greater than Long.MAX_VALUE!!!"); + } else if (longValue == Long.MIN_VALUE) { + throw new RuntimeException("Axis data cannot be less than Long.MIN_VALUE!!!"); + } + } public String getName() { @@ -175,7 +206,7 @@ public class Series { return name; } - public Collection<Number> getxData() { + public Collection<?> getxData() { return xData; } @@ -190,22 +221,22 @@ public class Series { return errorBars; } - public double getxMin() { + public BigDecimal getxMin() { return xMin; } - public double getxMax() { + public BigDecimal getxMax() { return xMax; } - public double getyMin() { + public BigDecimal getyMin() { return yMin; } - public double getyMax() { + public BigDecimal getyMax() { return yMax; } @@ -217,12 +248,12 @@ public class Series { public void setLineStyle(SeriesLineStyle lineStyle) { - this.stroke = SeriesLineStyle.getBasicStroke(lineStyle); + stroke = SeriesLineStyle.getBasicStroke(lineStyle); } public void setLineStyle(BasicStroke lineStyle) { - this.stroke = lineStyle; + stroke = lineStyle; } public Color getLineColor() { @@ -232,12 +263,12 @@ public class Series { public void setLineColor(SeriesColor lineColor) { - this.strokeColor = SeriesColor.getAWTColor(lineColor); + strokeColor = SeriesColor.getAWTColor(lineColor); } public void setLineColor(java.awt.Color lineColor) { - this.strokeColor = lineColor; + strokeColor = lineColor; } public Marker getMarker() { diff --git a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java index f571d3dd60c66434319e3a9bbdf16710f5aac23b..6696234bbe8c77f45281e963cf64ef9e80046788 100644 --- a/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java +++ b/src/main/java/com/xeiam/xchart/swing/SwingWrapper.java @@ -15,14 +15,10 @@ */ package com.xeiam.xchart.swing; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Graphics2D; import java.awt.GridLayout; import java.util.ArrayList; import java.util.List; -import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; @@ -108,10 +104,10 @@ public class SwingWrapper { // Create and set up the window. JFrame frame = new JFrame("XChart"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); - - JPanel chartPanel = new ChartJPanel(charts.get(0)); - frame.getContentPane().add(chartPanel); + // frame.setSize(new Dimension(charts.get(0).getWidth(), charts.get(0).getHeight())); + JPanel chartPanel = new XChartJPanel(charts.get(0)); + // frame.getContentPane().add(chartPanel); + frame.add(chartPanel); // Display the window. frame.pack(); @@ -139,7 +135,7 @@ public class SwingWrapper { for (Chart chart : charts) { if (chart != null) { - JPanel chartPanel = new ChartJPanel(chart); + JPanel chartPanel = new XChartJPanel(chart); frame.getContentPane().add(chartPanel); } else { JPanel chartPanel = new JPanel(); @@ -155,25 +151,4 @@ public class SwingWrapper { }); } - private class ChartJPanel extends JPanel { - - private Chart chart; - - public ChartJPanel(Chart chart) { - - this.chart = chart; - } - - @Override - public void paint(Graphics g) { - - chart.paint((Graphics2D) g); - } - - @Override - public Dimension getPreferredSize() { - - return new Dimension(chart.getWidth(), chart.getHeight()); - } - } } diff --git a/src/main/java/com/xeiam/xchart/swing/XChartJPanel.java b/src/main/java/com/xeiam/xchart/swing/XChartJPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..cd74d039fdb829069505f8f691599a5832775693 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/swing/XChartJPanel.java @@ -0,0 +1,55 @@ +/** + * Copyright 2012 Xeiam LLC. + * + * 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 com.xeiam.xchart.swing; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; + +import javax.swing.JPanel; + +import com.xeiam.xchart.Chart; + +/** + * @author timmolter + * @create Sep 9, 2012 + */ +public class XChartJPanel extends JPanel { + + private Chart chart; + + public XChartJPanel(Chart chart) { + + this.chart = chart; + } + + @Override + protected void paintComponent(Graphics g) { + + super.paintComponent(g); + + super.removeAll(); + System.out.println(getSize().toString()); + + chart.paint((Graphics2D) g, getSize().width, getSize().height); + } + + @Override + public Dimension getPreferredSize() { + + return new Dimension(chart.getWidth(), chart.getHeight()); + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example10.java b/src/test/java/com/xeiam/xchart/example/Example10.java new file mode 100644 index 0000000000000000000000000000000000000000..af8e41506bb88846d0696c7dd943f0948cfa0f75 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example10.java @@ -0,0 +1,54 @@ +/** + * Copyright 2011-2012 Xeiam LLC. + * + * 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 com.xeiam.xchart.example; + +import java.util.Arrays; +import java.util.Collection; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Creates a simple charts using Longs as inputs + * + * @author timmolter + */ +public class Example10 { + + public static void main(String[] args) throws Exception { + + Collection<Number> xData = Arrays + .asList(new Number[] { 1222812000000L, 1222898400000L, 1222984800000L, 1223071200000L, 1223157600000L, 1223244000000L, 1223330400000L, 1223416800000L, 1223503200000L, 1223589600000L }); + // .asList(new Number[] { 12228120L, 12228984L, 12229848L, 12230712L, 12231576L, 12232440L, 12233304L, 12234168L, 12235032L, 12235896L }); + // .asList(new Number[] { 12228120, 12228984, 12229848, 12230712, 12231576, 12232440, 12233304, 12234168, 12235032, 12235896 }); + // .asList(new Number[] { 12228120.0, 12228984.0, 12229848.0, 12230712.0, 12231576.0, 12232440.0, 12233304.0, 12234168.0, 12235032.0, 12235896.0 }); + // .asList(new Number[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }); + // .asList(new Number[] { 0.0, 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0 }); + // .asList(new Number[] { 0.0, 100000000.0, 200000000.0, 300000000.0, 400000000.0, 500000000.0, 600000000.0, 700000000.0, 800000000.0, 900000000.0 }); + Collection<Number> yData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0 }); + + // Create Chart + Chart chart = new Chart(700, 500); + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + chart.addSeries("y(x)", xData, yData); + + new SwingWrapper(chart).displayChart(); + + } + +} diff --git a/src/test/java/com/xeiam/xchart/example/Example3.java b/src/test/java/com/xeiam/xchart/example/Example3.java index a70b68ec6134fc73ec6092f5aaad5d4ba57e79a1..acf9f2c40e8a7eb6a57642dc6c1a3d37f3b83e25 100644 --- a/src/test/java/com/xeiam/xchart/example/Example3.java +++ b/src/test/java/com/xeiam/xchart/example/Example3.java @@ -48,6 +48,13 @@ public class Example3 { chart.setChartTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); + // chart.setAxisTicksVisible(false); + // chart.setAxisTitlesVisible(false); + // chart.setXAxisTitleVisible(false); + // chart.setXAxisTicksVisible(false); + chart.setChartTitleVisible(false); + chart.setChartLegendVisible(false); + chart.setChartGridlinesVisible(false); String seriesName = "y=" + 2 * i + "x-" + i * b + "b"; chart.addSeries(seriesName, xData, yData); @@ -56,5 +63,4 @@ public class Example3 { new SwingWrapper(chart).displayChart(); } - } diff --git a/src/test/java/com/xeiam/xchart/example/Example9.java b/src/test/java/com/xeiam/xchart/example/Example9.java new file mode 100644 index 0000000000000000000000000000000000000000..64260399a66569edea435b375fc2f1f554090eb4 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example9.java @@ -0,0 +1,61 @@ +/** + * Copyright 2011-2012 Xeiam LLC. + * + * 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 com.xeiam.xchart.example; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Create a chart with a Date x-axis + * + * @author timmolter + */ +public class Example9 { + + public static void main(String[] args) throws ParseException { + + // Create Chart + Chart chart = new Chart(700, 500); + + // generates linear data + Collection<Date> xData = new ArrayList<Date>(); + Collection<Number> yData = new ArrayList<Number>(); + + DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); + for (int i = 1; i <= 10; i++) { + Date date = sdf.parse(i + ".10.2008"); + xData.add(date); + yData.add(Math.random() * i); + } + + // Customize Chart + chart.setChartTitle("Sample Chart with Date X-Axis"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + + chart.addDateSeries("Fake Data", xData, yData); + + new SwingWrapper(chart).displayChart(); + } + +}