I just recently added support in XChart for making plots with error bars, which is what this post is about. The following demo, shows how to add error bars to an XChart chart. BTW, if you have any feature requests for XChart, please feel free to open a new issue on Github here. For more XChart exmaples go here.
XChart - Open Source Charting API
To use XChart, you first need to get the XChart jar, which is available here. If you use Maven, just add the following to your dependencies in pom.xml:<dependency> <groupId>com.xeiam</groupId> <artifactId>xchart</artifactId> <version>1.1.0</version> </dependency>
The XChart artifacts are currently hosted on the Xeiam Nexus repository here:
<repositories> <repository> <id>xchange-release</id> <releases/> <url>http://nexus.xeiam.com/content/repositories/releases</url> </repository> <repository> <id>xchange-snapshot</id> <snapshots/> <url>http://nexus.xeiam.com/content/repositories/snapshots/</url> </repository> </repositories>
Error Bars Example Code
package com.xeiam.xchart.example; import java.util.ArrayList; import java.util.Collection; import com.xeiam.xchart.Chart; 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.swing.SwingWrapper; /** * Create a Chart with error bars * * @author timmolter */ public class Example8 { public static void main(String[] args) { // generates data int size = 10; CollectionxData1 = new ArrayList (); Collection yData1 = new ArrayList (); Collection errorBars = new ArrayList (); for (int i = 0; i <= size; i++) { xData1.add(i); yData1.add(10 * Math.exp(-i)); errorBars.add(Math.random() + .3); } // Create Chart Chart chart = new Chart(600, 400); // Customize Chart chart.setChartTitleVisible(false); chart.setChartLegendVisible(false); chart.setAxisTitlesVisible(false); // Series 1 Series series1 = chart.addSeries("10^(-x)", xData1, yData1, errorBars); series1.setLineColor(SeriesColor.PURPLE); series1.setLineStyle(SeriesLineStyle.DASH_DASH); series1.setMarkerColor(SeriesColor.GREEN); series1.setMarker(SeriesMarker.SQUARE); new SwingWrapper(chart).displayChart(); } }
No comments:
Post a Comment