diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java index a7f702cf19fa26947d9833e2c639df442bc73a66..6c15a09a4b63803be33142e2f32edb1941caf9a7 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java @@ -29,6 +29,7 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import com.xeiam.xchart.XChartPanel; +import com.xeiam.xchart.demo.charts.area.AreaChart01; import com.xeiam.xchart.demo.charts.line.LineChart01; import com.xeiam.xchart.demo.charts.line.LineChart02; import com.xeiam.xchart.demo.charts.line.LineChart03; @@ -167,7 +168,14 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { category = new DefaultMutableTreeNode("Scatter Charts"); top.add(category); - chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob Scatter Plot", new ScatterChart01().getChart())); + chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob", new ScatterChart01().getChart())); + category.add(chart); + + // Second category + category = new DefaultMutableTreeNode("Area Charts"); + top.add(category); + + chart = new DefaultMutableTreeNode(new ChartInfo("AreaChart01 - 3-Series", new AreaChart01().getChart())); category.add(chart); } diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/area/AreaChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/area/AreaChart01.java new file mode 100644 index 0000000000000000000000000000000000000000..5ec467d90528cc1a6f08909eaea6b57d37379921 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/area/AreaChart01.java @@ -0,0 +1,55 @@ +/** + * Copyright 2011-2013 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.demo.charts.area; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.ChartBuilder; +import com.xeiam.xchart.ChartBuilder.ChartType; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.line.ExampleChart; +import com.xeiam.xchart.style.StyleManager.LegendPosition; + +/** + * 3-Series + * + * @author timmolter + */ +public class AreaChart01 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new AreaChart01(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new ChartBuilder().chartType(ChartType.Area).width(800).height(600).title("LineChart11").xAxisTitle("X").yAxisTitle("Y").build(); + chart.addSeries("a", new double[] { 0, 3, 5, 7, 9 }, new double[] { -3, 5, 9, 6, 5 }); + chart.addSeries("b", new double[] { 0, 2, 4, 6, 9 }, new double[] { -1, 6, 4, 0, 4 }); + chart.addSeries("c", new double[] { 0, 1, 3, 8, 9 }, new double[] { -2, -1, 1, 0, 1 }); + + // Customize Chart + chart.getStyleManager().setChartTitleVisible(false); + chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW); + + return chart; + } + +} diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java index af87da336bee71b6c35dd3540d9c923903534346..de30d5471385f4e1f6261ea1e370abd3fa19e859 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java @@ -29,9 +29,8 @@ import com.xeiam.xchart.style.SeriesMarker; * <p> * Demonstrates the following: * <ul> - * <li>Series data hardcoded, perhaps copied from elsewhere</li> - * <li>LineChart without series markers</li> - * </ul> + * <li>Series data hardcoded, perhaps copied from elsewhere + * <li>LineChart without series markers * * @author timmolter */ diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java index ae07ba4a2079eabca51b045dba85a0771f4e3184..018c6a8365fd119148f6da352bd74b2838e456d3 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java @@ -31,6 +31,11 @@ import com.xeiam.xchart.style.StyleManager.LegendPosition; /** * Date Axis + * <p> + * Demonstrates the following: + * <ul> + * <li>Date-tyoe X Axis Data + * <li>Placing legend inside northwest corner of plot area */ public class LineChart04 implements ExampleChart { diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java index 86afcc66942995a6cd691346edc32f62e3671e97..b0d9053b6b4b6da1754ad7e15552653b8a22e0b1 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java @@ -25,7 +25,7 @@ import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.line.ExampleChart; /** - * Gaussian Blob Scatter Plot + * Gaussian Blob * * @author timmolter */ @@ -41,7 +41,6 @@ public class ScatterChart01 implements ExampleChart { @Override public Chart getChart() { - // generates sine data List<Number> xData = new ArrayList<Number>(); List<Number> yData = new ArrayList<Number>(); Random random = new Random(); diff --git a/xchart/src/main/java/com/xeiam/xchart/AreaChart.java b/xchart/src/main/java/com/xeiam/xchart/AreaChart.java new file mode 100644 index 0000000000000000000000000000000000000000..1e5d66097b58d553c7cfa5278d93257a6d6fd1f6 --- /dev/null +++ b/xchart/src/main/java/com/xeiam/xchart/AreaChart.java @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart; + +/** + * @author timmolter + */ +public class AreaChart extends Chart { + + /** + * Constructor + * + * @param width + * @param height + */ + public AreaChart(int width, int height) { + + super(width, height); + + } + + /** + * Constructor + * + * @param chartBuilder + */ + public AreaChart(ChartBuilder chartBuilder) { + + super(chartBuilder); + } +} diff --git a/xchart/src/main/java/com/xeiam/xchart/ChartBuilder.java b/xchart/src/main/java/com/xeiam/xchart/ChartBuilder.java index 489fc6741f9838f97755aad7f1c1dbe51768e322..ff7fefd74d05924b8f3f2a825c2aeebef047455c 100644 --- a/xchart/src/main/java/com/xeiam/xchart/ChartBuilder.java +++ b/xchart/src/main/java/com/xeiam/xchart/ChartBuilder.java @@ -31,7 +31,7 @@ public class ChartBuilder { public enum ChartType { - Line, Scatter + Line, Scatter, Area } protected ChartType chartType = ChartType.Line; @@ -96,6 +96,8 @@ public class ChartBuilder { return new LineChart(this); case Scatter: return new ScatterChart(this); + case Area: + return new AreaChart(this); default: return new LineChart(this); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java index c70b31f39e069550d3b1ae491b86cabbaee24c92..c34cf3280ccc1fa11b1f088374d72c91ac9259ae 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java @@ -25,6 +25,7 @@ import java.util.Date; import java.util.Iterator; import java.util.Map; +import com.xeiam.xchart.AreaChart; import com.xeiam.xchart.Chart; import com.xeiam.xchart.ScatterChart; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; @@ -60,6 +61,7 @@ public class PlotContent implements ChartPart { public void paint(Graphics2D g) { boolean isScatterChart = getChart() instanceof ScatterChart; + boolean isAreaChart = getChart() instanceof AreaChart; Rectangle bounds = plot.getBounds(); @@ -112,23 +114,21 @@ public class PlotContent implements ChartPart { 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)); + int yBottomOfArea = (int) (bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace)); - // a check if all y data are the exact same values + // a check if all x 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); + yBottomOfArea = (int) (bounds.getHeight() / 2.0); } int xOffset = (int) (bounds.getX() + xTransform - 1); - int yOffset = (int) (bounds.getY() + yTransform); + int yOffset = (int) (bounds.getY() + yBottomOfArea); // System.out.println(yOffset); // System.out.println(yTransform); @@ -139,10 +139,21 @@ public class PlotContent implements ChartPart { g.setStroke(series.getStroke()); g.drawLine(previousX, previousY, xOffset, yOffset); } - previousX = xOffset; - previousY = yOffset; + + } + + // paint area + if (isAreaChart) { + if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) { + g.setColor(series.getStrokeColor()); + yBottomOfArea = (int) (bounds.getY() + bounds.getHeight() - yTopMargin + 1); + g.fillPolygon(new int[] { previousX, xOffset, xOffset, previousX }, new int[] { previousY, yOffset, yBottomOfArea, yBottomOfArea }, 4); + } } + previousX = xOffset; + previousY = yOffset; + // paint marker if (series.getMarker() != null) { g.setColor(series.getMarkerColor()); diff --git a/xchart/src/main/java/com/xeiam/xchart/style/SeriesColor.java b/xchart/src/main/java/com/xeiam/xchart/style/SeriesColor.java index 3b99122a8a95559caa66bf2ebfdb54678c7d34b4..d32d63b009a23017fa7b89c7d25fa42bf0d0ac78 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/SeriesColor.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/SeriesColor.java @@ -25,40 +25,40 @@ import java.awt.Color; public enum SeriesColor { /** BLUE */ - BLUE(0, new Color(0, 55, 255)), + BLUE(0, new Color(0, 55, 255, 180)), /** ORANGE */ - ORANGE(1, new Color(255, 172, 0)), + ORANGE(1, new Color(255, 172, 0, 180)), /** PURPLE */ - PURPLE(2, new Color(128, 0, 255)), + PURPLE(2, new Color(128, 0, 255, 180)), /** GREEN */ - GREEN(3, new Color(0, 205, 0)), + GREEN(3, new Color(0, 205, 0, 180)), /** RED */ - RED(4, new Color(205, 0, 0)), + RED(4, new Color(205, 0, 0, 180)), /** YELLOW */ - YELLOW(5, new Color(255, 215, 0)), + YELLOW(5, new Color(255, 215, 0, 180)), /** MAGENTA */ - MAGENTA(6, new Color(255, 0, 255)), + MAGENTA(6, new Color(255, 0, 255, 180)), /** PINK */ - PINK(7, new Color(255, 166, 201)), + PINK(7, new Color(255, 166, 201, 180)), /** LIGHT_GREY */ - LIGHT_GREY(8, new Color(207, 207, 207)), + LIGHT_GREY(8, new Color(207, 207, 207, 180)), /** CYAN */ - CYAN(9, new Color(0, 255, 255)), + CYAN(9, new Color(0, 255, 255, 180)), /** BROWN */ - BROWN(10, new Color(102, 56, 10)), + BROWN(10, new Color(102, 56, 10, 180)), /** BLACK */ - BLACK(11, new Color(0, 0, 0)); + BLACK(11, new Color(0, 0, 0, 180)); /** The index */ private int index;