diff --git a/.gitignore b/.gitignore index 2573b9370c20486f5fa5a88702f91a8bffc94162..078b81df2854d58eefa804f0f85f8f2d70bd3a07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ target/ +javadoc/ +bin/ .classpath .project .settings/ +.DS_Store *.png *.jpg diff --git a/.travis.yml b/.travis.yml index 595b653c258ca9320bbc5614552b2cd4c16e8436..391b1a9b67ca6e519dec0eac57e36e4147541819 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: java before_install: "git clone -b travis `git config --get remote.origin.url` target/travis" -script: "mvn deploy --settings target/travis/settings.xml" +script: "[ ${TRAVIS_PULL_REQUEST} = 'false' ] && mvn clean deploy --settings target/travis/settings.xml || mvn clean verify --settings target/travis/settings.xml" # whitelist branches: diff --git a/README.md b/README.md index 46e699b758807ea284112c869eaf5de9604a529e..9f85625dafd05a7fabe134bcd2e84a6622335716 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ Now go ahead and [study some more examples](http://xeiam.com/xchart_examplecode. * ~100KB Jar * Line charts * Scatter charts -* Area Charts -* Bar Charts +* Area charts +* Bar charts * Error bars * Logarithmic axes * Date and category X-Axis @@ -45,6 +45,7 @@ Now go ahead and [study some more examples](http://xeiam.com/xchart_examplecode. * Custom legend placement * CSV import and export * High resolution chart export +* Real-time charts ## Getting Started ### Non-Maven @@ -58,7 +59,7 @@ Add the XChart library as a dependency to your pom.xml file: <dependency> <groupId>com.xeiam.xchart</groupId> <artifactId>xchart</artifactId> - <version>2.3.0</version> + <version>2.3.1</version> </dependency> For snapshots, add the following to your pom.xml file: @@ -72,7 +73,7 @@ For snapshots, add the following to your pom.xml file: <dependency> <groupId>com.xeiam</groupId> <artifactId>xchart</artifactId> - <version>2.3.1-SNAPSHOT</version> + <version>2.3.2-SNAPSHOT</version> </dependency> ## Building @@ -80,7 +81,7 @@ For snapshots, add the following to your pom.xml file: #### general mvn clean package - mvn javadoc:javadoc + mvn javadoc:aggregate #### maven-license-plugin @@ -90,7 +91,7 @@ For snapshots, add the following to your pom.xml file: ## Running Demo cd /path/to/xchart-demo/jar/ - java -cp xchart-demo-2.3.0.jar:xchart-2.3.0.jar com.xeiam.xchart.demo.XChartDemo + java -cp xchart-demo-2.3.1.jar:xchart-2.3.1.jar com.xeiam.xchart.demo.XChartDemo ## Bugs Please report any bugs or submit feature requests to [XChart's Github issue tracker](https://github.com/timmolter/XChart/issues). @@ -100,4 +101,4 @@ Please report any bugs or submit feature requests to [XChart's Github issue trac [Build History](https://travis-ci.org/timmolter/XChart/builds) ## Donations -15MvtM8e3bzepmZ5vTe8cHvrEZg6eDzw2w \ No newline at end of file +1PrZHiJorAw7RQrjP9CJgtPuqr6fU65PKt diff --git a/pom.xml b/pom.xml index 555a7c7eb028e13f9aacac436ff8a930cced53b8..95cb07e31c48cdffc99e343b6e8b98ff1f08fdb7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ <groupId>com.xeiam.xchart</groupId> <artifactId>xchart-parent</artifactId> - <version>2.3.1-SNAPSHOT</version> + <version>2.3.2-SNAPSHOT</version> <packaging>pom</packaging> <name>XChart Parent</name> <description>Basic Charts for Java Applications</description> diff --git a/xchart-demo/pom.xml b/xchart-demo/pom.xml index 40b8c9f28ee96b7098c3a9ee18f4f03ee7c11322..9b8ac3889ef458c792a6066d6475a48aaa8f5f03 100644 --- a/xchart-demo/pom.xml +++ b/xchart-demo/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>com.xeiam.xchart</groupId> <artifactId>xchart-parent</artifactId> - <version>2.3.1-SNAPSHOT</version> + <version>2.3.2-SNAPSHOT</version> </parent> <artifactId>xchart-demo</artifactId> @@ -17,7 +17,7 @@ <dependency> <groupId>com.xeiam.xchart</groupId> <artifactId>xchart</artifactId> - <version>2.3.1-SNAPSHOT</version> + <version>2.3.2-SNAPSHOT</version> </dependency> </dependencies> diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart04.java index de90dc2c0082b4f22c5c9dee30f2df201c38ffa1..74400142b1590d8c48cdcc6f3223735c26699d4b 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart04.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/bar/BarChart04.java @@ -46,8 +46,8 @@ public class BarChart04 implements ExampleChart { // Create Chart Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("XFactor vs. Age").xAxisTitle("Age").yAxisTitle("XFactor").build(); - chart.addSeries("male", new double[] { 10, 20, 30, 50 }, new double[] { 40, 30, 20, 60 }); chart.addSeries("female", new double[] { 10, 20, 30, 40, 50 }, new double[] { 50, 10, 20, 40, 35 }); + chart.addSeries("male", new double[] { 10, 20, 30, 50 }, new double[] { 40, 30, 20, 60 }); chart.getStyleManager().setYAxisMin(5); chart.getStyleManager().setYAxisMax(70); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java index 71bbcd01e8b752746f8c87ebe4b0162a7f8e4624..45c5697af8d02005da1a63aac417a8de8a294ec7 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java @@ -33,6 +33,7 @@ import com.xeiam.xchart.demo.charts.ExampleChart; * <li>Customizing the series style properties * <li>Scatter and Line overlay * <li>Logarithmic Y-Axis + * <li>An X-Axis min value clipping off the series */ public class LineChart05 implements ExampleChart { @@ -74,8 +75,8 @@ public class LineChart05 implements ExampleChart { chart.getStyleManager().setYAxisMin(0.01); chart.getStyleManager().setYAxisMax(1000); - chart.getStyleManager().setXAxisMin(-2); - chart.getStyleManager().setXAxisMax(8); + chart.getStyleManager().setXAxisMin(2); + chart.getStyleManager().setXAxisMax(7); return chart; } diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/standalone/SwingDemo.java b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/SwingDemo.java new file mode 100644 index 0000000000000000000000000000000000000000..960905adfc5488a7ac6c9a785035d35e1ba1e87f --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/SwingDemo.java @@ -0,0 +1,62 @@ +/** + * Copyright 2011 - 2014 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.standalone; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import com.xeiam.xchart.XChartPanel; +import com.xeiam.xchart.demo.charts.area.AreaChart01; + +/** + * Class showing how to integrate a chart into a Swing JFrame + * + * @author timmolter + */ +public class SwingDemo { + + /** + * Create the GUI and show it. For thread safety, this method should be invoked from the event dispatch thread. + */ + private static void createAndShowGUI() { + + // Create and set up the window. + JFrame frame = new JFrame("XChart Swing Demo"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + // Add content to the window. + JPanel chartPanel = new XChartPanel(new AreaChart01().getChart()); + frame.add(chartPanel); + + // Display the window. + frame.pack(); + frame.setVisible(true); + } + + public static void main(String[] args) { + + // Schedule a job for the event dispatch thread: + // creating and showing this application's GUI. + javax.swing.SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + + createAndShowGUI(); + } + }); + } +} diff --git a/xchart/pom.xml b/xchart/pom.xml index 091e9981c1c560127888f0f39c3be0c5ab459467..62e9feceb0a2beab0003524a55026187690382ac 100644 --- a/xchart/pom.xml +++ b/xchart/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>com.xeiam.xchart</groupId> <artifactId>xchart-parent</artifactId> - <version>2.3.1-SNAPSHOT</version> + <version>2.3.2-SNAPSHOT</version> </parent> <artifactId>xchart</artifactId> diff --git a/xchart/src/main/java/com/xeiam/xchart/StyleManager.java b/xchart/src/main/java/com/xeiam/xchart/StyleManager.java index 7940b399d3b31f89efc98341462f55359eacb77f..452f3096e3f71f6a028a0c76e981caeca3901b90 100644 --- a/xchart/src/main/java/com/xeiam/xchart/StyleManager.java +++ b/xchart/src/main/java/com/xeiam/xchart/StyleManager.java @@ -116,6 +116,7 @@ public class StyleManager { private Double xAxisMax; private Double yAxisMin; private Double yAxisMax; + private double axisTickSpaceRatio; // Chart Plot Area /////////////////////////////// private boolean isPlotGridLinesVisible; @@ -193,6 +194,7 @@ public class StyleManager { xAxisMax = null; yAxisMin = null; yAxisMax = null; + axisTickSpaceRatio = .95; // Chart Plot Area /////////////////////////////// isPlotGridLinesVisible = theme.isPlotGridLinesVisible(); @@ -847,6 +849,14 @@ public class StyleManager { return yAxisMax; } + public void setAxisTickSpaceRatio(double axisTickSpaceRatio) { + this.axisTickSpaceRatio = axisTickSpaceRatio; + } + + public double getAxisTickSpaceRatio() { + return axisTickSpaceRatio; + } + // Chart Plot Area /////////////////////////////// /** diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java b/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java index a26f144eaeb688ea644a3bcb227a9918c4248318..8bceebf4b0fa5db46b118d264e616c1a60b28c0d 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java @@ -28,17 +28,6 @@ public class Utils { } - /** - * Gets the percentage of working space allowed for tick marks - * - * @param workingSpace - * @return - */ - public static int getTickSpace(int workingSpace) { - - return (int) (workingSpace * 0.95); - } - /** * Gets the offset for the beginning of the tick marks * diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java index b290cef90208211a8f756bab91904e7644b6a50b..e163b67852d693c58f0f1216cc7457fc4560953c 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java @@ -15,10 +15,10 @@ */ package com.xeiam.xchart.internal.chartpart; +import java.util.ArrayList; import java.util.Date; import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; +import java.util.List; import com.xeiam.xchart.Series; import com.xeiam.xchart.internal.Utils; @@ -49,14 +49,14 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { private void calculate(ChartPainter chartPainter) { - // tick space - a percentage of the working space available for ticks, i.e. 95% - int tickSpace = Utils.getTickSpace(workingSpace); // in plot space + // tick space - a percentage of the working space available for ticks + int tickSpace = (int) (styleManager.getAxisTickSpaceRatio() * workingSpace); // in plot space // where the tick should begin in the working space in pixels int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); // get all categories - Set<Object> categories = new TreeSet<Object>(); + List<Object> categories = new ArrayList<Object>(); for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { Iterator<?> xItr = series.getXData().iterator(); @@ -71,7 +71,9 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.String) { x = xItr.next(); } - categories.add(x); + if (!categories.contains(x)) { + categories.add(x); + } } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java index 1984e44492e498e64d64e609ad11460b077c8eab..512b15e23d60cc3ba8ba6816070840b852c3c099 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java @@ -46,8 +46,8 @@ public class AxisTickDateCalculator extends AxisTickCalculator { private void calculate() { - // tick space - a percentage of the working space available for ticks, i.e. 95% - int tickSpace = Utils.getTickSpace(workingSpace); // in plot space + // tick space - a percentage of the working space available for ticks + int tickSpace = (int)(styleManager.getAxisTickSpaceRatio() * workingSpace); // in plot space // where the tick should begin in the working space in pixels int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java index 88ab25b5bbfe569ae09a4f763832fefbe84508ee..db3f0cbe9a31d15ec90d65c790681fa3882a62d9 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java @@ -53,8 +53,8 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { return; } - // tick space - a percentage of the working space available for ticks, i.e. 95% - int tickSpace = Utils.getTickSpace(workingSpace); // in plot space + // tick space - a percentage of the working space available for ticks + int tickSpace = (int) (styleManager.getAxisTickSpaceRatio() * workingSpace); // in plot space // where the tick should begin in the working space in pixels int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); @@ -109,7 +109,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { } // only add labels for the decades - if (Math.log10(j) % 1 == 0.0) { + if (Math.abs(Math.log10(j) % 1) < 0.00000001) { tickLabels.add(numberFormatter.formatNumber(j)); } else { diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java index e6d273f2043178ca1c3e76c421ce20c1bdef7f3b..85e296b640fdafd7f2014994818393e543f48ae4 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java @@ -53,8 +53,8 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { return; } - // tick space - a percentage of the working space available for ticks, i.e. 95% - int tickSpace = Utils.getTickSpace(workingSpace); // in plot space + // tick space - a percentage of the working space available for ticks + int tickSpace = (int)(styleManager.getAxisTickSpaceRatio() * workingSpace); // in plot space // where the tick should begin in the working space in pixels int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java index 41a31636130f79f4558837b55cc41d7a0d4ad1b5..7fc2e0e3e3d0d7d826e7f7713c9a8727dbe34465 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java @@ -18,12 +18,13 @@ package com.xeiam.xchart.internal.chartpart; import java.awt.Graphics2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; +import java.util.List; import com.xeiam.xchart.Series; +import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.internal.Utils; /** @@ -45,22 +46,26 @@ public class PlotContentBarChart extends PlotContent { public void paint(Graphics2D g) { Rectangle2D bounds = plot.getBounds(); + StyleManager styleManager = plot.getChartPainter().getStyleManager(); // X-Axis - int xTickSpace = Utils.getTickSpace((int) bounds.getWidth()); + int xTickSpace = (int) (styleManager.getAxisTickSpaceRatio() * bounds.getWidth()); int xLeftMargin = Utils.getTickStartOffset((int) bounds.getWidth(), xTickSpace); // Y-Axis - int yTickSpace = Utils.getTickSpace((int) bounds.getHeight()); + int yTickSpace = (int) (styleManager.getAxisTickSpaceRatio() * bounds.getHeight()); int yTopMargin = Utils.getTickStartOffset((int) bounds.getHeight(), yTickSpace); // get all categories - Set<Object> categories = new TreeSet<Object>(); + List<Object> categories = new ArrayList<Object>(); for (Series series : getChartPainter().getAxisPair().getSeriesMap().values()) { Iterator<?> xItr = series.getXData().iterator(); while (xItr.hasNext()) { - categories.add(xItr.next()); + Object object = xItr.next(); + if (!categories.contains(object)) { + categories.add(object); + } } } int numBars = categories.size(); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java index f70c8bd61ba69527e78711f9293881a4d02cc6fe..bf6ff687966e569c3ad820b67d79af463870ef35 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java @@ -25,6 +25,7 @@ import java.util.Date; import java.util.Iterator; import com.xeiam.xchart.Series; +import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.StyleManager.ChartType; import com.xeiam.xchart.internal.Utils; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; @@ -48,13 +49,17 @@ public class PlotContentLineChart extends PlotContent { public void paint(Graphics2D g) { Rectangle2D bounds = plot.getBounds(); + StyleManager styleManager = plot.getChartPainter().getStyleManager(); + + // this is for preventing the series to be drawn outside the plot area if min and max is overridden to fall inside the data range + g.setClip(bounds); // X-Axis - int xTickSpace = Utils.getTickSpace((int) bounds.getWidth()); + int xTickSpace = (int)(styleManager.getAxisTickSpaceRatio() * bounds.getWidth()); int xLeftMargin = Utils.getTickStartOffset((int) bounds.getWidth(), xTickSpace); // Y-Axis - int yTickSpace = Utils.getTickSpace((int) bounds.getHeight()); + int yTickSpace = (int)(styleManager.getAxisTickSpaceRatio() * bounds.getHeight()); int yTopMargin = Utils.getTickStartOffset((int) bounds.getHeight(), yTickSpace); for (Series series : getChartPainter().getAxisPair().getSeriesMap().values()) { @@ -98,10 +103,6 @@ public class PlotContentLineChart extends PlotContent { Iterator<?> xItr = xData.iterator(); Iterator<? extends Number> yItr = yData.iterator(); - Iterator<? extends Number> ebItr = null; - if (errorBars != null) { - ebItr = errorBars.iterator(); - } Path2D.Double path = null; @@ -112,7 +113,7 @@ public class PlotContentLineChart extends PlotContent { x = ((Number) xItr.next()).doubleValue(); // System.out.println(x); } - if (getChartPainter().getAxisPair().getXAxis().getAxisType() == AxisType.Date) { + else if (getChartPainter().getAxisPair().getXAxis().getAxisType() == AxisType.Date) { x = ((Date) xItr.next()).getTime(); // System.out.println(x); } @@ -134,12 +135,8 @@ public class PlotContentLineChart extends PlotContent { } double yOrig = next.doubleValue(); - double y = 0.0; - double eb = 0.0; - if (errorBars != null) { - eb = (Double) ebItr.next(); - } + double y = 0.0; // System.out.println(y); if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { @@ -208,7 +205,17 @@ public class PlotContentLineChart extends PlotContent { series.getMarker().paint(g, xOffset, yOffset); } - // paint errorbar + // paint errorbars + Iterator<? extends Number> ebItr = null; + if (errorBars != null) { + ebItr = errorBars.iterator(); + } + double eb = 0.0; + + if (errorBars != null) { + eb = (Double) ebItr.next(); + } + if (errorBars != null) { g.setColor(getChartPainter().getStyleManager().getErrorBarsColor());