diff --git a/pom.xml b/pom.xml index c40090c5dcf60f822c54e694ab6c0ce9e4143891..e8d0dd0861ab16602bc65a318f3ec73a7355fe4c 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,9 @@ </goals> </execution> </executions> + <configuration> + <excludePackageNames>com.xeiam.xchart.internal.*</excludePackageNames> + </configuration> </plugin> </plugins> </build> diff --git a/src/main/java/com/xeiam/xchart/Chart.java b/src/main/java/com/xeiam/xchart/Chart.java index 580105ef656747d92a7fa9437827df5564ca89f7..3f3433773e2b81ac8448c082ed6ed54a2c4b981d 100644 --- a/src/main/java/com/xeiam/xchart/Chart.java +++ b/src/main/java/com/xeiam/xchart/Chart.java @@ -24,27 +24,29 @@ import java.util.Collection; import java.util.Date; import java.util.Locale; -import com.xeiam.xchart.series.Series; -import com.xeiam.xchart.series.SeriesColor; -import com.xeiam.xchart.series.SeriesLineStyle; -import com.xeiam.xchart.series.SeriesMarker; +import com.xeiam.xchart.internal.chartpart.AxisPair; +import com.xeiam.xchart.internal.chartpart.ChartTitle; +import com.xeiam.xchart.internal.chartpart.Legend; +import com.xeiam.xchart.internal.chartpart.Plot; /** + * An XChart Chart + * * @author timmolter */ public class Chart { - protected int width; - protected int height; + public int width; + public int height; private Color backgroundColor; - protected Color bordersColor; - protected Color fontColor; + public Color bordersColor; + public Color fontColor; - protected final static int CHART_PADDING = 10; + public final static int CHART_PADDING = 10; - protected ChartTitle chartTitle = new ChartTitle(this); - protected Legend chartLegend = new Legend(this); - protected AxisPair axisPair = new AxisPair(this); + public ChartTitle chartTitle = new ChartTitle(this); + public Legend chartLegend = new Legend(this); + public AxisPair axisPair = new AxisPair(this); protected Plot plot = new Plot(this); /** diff --git a/src/main/java/com/xeiam/xchart/ChartColor.java b/src/main/java/com/xeiam/xchart/ChartColor.java index b8c1f419872e29e29c5988f92b5c5b1fe2b5e8f6..9735a7f0ecf5863237454512064d930dfbe8492d 100644 --- a/src/main/java/com/xeiam/xchart/ChartColor.java +++ b/src/main/java/com/xeiam/xchart/ChartColor.java @@ -18,6 +18,8 @@ package com.xeiam.xchart; import java.awt.Color; /** + * Pre-defined Colors used for various Chart Elements + * * @author timmolter */ public enum ChartColor { diff --git a/src/main/java/com/xeiam/xchart/QuickChart.java b/src/main/java/com/xeiam/xchart/QuickChart.java index 97522a41c305d8b87ac914d49328c8c41e38a19a..a7ace066eb20a17d73b9fc65c8ec806c0f7ad4b1 100644 --- a/src/main/java/com/xeiam/xchart/QuickChart.java +++ b/src/main/java/com/xeiam/xchart/QuickChart.java @@ -15,11 +15,8 @@ */ package com.xeiam.xchart; -import com.xeiam.xchart.series.Series; -import com.xeiam.xchart.series.SeriesMarker; - /** - * A convenience class for making Charts with one line of code. + * A convenience class for making Charts with one line of code * * @author timmolter */ diff --git a/src/main/java/com/xeiam/xchart/Series.java b/src/main/java/com/xeiam/xchart/Series.java new file mode 100644 index 0000000000000000000000000000000000000000..fc1980c2f1dd62eadb3111bde2c7b48083be4f9b --- /dev/null +++ b/src/main/java/com/xeiam/xchart/Series.java @@ -0,0 +1,204 @@ +/** + * 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.math.BigDecimal; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; + +import com.xeiam.xchart.internal.chartpart.Axis.AxisType; +import com.xeiam.xchart.internal.markers.Marker; + +/** + * A Series containing X and Y data to be plotted on a Chart + * + * @author timmolter + */ +public class Series { + + public String name = ""; + + public Collection<?> xData; + + public Collection<Number> yData; + + public Collection<Number> errorBars; + + /** the minimum value of axis range */ + public BigDecimal xMin; + + /** the maximum value of axis range */ + public BigDecimal xMax; + + /** the minimum value of axis range */ + public BigDecimal yMin; + + /** the maximum value of axis range */ + public BigDecimal yMax; + + /** Line Style */ + public BasicStroke stroke; + + /** Line Color */ + public Color strokeColor; + + /** Marker Style */ + public Marker marker; + + /** Marker Color */ + public Color markerColor; + + /** + * Constructor + * + * @param name + * @param xData + * @param xAxisType + * @param yData + * @param yAxisType + * @param errorBars + */ + public Series(String name, Collection<?> xData, AxisType xAxisType, Collection<Number> yData, AxisType yAxisType, Collection<Number> errorBars) { + + this.name = name; + this.xData = xData; + this.yData = yData; + this.errorBars = errorBars; + + // xData + BigDecimal[] xMinMax = findMinMax(xData, xAxisType); + xMin = xMinMax[0]; + xMax = xMinMax[1]; + + // yData + BigDecimal[] yMinMax = null; + if (errorBars == null) { + yMinMax = findMinMax(yData, yAxisType); + } else { + yMinMax = findMinMaxWithErrorBars(yData, errorBars); + } + yMin = yMinMax[0]; + yMax = yMinMax[1]; + // System.out.println(yMin); + // System.out.println(yMax); + + Color color = SeriesColor.getNextAWTColor(); + strokeColor = color; + markerColor = color; + + marker = SeriesMarker.getNextMarker(); + stroke = SeriesLineStyle.getNextBasicStroke(); + + } + + /** + * Finds the min and max of a dataset + * + * @param data + * @return + */ + 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).toString()); + + } else if (axisType == AxisType.DATE) { + Date date = (Date) dataPoint; + bigDecimal = new BigDecimal(date.getTime()); + } + if (min == null || bigDecimal.compareTo(min) < 0) { + min = bigDecimal; + } + if (max == null || bigDecimal.compareTo(max) > 0) { + max = bigDecimal; + } + } + + return new BigDecimal[] { min, max }; + } + + /** + * Finds the min and max of a dataset accounting for error bars + * + * @param data + * @return + */ + private BigDecimal[] findMinMaxWithErrorBars(Collection<Number> data, Collection<Number> errorBars) { + + BigDecimal min = null; + BigDecimal max = null; + + Iterator<Number> itr = data.iterator(); + Iterator<Number> ebItr = errorBars.iterator(); + while (itr.hasNext()) { + BigDecimal bigDecimal = new BigDecimal(itr.next().doubleValue()); + BigDecimal eb = new BigDecimal(ebItr.next().doubleValue()); + if (min == null || (bigDecimal.subtract(eb)).compareTo(min) < 0) { + min = bigDecimal.subtract(eb); + } + if (max == null || (bigDecimal.add(eb)).compareTo(max) > 0) { + max = bigDecimal.add(eb); + } + } + return new BigDecimal[] { min, max }; + } + + public void setLineStyle(SeriesLineStyle lineStyle) { + + stroke = SeriesLineStyle.getBasicStroke(lineStyle); + } + + public void setLineStyle(BasicStroke lineStyle) { + + stroke = lineStyle; + } + + public void setLineColor(SeriesColor lineColor) { + + strokeColor = SeriesColor.getAWTColor(lineColor); + } + + public void setLineColor(java.awt.Color lineColor) { + + strokeColor = lineColor; + } + + public void setMarker(SeriesMarker marker) { + + this.marker = SeriesMarker.getMarker(marker); + } + + public void setMarkerColor(SeriesColor lineColor) { + + this.markerColor = SeriesColor.getAWTColor(lineColor); + } + + public void setMarkerColor(java.awt.Color lineColor) { + + this.markerColor = lineColor; + } + +} diff --git a/src/main/java/com/xeiam/xchart/SeriesColor.java b/src/main/java/com/xeiam/xchart/SeriesColor.java new file mode 100644 index 0000000000000000000000000000000000000000..605c2275075263794fe49735eaf495cd289aff4b --- /dev/null +++ b/src/main/java/com/xeiam/xchart/SeriesColor.java @@ -0,0 +1,118 @@ +/** + * 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.Color; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +/** + * Pre-defined Colors used for Series Lines and Markers + * + * @author timmolter + */ +public enum SeriesColor { + + /** BLUE */ + BLUE(0, new Color(0, 55, 255)), + + /** ORANGE */ + ORANGE(1, new Color(255, 172, 0)), + + /** PURPLE */ + PURPLE(2, new Color(128, 0, 255)), + + /** GREEN */ + GREEN(3, new Color(0, 205, 0)), + + /** RED */ + RED(4, new Color(205, 0, 0)), + + /** YELLOW */ + YELLOW(5, new Color(255, 215, 0)), + + /** MAGENTA */ + MAGENTA(6, new Color(255, 0, 255)), + + /** PINK */ + PINK(7, new Color(255, 166, 201)), + + /** LIGHT_GREY */ + LIGHT_GREY(8, new Color(207, 207, 207)), + + /** CYAN */ + CYAN(9, new Color(0, 255, 255)), + + /** BROWN */ + BROWN(10, new Color(150, 74, 0)), + + /** BLACK */ + BLACK(11, new Color(0, 0, 0)), + + /** RANDOM */ + RANDOM(12, new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); + + int id; + Color color; + + private static int nextId = 0; + + private static final Map<Integer, SeriesColor> idLookup = new HashMap<Integer, SeriesColor>(); + static { + for (SeriesColor seriesColor : EnumSet.allOf(SeriesColor.class)) { + idLookup.put(seriesColor.getId(), seriesColor); + } + } + + private Integer getId() { + + return id; + } + + protected static void resetId() { + + nextId = 0; + } + + protected static Color getAWTColor(SeriesColor seriesColor) { + + return seriesColor.color; + } + + protected static Color getNextAWTColor() { + + SeriesColor seriesColor = idLookup.get(nextId); + if (seriesColor == null) { + // rotate thru from beginning + resetId(); + } + return idLookup.get(nextId++).color; + } + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesColor(int id, Color color) { + + this.id = id; + this.color = color; + } + +} diff --git a/src/main/java/com/xeiam/xchart/SeriesLineStyle.java b/src/main/java/com/xeiam/xchart/SeriesLineStyle.java new file mode 100644 index 0000000000000000000000000000000000000000..55fe62a75bce9e5dafe2da185d05602c39d314a6 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/SeriesLineStyle.java @@ -0,0 +1,106 @@ +/** + * 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.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +/** + * Pre-defined Line Styles used for Series Lines + * + * @author timmolter + */ +public enum SeriesLineStyle { + + /** NONE */ + NONE(-1, null), + + /** SOLID */ + SOLID(0, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)), + + /** DASH_DOT */ + DASH_DOT(1, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 1.0f }, 0.0f)), + + /** DASH_DASH */ + DASH_DASH(2, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f)), + + /** DOT_DOT */ + DOT_DOT(3, new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 1.0f, 1.0f }, 0.0f)); + + int id; + + BasicStroke basicStroke; + + private static int nextId = 0; + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesLineStyle(int id, BasicStroke basicStroke) { + + this.id = id; + this.basicStroke = basicStroke; + } + + private static final Map<Integer, SeriesLineStyle> idLookup = new HashMap<Integer, SeriesLineStyle>(); + static { + for (SeriesLineStyle seriesLineStyle : EnumSet.allOf(SeriesLineStyle.class)) { + idLookup.put(seriesLineStyle.getId(), seriesLineStyle); + } + } + + private Integer getId() { + + return id; + } + + protected static void resetId() { + + nextId = 0; + } + + /** + * Get an AWT Stroke + * + * @param seriesMarker + * @return + */ + protected static BasicStroke getBasicStroke(SeriesLineStyle seriesMarker) { + + return seriesMarker.basicStroke; + } + + /** + * Gets the next Stroke + * + * @return + */ + protected static BasicStroke getNextBasicStroke() { + + SeriesLineStyle seriesLineStyle = idLookup.get(nextId); + if (seriesLineStyle == null) { + // rotate thru from beginning + resetId(); + } + return idLookup.get(nextId++).basicStroke; + } + +} diff --git a/src/main/java/com/xeiam/xchart/SeriesMarker.java b/src/main/java/com/xeiam/xchart/SeriesMarker.java new file mode 100644 index 0000000000000000000000000000000000000000..69edc8ee6d9df2f780df3ceb89b107a150355bbf --- /dev/null +++ b/src/main/java/com/xeiam/xchart/SeriesMarker.java @@ -0,0 +1,102 @@ +/** + * 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.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +import com.xeiam.xchart.internal.markers.Circle; +import com.xeiam.xchart.internal.markers.Diamond; +import com.xeiam.xchart.internal.markers.Marker; +import com.xeiam.xchart.internal.markers.Square; +import com.xeiam.xchart.internal.markers.TriangleDown; +import com.xeiam.xchart.internal.markers.TriangleUp; + +/** + * Pre-defined Markers used for Series Lines + * + * @author timmolter + */ +public enum SeriesMarker { + + /** NONE */ + NONE(-1, null), + + /** CIRCLE */ + CIRCLE(0, new Circle()), + + /** DIAMOND */ + DIAMOND(1, new Diamond()), + + /** SQUARE */ + SQUARE(2, new Square()), + + /** TRIANGLE_DOWN */ + TRIANGLE_DOWN(3, new TriangleDown()), + + /** TRIANGLE_UP */ + TRIANGLE_UP(4, new TriangleUp()); + + int id; + Marker marker; + private static int nextId = 0; + + private static final Map<Integer, SeriesMarker> idLookup = new HashMap<Integer, SeriesMarker>(); + static { + for (SeriesMarker seriesMarker : EnumSet.allOf(SeriesMarker.class)) { + idLookup.put(seriesMarker.getId(), seriesMarker); + } + } + + private Integer getId() { + + return id; + } + + protected static void resetId() { + + nextId = 0; + } + + protected static Marker getMarker(SeriesMarker seriesMarker) { + + return seriesMarker.marker; + } + + protected static Marker getNextMarker() { + + SeriesMarker seriesMarker = idLookup.get(nextId); + if (seriesMarker == null) { + // rotate thru from beginning + resetId(); + } + return idLookup.get(nextId++).marker; + } + + /** + * Constructor + * + * @param id + * @param color + */ + private SeriesMarker(int id, Marker marker) { + + this.id = id; + this.marker = marker; + } + +} diff --git a/src/main/java/com/xeiam/xchart/ServletEncoder.java b/src/main/java/com/xeiam/xchart/ServletEncoder.java index d4d6e81067d2557e126976a063d5fade2dc9c6ba..d5bee0c23254cfd5e82160efc0c0686b5ac2faf4 100644 --- a/src/main/java/com/xeiam/xchart/ServletEncoder.java +++ b/src/main/java/com/xeiam/xchart/ServletEncoder.java @@ -23,6 +23,8 @@ import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; /** + * A helper class to be used in conjuction with a Servlet for streaming a Chart to an HTTP client + * * @author timmolter */ public class ServletEncoder { diff --git a/src/main/java/com/xeiam/xchart/SwingWrapper.java b/src/main/java/com/xeiam/xchart/SwingWrapper.java index 4e109e04edfbc20abf4758b146e8efa650ae8c8d..7805f879587c77babcf207e44f05c1189e1fd95c 100644 --- a/src/main/java/com/xeiam/xchart/SwingWrapper.java +++ b/src/main/java/com/xeiam/xchart/SwingWrapper.java @@ -23,6 +23,8 @@ import javax.swing.JFrame; import javax.swing.JPanel; /** + * A convenience class used to display a Chart in a barebones Swing application + * * @author timmolter */ public class SwingWrapper { @@ -83,7 +85,7 @@ public class SwingWrapper { // Create and set up the window. JFrame frame = new JFrame("XChart"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - JPanel chartPanel = new XChartJPanel(charts.get(0)); + JPanel chartPanel = new XChartPanel(charts.get(0)); frame.add(chartPanel); // Display the window. @@ -112,7 +114,7 @@ public class SwingWrapper { for (Chart chart : charts) { if (chart != null) { - JPanel chartPanel = new XChartJPanel(chart); + JPanel chartPanel = new XChartPanel(chart); frame.add(chartPanel); } else { JPanel chartPanel = new JPanel(); diff --git a/src/main/java/com/xeiam/xchart/XChartPanel.java b/src/main/java/com/xeiam/xchart/XChartPanel.java new file mode 100644 index 0000000000000000000000000000000000000000..c4842a5741ab8c05ac8bfda3e501a669eaf4bf7e --- /dev/null +++ b/src/main/java/com/xeiam/xchart/XChartPanel.java @@ -0,0 +1,58 @@ +/** + * 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; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; + +import javax.swing.JPanel; + +/** + * A Swing JPanel that contains a Chart + * + * @author timmolter + * @create Sep 9, 2012 + */ +public class XChartPanel extends JPanel { + + private final Chart chart; + + /** + * Constructor + * + * @param chart + */ + public XChartPanel(final Chart chart) { + + this.chart = chart; + + } + + @Override + protected void paintComponent(Graphics g) { + + super.paintComponent(g); + + chart.paint((Graphics2D) g, getWidth(), getHeight()); + } + + @Override + public Dimension getPreferredSize() { + + return new Dimension(chart.width, chart.height); + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java new file mode 100644 index 0000000000000000000000000000000000000000..731049cd26a825191271c49247f451815baf0bf5 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java @@ -0,0 +1,235 @@ +/** + * 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.internal.chartpart; + +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.Chart; +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * Axis + * + * @author timmolter + */ +public class Axis implements IChartPart { + + public enum AxisType { + + NUMBER, DATE; + } + + /** parent */ + protected AxisPair axisPair; + + /** the axisType */ + protected AxisType axisType; + + /** the axis title */ + public AxisTitle axisTitle; + + /** the axis tick */ + public AxisTick axisTick; + + /** the axis direction */ + protected Direction direction; + + protected BigDecimal min = null; + + protected BigDecimal max = null; + + /** the bounds */ + private Rectangle bounds; + + /** the paint zone */ + private Rectangle paintZone; + + /** An axis direction */ + protected enum Direction { + + /** the constant to represent X axis */ + X, + + /** the constant to represent Y axis */ + Y + } + + /** + * Constructor + * + * @param direction the axis direction (X or Y) + * @param chart the chart + */ + protected Axis(AxisPair axisPair, Direction direction) { + + this.axisPair = axisPair; + this.direction = direction; + + axisTitle = new AxisTitle(this); + axisTick = new AxisTick(this); + } + + /** + * @param min + * @param max + */ + protected void addMinMax(BigDecimal min, BigDecimal max) { + + // System.out.println(min); + // System.out.println(max); + if (this.min == null || min.compareTo(this.min) < 0) { + this.min = min; + } + if (this.max == null || max.compareTo(this.max) > 0) { + this.max = max; + } + + // System.out.println(this.min); + // System.out.println(this.max); + } + + protected 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; + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + protected Rectangle getPaintZone() { + + return paintZone; + } + + public AxisTitle getAxisTitle() { + + return axisTitle; + } + + public void setAxisTitle(String title) { + + this.axisTitle.setText(title); + } + + protected void setAxisTitle(AxisTitle axisTitle) { + + this.axisTitle = axisTitle; + } + + /** + * @return + */ + protected int getSizeHint() { + + if (direction == Direction.X) { // X-Axis + + // Axis title + 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); + titleHeight = rectangle.getHeight() + AxisTitle.AXIS_TITLE_PADDING; + } + + // Axis tick labels + double axisTickLabelsHeight = 0.0; + if (axisTick.isVisible) { + TextLayout textLayout = new TextLayout("0", axisTick.axisTickLabels.font, 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 + } + } + + @Override + public void paint(Graphics2D g) { + + paintZone = new Rectangle(); + bounds = new Rectangle(); + + // determine Axis bounds + if (direction == Direction.Y) { // Y-Axis + + // calculate paint zone + // ---- + // | + // | + // | + // | + // ---- + int xOffset = Chart.CHART_PADDING; + int yOffset = (int) (axisPair.getChartTitleBounds().getY() + axisPair.getChartTitleBounds().getHeight() + Chart.CHART_PADDING); + int width = 80; // arbitrary, final width depends on Axis tick labels + int height = axisPair.chart.height - yOffset - axisPair.xAxis.getSizeHint() - Chart.CHART_PADDING; + Rectangle yAxisRectangle = new Rectangle(xOffset, yOffset, width, height); + this.paintZone = yAxisRectangle; + // g.setColor(Color.green); + // g.draw(yAxisRectangle); + + // fill in Axis with sub-components + axisTitle.paint(g); + axisTick.paint(g); + + xOffset = (int) paintZone.getX(); + yOffset = (int) paintZone.getY(); + 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); + // g.draw(bounds); + + } else { // X-Axis + + // calculate paint zone + // |____________________| + + int xOffset = (int) (axisPair.yAxis.getBounds().getWidth() + (axisPair.yAxis.axisTick.isVisible ? Plot.PLOT_PADDING : 0) + Chart.CHART_PADDING); + int yOffset = (int) (axisPair.yAxis.getBounds().getY() + axisPair.yAxis.getBounds().getHeight()); + int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.chartLegend.isVisible ? 3 : 2) * Chart.CHART_PADDING); + int height = this.getSizeHint(); + Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); + this.paintZone = xAxisRectangle; + // g.setColor(Color.green); + // g.draw(xAxisRectangle); + + axisTitle.paint(g); + axisTick.paint(g); + + xOffset = (int) paintZone.getX(); + yOffset = (int) paintZone.getY(); + 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); + // g.draw(bounds); + } + + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java new file mode 100644 index 0000000000000000000000000000000000000000..6ec16346c6178eaab2e5ab5f1672bd7118f5c600 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java @@ -0,0 +1,156 @@ +/** + * 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.internal.chartpart; + +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.Chart; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.internal.chartpart.Axis.AxisType; +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * @author timmolter + */ +public class AxisPair implements IChartPart { + + /** parent */ + protected Chart chart; + + public Map<Integer, Series> seriesMap = new LinkedHashMap<Integer, Series>(); + + private int seriesCount; + + public Axis xAxis; + public Axis yAxis; + + /** + * Constructor. + * + * @param chart the chart + */ + public AxisPair(Chart chart) { + + this.chart = chart; + seriesCount = 0; + + // add axes + xAxis = new Axis(this, Axis.Direction.X); + yAxis = new Axis(this, Axis.Direction.Y); + } + + /** + * @param <T> + * @param xData + * @param yData + */ + public <T> Series addSeries(String seriesName, Collection<T> xData, Collection<Number> yData, Collection<Number> errorBars) { + + // Sanity checks + if (seriesName == null) { + throw new IllegalArgumentException("Series Name cannot be null!!!"); + } + if (yData == null) { + throw new IllegalArgumentException("Y-Axis data cannot be null!!!"); + } + if (yData.size() == 0) { + throw new IllegalArgumentException("Y-Axis data cannot be empty!!!"); + } + if (xData != null && xData.size() == 0) { + throw new IllegalArgumentException("X-Axis data cannot be empty!!!"); + } + + Series series = null; + if (xData != null) { + // 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.axisType, yData, yAxis.axisType, errorBars); + } else { // generate xData + Collection<Number> generatedXData = new ArrayList<Number>(); + for (int i = 1; i < yData.size(); i++) { + generatedXData.add(i); + } + xAxis.setAxisType(AxisType.NUMBER); + yAxis.setAxisType(AxisType.NUMBER); + series = new Series(seriesName, generatedXData, xAxis.axisType, yData, yAxis.axisType, errorBars); + } + + // Sanity check + if (xData != null && xData.size() != yData.size()) { + throw new IllegalArgumentException("X and Y-Axis sizes are not the same!!! "); + } + if (errorBars != null && errorBars.size() != yData.size()) { + throw new IllegalArgumentException("errorbars and Y-Axis sizes are not the same!!! "); + } + + seriesMap.put(seriesCount++, series); + + // add min/max to axis + xAxis.addMinMax(series.xMin, series.xMax); + yAxis.addMinMax(series.yMin, series.yMax); + + return series; + } + + protected Rectangle getChartTitleBounds() { + + return chart.chartTitle.getBounds(); + } + + protected Rectangle getChartLegendBounds() { + + return chart.chartLegend.getBounds(); + } + + protected static int getTickSpace(int workingSpace) { + + return (int) (workingSpace * 0.95); + } + + protected static int getMargin(int workingSpace, int tickSpace) { + + int marginSpace = workingSpace - tickSpace; + return (int) (marginSpace / 2.0); + } + + @Override + public void paint(Graphics2D g) { + + yAxis.paint(g); + xAxis.paint(g); + } + + @Override + public Rectangle getBounds() { + + return null; // should never be called + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java new file mode 100644 index 0000000000000000000000000000000000000000..39a5d568e473ffb7e54df5a57b2ede16fc4b009b --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java @@ -0,0 +1,280 @@ +/** + * 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.internal.chartpart; + +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; + +import com.xeiam.xchart.internal.chartpart.Axis.AxisType; +import com.xeiam.xchart.internal.interfaces.IChartPart; +import com.xeiam.xchart.internal.interfaces.IHideable; + +/** + * An axis tick. + */ +public class AxisTick implements IChartPart, IHideable { + + /** 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; + + /** parent */ + protected Axis axis; + + /** the axisticklabels */ + public AxisTickLabels axisTickLabels; + + /** the axistickmarks */ + protected AxisTickMarks axisTickMarks; + + /** the arraylist of tick label position in pixels */ + protected List<Integer> tickLocations; + + /** the arraylist of tick label values */ + protected List<String> tickLabels; + + private int workingSpace; + + /** the Locale for Date tick labels */ + public Locale locale; + + public String normalDecimalPattern; + public String scientificDecimalPattern; + public String datePattern; + + /** the bounds */ + private Rectangle bounds; + + /** the visibility state of axistick */ + protected boolean isVisible = true; // default to true + + /** + * Constructor + * + * @param axis the axis + */ + protected AxisTick(Axis axis) { + + this.axis = axis; + axisTickLabels = new AxisTickLabels(this); + axisTickMarks = new AxisTickMarks(this); + + // formatting + locale = Locale.getDefault(); + normalDecimalPattern = "#.###"; + scientificDecimalPattern = "0.###E0"; + datePattern = "HHmmss"; + + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + if (axis.direction == Axis.Direction.Y) { + workingSpace = (int) axis.getPaintZone().getHeight(); // number of pixels the axis has to work with for drawing AxisTicks + // System.out.println("workingspace= " + workingSpace); + } else { + workingSpace = (int) axis.getPaintZone().getWidth(); // number of pixels the axis has to work with for drawing AxisTicks + // System.out.println("workingspace= " + workingSpace); + } + + determineAxisTick(); + + // for (Integer position : tickLocations) { + // System.out.println(position); + // } + // for (String label : tickLabels) { + // System.out.println(label); + // } + + if (isVisible) { + axisTickLabels.paint(g); + axisTickMarks.paint(g); + + if (axis.direction == 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); + } + } + + } + + /** + * + */ + private void determineAxisTick() { + + tickLocations = new LinkedList<Integer>(); + tickLabels = new LinkedList<String>(); + + // System.out.println("workingSpace= " + workingSpace); + + int tickSpace = AxisPair.getTickSpace(workingSpace); + // System.out.println("tickSpace= " + tickSpace); + + int margin = AxisPair.getMargin(workingSpace, tickSpace); + + // a check if all axis data are the exact same values + if (axis.max == axis.min) { + tickLabels.add(format(axis.max)); + tickLocations.add((int) (margin + tickSpace / 2.0)); + } else { + + final BigDecimal min = new BigDecimal(axis.min.doubleValue()); + BigDecimal firstPosition; + BigDecimal gridStep = getGridStep(tickSpace); + + double xyz = min.remainder(gridStep).doubleValue(); + if (xyz <= 0.0) { + firstPosition = min.subtract(min.remainder(gridStep)); + } else { + firstPosition = min.subtract(min.remainder(gridStep)).add(gridStep); + } + + for (BigDecimal b = firstPosition; b.compareTo(axis.max) <= 0; b = b.add(gridStep)) { + + // System.out.println("b= " + b); + tickLabels.add(format(b)); + int tickLabelPosition = (int) (margin + ((b.subtract(axis.min)).doubleValue() / (axis.max.subtract(axis.min)).doubleValue() * tickSpace)); + // System.out.println("tickLabelPosition= " + tickLabelPosition); + + tickLocations.add(tickLabelPosition); + } + } + } + + private BigDecimal getGridStep(int tickSpace) { + + double length = Math.abs(axis.max.subtract(axis.min).doubleValue()); + // System.out.println(axis.getMax()); + // System.out.println(axis.min); + // System.out.println(length); + double gridStepHint = length / tickSpace * DEFAULT_TICK_MARK_STEP_HINT; + + // gridStepHint --> mantissa * 10 ** exponent + // e.g. 724.1 --> 7.241 * 10 ** 2 + double mantissa = gridStepHint; + int exponent = 0; + if (mantissa == 0) { + exponent = 1; + } else if (mantissa < 1) { + while (mantissa < 1) { + mantissa *= 10.0; + exponent--; + } + } else { + while (mantissa >= 10) { + mantissa /= 10.0; + exponent++; + } + } + + // calculate the grid step with hint. + BigDecimal gridStep; + if (mantissa > 7.5) { + // gridStep = 10.0 * 10 ** exponent + gridStep = BigDecimal.TEN.multiply(pow(10, exponent)); + } else if (mantissa > 3.5) { + // gridStep = 5.0 * 10 ** exponent + gridStep = new BigDecimal(new Double(5).toString()).multiply(pow(10, exponent)); + } else if (mantissa > 1.5) { + // gridStep = 2.0 * 10 ** exponent + gridStep = new BigDecimal(new Double(2).toString()).multiply(pow(10, exponent)); + } else { + // gridStep = 1.0 * 10 ** exponent + gridStep = pow(10, exponent); + } + return gridStep; + } + + /** + * Calculates the value of the first argument raised to the power of the second argument. + * + * @param base the base + * @param exponent the exponent + * @return the value <tt>a<sup>b</sup></tt> in <tt>BigDecimal</tt> + */ + private BigDecimal pow(double base, int exponent) { + + BigDecimal value; + if (exponent > 0) { + value = new BigDecimal(new Double(base).toString()).pow(exponent); + } else { + value = BigDecimal.ONE.divide(new BigDecimal(new Double(base).toString()).pow(-exponent)); + } + return value; + } + + private String format(BigDecimal value) { + + if (axis.axisType == AxisType.NUMBER) { + + NumberFormat nf = NumberFormat.getNumberInstance(locale); + + if (Math.abs(value.doubleValue()) <= 9999 && Math.abs(value.doubleValue()) > .0001 || value.doubleValue() == 0) { + + DecimalFormat normalFormat = (DecimalFormat) nf; + normalFormat.applyPattern(normalDecimalPattern); + return normalFormat.format(value.doubleValue()); + + } else { + + DecimalFormat scientificFormat = (DecimalFormat) nf; + scientificFormat.applyPattern(scientificDecimalPattern); + return scientificFormat.format(value.doubleValue()); + + } + } else { + + // TODO set this more intelligently + SimpleDateFormat simpleDateformat = new SimpleDateFormat(datePattern, locale); + simpleDateformat.applyPattern(datePattern); + return simpleDateformat.format(value.longValueExact()); + + } + + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java new file mode 100644 index 0000000000000000000000000000000000000000..ef66f9915ab60c9e82ed68c3da3281e4bc461fcf --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java @@ -0,0 +1,118 @@ +/** + * 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.internal.chartpart; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.font.FontRenderContext; +import java.awt.font.TextLayout; + +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * Axis tick labels + */ +public class AxisTickLabels implements IChartPart { + + /** parent */ + private AxisTick axisTick; + + /** the font */ + public Font font; + + /** the bounds */ + private Rectangle bounds; + + /** + * Constructor + * + * @param axisTick + */ + protected AxisTickLabels(AxisTick axisTick) { + + this.axisTick = axisTick; + font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + g.setColor(axisTick.axis.axisPair.chart.fontColor); + + if (axisTick.axis.direction == Axis.Direction.Y) { // Y-Axis + + int xOffset = (int) (axisTick.axis.getAxisTitle().getBounds().getX() + axisTick.axis.getAxisTitle().getBounds().getWidth()); + int yOffset = (int) (axisTick.axis.getPaintZone().getY()); + int maxTickLabelWidth = 0; + for (int i = 0; i < axisTick.tickLabels.size(); i++) { + + String tickLabel = axisTick.tickLabels.get(i); + int tickLocation = axisTick.tickLocations.get(i); + + FontRenderContext frc = g.getFontRenderContext(); + // TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); + TextLayout layout = new TextLayout(tickLabel, font, frc); + Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); + layout.draw(g, xOffset, (int) (yOffset + axisTick.axis.getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0)); + + if (tickLabelBounds.getWidth() > maxTickLabelWidth) { + maxTickLabelWidth = (int) tickLabelBounds.getWidth(); + } + } + + // bounds + bounds = new Rectangle(xOffset, yOffset, maxTickLabelWidth, (int) axisTick.axis.getPaintZone().getHeight()); + // g.setColor(Color.blue); + // g.draw(bounds); + + } else { // X-Axis + + int xOffset = (int) (axisTick.axis.getPaintZone().getX()); + int yOffset = (int) (axisTick.axis.getAxisTitle().getBounds().getY()); + int maxTickLabelHeight = 0; + for (int i = 0; i < axisTick.tickLabels.size(); i++) { + + String tickLabel = axisTick.tickLabels.get(i); + int tickLocation = axisTick.tickLocations.get(i); + + FontRenderContext frc = g.getFontRenderContext(); + TextLayout layout = new TextLayout(tickLabel, font, frc); + Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); + layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset); + + if (tickLabelBounds.getHeight() > maxTickLabelHeight) { + maxTickLabelHeight = (int) tickLabelBounds.getHeight(); + } + } + + // bounds + bounds = new Rectangle(xOffset, yOffset - maxTickLabelHeight, (int) axisTick.axis.getPaintZone().getWidth(), maxTickLabelHeight); + // g.setColor(Color.blue); + // g.draw(bounds); + + } + + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java new file mode 100644 index 0000000000000000000000000000000000000000..30e29ca9f97da3f71d2f0949c80ebebb73821807 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java @@ -0,0 +1,113 @@ +/** + * 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.internal.chartpart; + +import java.awt.BasicStroke; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.Stroke; + +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * Axis tick marks. + */ +public class AxisTickMarks implements IChartPart { + + /** the tick length */ + public static final int TICK_LENGTH = 3; + + /** parent */ + private AxisTick axisTick; + + /** the line style */ + private Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + /** the bounds */ + private Rectangle bounds; + + /** + * Constructor + * + * @param axisTick + */ + protected AxisTickMarks(AxisTick axisTick) { + + this.axisTick = axisTick; + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + g.setColor(axisTick.axis.axisPair.chart.bordersColor); + + if (axisTick.axis.direction == Axis.Direction.Y) { // Y-Axis + + int xOffset = (int) (axisTick.axisTickLabels.getBounds().getX() + axisTick.axisTickLabels.getBounds().getWidth() + AxisTick.AXIS_TICK_PADDING); + int yOffset = (int) (axisTick.axis.getPaintZone().getY()); + + // tick marks + for (int i = 0; i < axisTick.tickLabels.size(); i++) { + + int tickLocation = axisTick.tickLocations.get(i); + + g.setColor(axisTick.axis.axisPair.chart.bordersColor); + g.setStroke(stroke); + + g.drawLine(xOffset, yOffset + (int) (axisTick.axis.getPaintZone().getHeight() - tickLocation), xOffset + TICK_LENGTH, yOffset + (int) (axisTick.axis.getPaintZone().getHeight() - tickLocation)); + + } + // Line + g.drawLine(xOffset + TICK_LENGTH, yOffset, xOffset + TICK_LENGTH, yOffset + (int) axisTick.axis.getPaintZone().getHeight()); + + // bounds + bounds = new Rectangle(xOffset, yOffset, TICK_LENGTH, (int) axisTick.axis.getPaintZone().getHeight()); + // g.setColor(Color.yellow); + // g.draw(bounds); + + } else { // X-Axis + + int xOffset = (int) (axisTick.axis.getPaintZone().getX()); + int yOffset = (int) (axisTick.axisTickLabels.getBounds().getY() - AxisTick.AXIS_TICK_PADDING); + + // tick marks + for (int i = 0; i < axisTick.tickLabels.size(); i++) { + + int tickLocation = axisTick.tickLocations.get(i); + + g.setColor(axisTick.axis.axisPair.chart.bordersColor); + g.setStroke(stroke); + + g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - TICK_LENGTH); + } + // Line + g.drawLine(xOffset, yOffset - TICK_LENGTH, xOffset + (int) axisTick.axis.getPaintZone().getWidth(), yOffset - TICK_LENGTH); + + // bounds + bounds = new Rectangle(xOffset, yOffset - TICK_LENGTH, (int) axisTick.axis.getPaintZone().getWidth(), TICK_LENGTH); + // g.setColor(Color.yellow); + // g.draw(bounds); + } + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java new file mode 100644 index 0000000000000000000000000000000000000000..e52fef2b196044c17fadd9bf5f1906e2fe880632 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java @@ -0,0 +1,151 @@ +/** + * 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.internal.chartpart; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.font.FontRenderContext; +import java.awt.font.TextLayout; +import java.awt.geom.AffineTransform; + +import com.xeiam.xchart.internal.interfaces.IChartPart; +import com.xeiam.xchart.internal.interfaces.IHideable; + +/** + * AxisTitle + */ +public class AxisTitle implements IChartPart, IHideable { + + protected final static int AXIS_TITLE_PADDING = 10; + + /** parent */ + private Axis axis; + + /** the title text */ + protected String text = ""; // default to "" + + /** the visibility state of title */ + protected boolean isVisible = false; // default to false, set true if text is set + + /** the font */ + public Font font; + + /** the bounds */ + private Rectangle bounds; + + /** + * Constructor. + * + * @param axis the axis + */ + protected AxisTitle(Axis axis) { + + this.axis = axis; + font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font + } + + protected String getText() { + + return text; + } + + protected void setText(String text) { + + if (text.trim().equalsIgnoreCase("")) { + this.isVisible = false; + } else { + this.isVisible = true; + } + this.text = text; + } + + protected Font getFont() { + + return font; + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + g.setColor(axis.axisPair.chart.fontColor); + + if (axis.direction == Axis.Direction.Y) { + if (isVisible) { + + FontRenderContext frc = g.getFontRenderContext(); + TextLayout nonRotatedTextLayout = new TextLayout(text, font, frc); + Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0); + // System.out.println(nonRotatedRectangle); + + TextLayout rotatedTextLayout = new TextLayout(text, font.deriveFont(AffineTransform.getRotateInstance(Math.PI / -2.0, 0, 0)), frc); + // Rectangle rotatedRectangle = rotatedTextLayout.getPixelBounds(null, 0, 0); + // System.out.println(rotatedRectangle); + + int xOffset = (int) (axis.getPaintZone().getX() + nonRotatedRectangle.getHeight()); + int yOffset = (int) ((axis.getPaintZone().getHeight() + nonRotatedRectangle.getWidth()) / 2.0 + axis.getPaintZone().getY()); + rotatedTextLayout.draw(g, xOffset, yOffset); + + // bounds + bounds = new Rectangle((int) (xOffset - nonRotatedRectangle.getHeight()), (int) (yOffset - nonRotatedRectangle.getWidth()), (int) nonRotatedRectangle.getHeight() + AXIS_TITLE_PADDING, + (int) nonRotatedRectangle.getWidth()); + // g.setColor(Color.blue); + // g.draw(bounds); + } else { + bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) axis.getPaintZone().getY(), 0, (int) axis.getPaintZone().getHeight()); + } + + } else { + + if (isVisible) { + + FontRenderContext frc = g.getFontRenderContext(); + TextLayout textLayout = new TextLayout(text, font, frc); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + // System.out.println(rectangle); + + int xOffset = (int) (axis.getPaintZone().getX() + (axis.getPaintZone().getWidth() - rectangle.getWidth()) / 2.0); + int yOffset = (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight() - rectangle.getHeight()); + + textLayout.draw(g, xOffset, (float) (yOffset - rectangle.getY())); + + bounds = new Rectangle(xOffset, yOffset - AXIS_TITLE_PADDING, (int) rectangle.getWidth(), (int) rectangle.getHeight() + AXIS_TITLE_PADDING); + // g.setColor(Color.blue); + // g.draw(bounds); + + } else { + bounds = new Rectangle((int) axis.getPaintZone().getX(), (int) (axis.getPaintZone().getY() + axis.getPaintZone().getHeight()), (int) axis.getPaintZone().getWidth(), 0); + // g.setColor(Color.blue); + // g.draw(bounds); + + } + } + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java b/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java new file mode 100644 index 0000000000000000000000000000000000000000..3b38c746279436a974f625a118aabe5d87c0039f --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java @@ -0,0 +1,103 @@ +/** + * 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.internal.chartpart; + +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.font.FontRenderContext; +import java.awt.font.TextLayout; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.internal.interfaces.IChartPart; +import com.xeiam.xchart.internal.interfaces.IHideable; + +/** + * Chart Title + */ +public class ChartTitle implements IChartPart, IHideable { + + /** parent */ + private Chart chart; + + /** the title text */ + protected String text = ""; // default to "" + + /** the visibility state of title */ + protected boolean isVisible = false; // default to false + + /** the font */ + public Font font; + + /** the bounds */ + private Rectangle bounds; + + /** + * Constructor + * + * @param chart + */ + public ChartTitle(Chart chart) { + + this.chart = chart; + font = new Font(Font.SANS_SERIF, Font.BOLD, 14); // default font + } + + public void setText(String text) { + + if (text.trim().equalsIgnoreCase("")) { + this.isVisible = false; + } else { + this.isVisible = true; + } + this.text = text; + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + if (isVisible) { + + FontRenderContext frc = g.getFontRenderContext(); + TextLayout textLayout = new TextLayout(text, font, frc); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + int xOffset = (int) ((chart.width - rectangle.getWidth()) / 2.0); + int yOffset = (int) ((isVisible ? (Chart.CHART_PADDING - rectangle.getY()) : 0)); + + 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); + + g.setColor(chart.fontColor); + textLayout.draw(g, xOffset, yOffset); + } + + } + + @Override + public Rectangle getBounds() { + + return bounds; + } +} \ No newline at end of file diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java b/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java new file mode 100644 index 0000000000000000000000000000000000000000..984b8d41d88d389504c3a7783914d9293e911824 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java @@ -0,0 +1,156 @@ +/** + * 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.internal.chartpart; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.font.FontRenderContext; +import java.awt.font.TextLayout; +import java.util.Map; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.ChartColor; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.internal.interfaces.IChartPart; +import com.xeiam.xchart.internal.interfaces.IHideable; +import com.xeiam.xchart.internal.markers.Marker; + +/** + * @author timmolter + */ +public class Legend implements IChartPart, IHideable { + + private static final int LEGEND_PADDING = 10; + + /** parent */ + private Chart chart; + + /** the visibility state of legend */ + protected boolean isVisible = true; // default to true + + /** the font */ + public Font font; + + /** the background color */ + public Color backgroundColor; + + /** the bounds */ + private Rectangle bounds; + + /** + * Constructor + * + * @param chart + */ + public Legend(Chart chart) { + + this.chart = chart; + backgroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color + font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + if (isVisible) { + + Map<Integer, Series> seriesMap = chart.axisPair.seriesMap; + + // determine legend text content max width + int legendTextContentMaxWidth = 0; + int legendTextContentMaxHeight = 0; + + for (Integer seriesId : seriesMap.keySet()) { + Series series = seriesMap.get(seriesId); + TextLayout textLayout = new TextLayout(series.name, font, new FontRenderContext(null, true, false)); + Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); + // System.out.println(rectangle); + if (rectangle.getWidth() > legendTextContentMaxWidth) { + legendTextContentMaxWidth = (int) rectangle.getWidth(); + } + if (rectangle.getHeight() > legendTextContentMaxHeight) { + legendTextContentMaxHeight = (int) rectangle.getHeight(); + } + } + + // determine legend content height + int legendContentHeight = 0; + int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); + legendContentHeight = maxContentHeight * seriesMap.size() + LEGEND_PADDING * (seriesMap.size() - 1); + + // determine legend content width + int legendContentWidth = (int) (3.0 * Marker.SIZE + LEGEND_PADDING + legendTextContentMaxWidth); + + // Draw Legend Box + int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING; + int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING; + int xOffset = chart.width - legendBoxWidth - Chart.CHART_PADDING; + int yOffset = (int) ((chart.height - legendBoxHeight) / 2.0 + chart.chartTitle.getBounds().getY() + chart.chartTitle.getBounds().getHeight()); + + g.setColor(chart.bordersColor); + g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); + g.setColor(backgroundColor); + g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); + + // Draw legend content inside legend box + int startx = xOffset + LEGEND_PADDING; + int starty = yOffset + LEGEND_PADDING; + for (Integer seriesId : seriesMap.keySet()) { + Series series = seriesMap.get(seriesId); + // paint line + if (series.stroke != null) { + g.setColor(series.strokeColor); + g.setStroke(series.stroke); + g.drawLine(startx, starty - Marker.Y_OFFSET, (int) (startx + Marker.SIZE * 3.0), starty - Marker.Y_OFFSET); + } + // paint marker + if (series.marker != null) { + g.setColor(series.markerColor); + series.marker.paint(g, (int) (startx + (Marker.SIZE * 1.5)), starty - Marker.Y_OFFSET); + } + + // paint series name + g.setColor(chart.fontColor); + TextLayout layout = new TextLayout(series.name, font, new FontRenderContext(null, true, false)); + layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + LEGEND_PADDING), (starty + Marker.SIZE)); + starty = starty + legendTextContentMaxHeight + LEGEND_PADDING; + } + + // bounds + bounds = new Rectangle(xOffset, yOffset, legendBoxWidth, legendBoxHeight); + // g.setColor(Color.blue); + // g.draw(bounds); + } + + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java b/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java new file mode 100644 index 0000000000000000000000000000000000000000..2713239afb1d0711a790f8bb66a25f4ec10f741a --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/Plot.java @@ -0,0 +1,73 @@ +/** + * 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.internal.chartpart; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * @author timmolter + */ +public class Plot implements IChartPart { + + /** parent */ + protected Chart chart; + + public PlotSurface plotSurface; + + protected PlotContent plotContent; + + public static final int PLOT_PADDING = 3; + + /** the bounds */ + private Rectangle bounds; + + public Plot(Chart chart) { + + this.chart = chart; + this.plotSurface = new PlotSurface(this); + this.plotContent = new PlotContent(this); + } + + @Override + public Rectangle getBounds() { + + return bounds; + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + + // calculate bounds + int xOffset = (int) (chart.axisPair.yAxis.getBounds().getX() + chart.axisPair.yAxis.getBounds().getWidth() + (chart.axisPair.yAxis.axisTick.isVisible ? (Plot.PLOT_PADDING + 1) : 0)); + int yOffset = (int) (chart.axisPair.yAxis.getBounds().getY()); + int width = (int) chart.axisPair.xAxis.getBounds().getWidth(); + int height = (int) chart.axisPair.yAxis.getBounds().getHeight(); + bounds = new Rectangle(xOffset, yOffset, width, height); + // g.setColor(Color.green); + // g.draw(bounds); + + plotSurface.paint(g); + plotContent.paint(g); + + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java b/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java new file mode 100644 index 0000000000000000000000000000000000000000..16a3d68dbd185f3758d6fda80fc5e3c3af976ab7 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java @@ -0,0 +1,162 @@ +/** + * 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.internal.chartpart; + +import java.awt.BasicStroke; +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.Series; +import com.xeiam.xchart.internal.chartpart.Axis.AxisType; +import com.xeiam.xchart.internal.interfaces.IChartPart; + +/** + * @author timmolter + */ +public class PlotContent implements IChartPart { + + /** parent */ + private Plot plot; + + /** + * Constructor + * + * @param plot + */ + protected PlotContent(Plot plot) { + + this.plot = plot; + } + + @Override + public Rectangle getBounds() { + + return plot.getBounds(); + } + + @Override + public void paint(Graphics2D g) { + + Rectangle bounds = plot.getBounds(); + + Map<Integer, Series> seriesMap = plot.chart.axisPair.seriesMap; + for (Integer seriesId : seriesMap.keySet()) { + + Series series = seriesMap.get(seriesId); + + // X-Axis + int xTickSpace = AxisPair.getTickSpace((int) bounds.getWidth()); + int xLeftMargin = AxisPair.getMargin((int) bounds.getWidth(), xTickSpace); + + // Y-Axis + int yTickSpace = AxisPair.getTickSpace((int) bounds.getHeight()); + int yTopMargin = AxisPair.getMargin((int) bounds.getHeight(), yTickSpace); + + // data points + Collection<?> xData = series.xData; + BigDecimal xMin = plot.chart.axisPair.xAxis.min; + BigDecimal xMax = plot.chart.axisPair.xAxis.max; + Collection<Number> yData = series.yData; + BigDecimal yMin = plot.chart.axisPair.yAxis.min; + BigDecimal yMax = plot.chart.axisPair.yAxis.max; + Collection<Number> errorBars = series.errorBars; + + int previousX = Integer.MIN_VALUE; + int previousY = Integer.MIN_VALUE; + + Iterator<?> xItr = xData.iterator(); + Iterator<Number> yItr = yData.iterator(); + Iterator<Number> ebItr = null; + if (errorBars != null) { + ebItr = errorBars.iterator(); + } + while (xItr.hasNext()) { + + BigDecimal x = null; + if (plot.chart.axisPair.xAxis.axisType == AxisType.NUMBER) { + x = new BigDecimal(((Number) xItr.next()).doubleValue()); + } + if (plot.chart.axisPair.xAxis.axisType == 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(); + } + + // 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.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.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.stroke != null) { + if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { + g.setColor(series.strokeColor); + g.setStroke(series.stroke); + g.drawLine(previousX, previousY, xOffset, yOffset); + } + previousX = xOffset; + previousY = yOffset; + } + + // paint marker + if (series.marker != null) { + g.setColor(series.markerColor); + series.marker.paint(g, xOffset, yOffset); + } + + // paint errorbar + if (errorBars != null) { + g.setColor(plot.chart.bordersColor); + g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); + 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/internal/chartpart/PlotSurface.java b/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java new file mode 100644 index 0000000000000000000000000000000000000000..8965a721c69498afdd42a287d8d0338d9a04740c --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java @@ -0,0 +1,130 @@ +/** + * 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.internal.chartpart; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.util.List; + +import com.xeiam.xchart.ChartColor; +import com.xeiam.xchart.internal.interfaces.IChartPart; +import com.xeiam.xchart.internal.interfaces.IHideable; + +/** + * @author timmolter + */ +public class PlotSurface implements IChartPart, IHideable { + + /** parent */ + private Plot plot; + + /** the gridLines Color */ + private Color gridLinesColor; + + /** the background color */ + private Color foregroundColor; + + /** the line style */ + private BasicStroke stroke; + + /** the visibility state of PlotSurface */ + protected boolean isVisible = true; // default to true + + /** + * Constructor + * + * @param plot + */ + protected PlotSurface(Plot plot) { + + this.plot = plot; + gridLinesColor = ChartColor.getAWTColor(ChartColor.GREY); // default gridLines color + foregroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default foreground Color color + stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f); + } + + @Override + public Rectangle getBounds() { + + return plot.getBounds(); + } + + @Override + public void paint(Graphics2D g) { + + Rectangle bounds = plot.getBounds(); + + // paint foreground + Rectangle backgroundRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); + g.setColor(foregroundColor); + g.fill(backgroundRectangle); + Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); + g.setColor(plot.chart.bordersColor); + g.draw(borderRectangle); + + // paint grid lines + if (isVisible) { + // horizontal + List<Integer> yAxisTickLocations = plot.chart.axisPair.yAxis.axisTick.tickLocations; + for (int i = 0; i < yAxisTickLocations.size(); i++) { + + int tickLocation = yAxisTickLocations.get(i); + + g.setColor(gridLinesColor); + 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 = plot.chart.axisPair.xAxis.axisTick.tickLocations; + for (int i = 0; i < xAxisTickLocations.size(); i++) { + + int tickLocation = xAxisTickLocations.get(i); + + g.setColor(gridLinesColor); + 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)); + } + } + } + + @Override + public void setVisible(boolean isVisible) { + + this.isVisible = isVisible; + } + + /** + * @param gridLinesColor the gridLinesColor to set + */ + public void setGridLinesColor(Color gridLinesColor) { + + this.gridLinesColor = gridLinesColor; + } + + /** + * @param foregroundColor the foregroundColor to set + */ + public void setForegroundColor(Color foregroundColor) { + + this.foregroundColor = foregroundColor; + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/interfaces/IChartPart.java b/src/main/java/com/xeiam/xchart/internal/interfaces/IChartPart.java new file mode 100644 index 0000000000000000000000000000000000000000..0e385adc3b31feb1e15514bbc0db944615af507c --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/interfaces/IChartPart.java @@ -0,0 +1,32 @@ +/** + * 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.internal.interfaces; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +/** + * All components of a chart that need to be painted should implement this interface + * + * @author timmolter + */ +public interface IChartPart { + + public Rectangle getBounds(); + + public void paint(final Graphics2D g); + +} diff --git a/src/main/java/com/xeiam/xchart/internal/interfaces/IHideable.java b/src/main/java/com/xeiam/xchart/internal/interfaces/IHideable.java new file mode 100644 index 0000000000000000000000000000000000000000..b546fc47fbf4dda488b53b80828f5c3d924c1d72 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/interfaces/IHideable.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2008-2011 SWTChart project. All rights reserved. + * + * This code is distributed under the terms of the Eclipse Public License v1.0 + * which is available at http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package com.xeiam.xchart.internal.interfaces; + +/** + * ChartParts that can be set visible or not should implement this interface + * + * @author timmolter + */ +public interface IHideable extends IChartPart { + + public void setVisible(boolean isVisible); + +} \ No newline at end of file diff --git a/src/main/java/com/xeiam/xchart/internal/markers/Circle.java b/src/main/java/com/xeiam/xchart/internal/markers/Circle.java new file mode 100644 index 0000000000000000000000000000000000000000..c60b6238a823e6d868c50183dea273dd9a87e945 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/Circle.java @@ -0,0 +1,33 @@ +/** + * 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.internal.markers; + +import java.awt.Graphics2D; + +/** + * @author timmolter + */ +public class Circle extends Marker { + + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { + + g.setStroke(stroke); + g.fillOval(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE); + + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/markers/Diamond.java b/src/main/java/com/xeiam/xchart/internal/markers/Diamond.java new file mode 100644 index 0000000000000000000000000000000000000000..f14c43ad23370d0bbde13a3bc3c40f27a0d47045 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/Diamond.java @@ -0,0 +1,52 @@ +/** + * 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.internal.markers; + +import java.awt.Graphics2D; +import java.awt.Polygon; + +/** + * @author timmolter + */ +public class Diamond extends Marker { + + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { + + g.setStroke(stroke); + + int[] x = new int[4]; + int[] y = new int[4]; + int n = 4; + + // Make a diamond + int halfSize = (int) (Math.ceil((Marker.SIZE + 3) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + halfSize; + x[2] = xOffset - halfSize + Marker.SIZE + 3; + x[3] = xOffset - halfSize + halfSize; + + y[0] = 1 + yOffset - halfSize + halfSize; + y[1] = 1 + yOffset - halfSize + Marker.SIZE + 3; + y[2] = 1 + yOffset - halfSize + halfSize; + y[3] = 1 + yOffset - halfSize + 0; + + Polygon diamond = new Polygon(x, y, n); + g.fillPolygon(diamond); + + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/markers/Marker.java b/src/main/java/com/xeiam/xchart/internal/markers/Marker.java new file mode 100644 index 0000000000000000000000000000000000000000..304c19b603b8e3dd0691d72471d644c5b89c9ce9 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/Marker.java @@ -0,0 +1,34 @@ +/** + * 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.internal.markers; + +import java.awt.BasicStroke; +import java.awt.Graphics2D; + +/** + * @author timmolter + */ +public abstract class Marker { + + protected BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); + + public static final int SIZE = 7; // make this an odd number! + + public static final int X_OFFSET = (int) (-1.0 * (SIZE / 2.0)); + public static final int Y_OFFSET = (int) (-1.0 * (SIZE / 2.0)); + + public abstract void paint(Graphics2D g, int xOffset, int yOffset); +} diff --git a/src/main/java/com/xeiam/xchart/internal/markers/Square.java b/src/main/java/com/xeiam/xchart/internal/markers/Square.java new file mode 100644 index 0000000000000000000000000000000000000000..5117a4d7223d74eb1a3865c98ef3f7fcb24cfac6 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/Square.java @@ -0,0 +1,34 @@ +/** + * 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.internal.markers; + +import java.awt.Graphics2D; +import java.awt.Rectangle; + +/** + * @author timmolter + */ +public class Square extends Marker { + + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { + + g.setStroke(stroke); + g.fill(new Rectangle(xOffset + Marker.X_OFFSET, yOffset + Marker.Y_OFFSET, Marker.SIZE, Marker.SIZE)); + + } + +} diff --git a/src/main/java/com/xeiam/xchart/internal/markers/TriangleDown.java b/src/main/java/com/xeiam/xchart/internal/markers/TriangleDown.java new file mode 100644 index 0000000000000000000000000000000000000000..944f5d80321df7081afc24d17c4ee7a49d3ce013 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/TriangleDown.java @@ -0,0 +1,49 @@ +/** + * 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.internal.markers; + +import java.awt.Graphics2D; +import java.awt.Polygon; + +/** + * @author timmolter + */ +public class TriangleDown extends Marker { + + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { + + g.setStroke(stroke); + + int[] x = new int[3]; + int[] y = new int[3]; + int n = 3; + + // Make a triangle + int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + halfSize; + x[2] = xOffset - halfSize + Marker.SIZE + 1; + + y[0] = 1 + yOffset - halfSize + 0; + y[1] = 1 + yOffset - halfSize + Marker.SIZE + 1; + y[2] = 1 + yOffset - halfSize + 0; + + Polygon triangle = new Polygon(x, y, n); + g.fillPolygon(triangle); + + } +} diff --git a/src/main/java/com/xeiam/xchart/internal/markers/TriangleUp.java b/src/main/java/com/xeiam/xchart/internal/markers/TriangleUp.java new file mode 100644 index 0000000000000000000000000000000000000000..5d7e4bb45209f5053073d58d61ac90f39dfd0b26 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/internal/markers/TriangleUp.java @@ -0,0 +1,49 @@ +/** + * 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.internal.markers; + +import java.awt.Graphics2D; +import java.awt.Polygon; + +/** + * @author timmolter + */ +public class TriangleUp extends Marker { + + @Override + public void paint(Graphics2D g, int xOffset, int yOffset) { + + g.setStroke(stroke); + + int[] x = new int[3]; + int[] y = new int[3]; + int n = 3; + + // Make a triangle + int halfSize = (int) (Math.ceil((Marker.SIZE + 1) / 2.0)); + x[0] = xOffset - halfSize + 0; + x[1] = xOffset - halfSize + Marker.SIZE + 1; + x[2] = xOffset - halfSize + halfSize; + + y[0] = yOffset - halfSize + Marker.SIZE + 1; + y[1] = yOffset - halfSize + Marker.SIZE + 1; + y[2] = yOffset - halfSize + 0; + + Polygon triangle = new Polygon(x, y, n); + g.fillPolygon(triangle); + + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example1.java b/src/test/java/com/xeiam/xchart/example/Example1.java index e71b16b7ba80f1d093332badf9ab7c866957780f..d3378f195ba32c709243379abd4bf79bc6f505af 100644 --- a/src/test/java/com/xeiam/xchart/example/Example1.java +++ b/src/test/java/com/xeiam/xchart/example/Example1.java @@ -22,7 +22,7 @@ import com.xeiam.xchart.BitmapEncoder; import com.xeiam.xchart.Chart; /** - * Creates a simple charts and saves it as a PNG image file. + * Creates a simple Chart and saves it as a PNG and JPEG image file. * * @author timmolter */ @@ -41,6 +41,7 @@ public class Example1 { chart.addSeries("y(x)", xData, yData); BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); + BitmapEncoder.saveJPG(chart, "./Sample_Chart.jpg", 0.95f); } diff --git a/src/test/java/com/xeiam/xchart/example/Example2.java b/src/test/java/com/xeiam/xchart/example/Example2.java index 94075eb392ec1d3cf493f3c927d7dc3ea63042d0..909d98d832969f18f14d22a436139b44ec5dae05 100644 --- a/src/test/java/com/xeiam/xchart/example/Example2.java +++ b/src/test/java/com/xeiam/xchart/example/Example2.java @@ -19,11 +19,11 @@ import java.util.ArrayList; import java.util.Collection; import com.xeiam.xchart.Chart; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.SeriesColor; +import com.xeiam.xchart.SeriesLineStyle; +import com.xeiam.xchart.SeriesMarker; import com.xeiam.xchart.SwingWrapper; -import com.xeiam.xchart.series.Series; -import com.xeiam.xchart.series.SeriesColor; -import com.xeiam.xchart.series.SeriesLineStyle; -import com.xeiam.xchart.series.SeriesMarker; /** * Embed a Chart in a simple Swing application diff --git a/src/test/java/com/xeiam/xchart/example/Example8.java b/src/test/java/com/xeiam/xchart/example/Example8.java index 8f5330b17b6c97a488e9094bf37c4962bbb8eed7..b3322d18f634cebdaed799eb482646ac084130c4 100644 --- a/src/test/java/com/xeiam/xchart/example/Example8.java +++ b/src/test/java/com/xeiam/xchart/example/Example8.java @@ -19,11 +19,11 @@ import java.util.ArrayList; import java.util.Collection; import com.xeiam.xchart.Chart; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.SeriesColor; +import com.xeiam.xchart.SeriesLineStyle; +import com.xeiam.xchart.SeriesMarker; import com.xeiam.xchart.SwingWrapper; -import com.xeiam.xchart.series.Series; -import com.xeiam.xchart.series.SeriesColor; -import com.xeiam.xchart.series.SeriesLineStyle; -import com.xeiam.xchart.series.SeriesMarker; /** * Create a Chart with error bars diff --git a/src/test/java/com/xeiam/xchart/example/Example9.java b/src/test/java/com/xeiam/xchart/example/Example9.java index 8ffe4c116393899ac2c8dff0f29b77a62f236ae4..dc9b777df6cb74e45d87c2326514bb95b25cb855 100644 --- a/src/test/java/com/xeiam/xchart/example/Example9.java +++ b/src/test/java/com/xeiam/xchart/example/Example9.java @@ -27,11 +27,11 @@ import java.util.Locale; import com.xeiam.xchart.Chart; import com.xeiam.xchart.ChartColor; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.SeriesColor; +import com.xeiam.xchart.SeriesLineStyle; +import com.xeiam.xchart.SeriesMarker; import com.xeiam.xchart.SwingWrapper; -import com.xeiam.xchart.series.Series; -import com.xeiam.xchart.series.SeriesColor; -import com.xeiam.xchart.series.SeriesLineStyle; -import com.xeiam.xchart.series.SeriesMarker; /** * Create a chart with a Date x-axis and extensive chart customization