From 2d9434001aafed5ae5a2d6b2c28dcf37cfebb7af Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Thu, 10 Jan 2013 22:37:06 +0100 Subject: [PATCH] cleaned up QuickChart, added an example --- .../java/com/xeiam/xchart/QuickChart.java | 72 +++++++++++++++---- .../com/xeiam/xchart/example/Example0.java | 45 ++++++++++++ .../com/xeiam/xchart/example/Example1.java | 1 - 3 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/xeiam/xchart/example/Example0.java diff --git a/src/main/java/com/xeiam/xchart/QuickChart.java b/src/main/java/com/xeiam/xchart/QuickChart.java index 28c16461..75931dec 100644 --- a/src/main/java/com/xeiam/xchart/QuickChart.java +++ b/src/main/java/com/xeiam/xchart/QuickChart.java @@ -15,20 +15,34 @@ */ package com.xeiam.xchart; +import java.util.Collection; + /** * A convenience class for making Charts with one line of code * * @author timmolter */ -public class QuickChart { +public final class QuickChart { + + private final static int WIDTH = 600; + private final static int HEIGHT = 400; + + /** + * private Constructor + */ + private QuickChart() { + + } /** - * @param chartTitle - * @param xTitle - * @param yTitle - * @param seriesName - * @param xData - * @param yData + * Creates a Chart with default style + * + * @param chartTitle the Chart title + * @param xTitle The X-Axis title + * @param yTitle The Y-Axis title + * @param seriesNames The name of the series + * @param xData An array containing the X-Axis data + * @param yData An array containing Y-Axis data * @return a Chart Object */ public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) { @@ -42,18 +56,20 @@ public class QuickChart { } /** - * @param chartTitle - * @param xTitle - * @param yTitle - * @param seriesNames - * @param xData - * @param yData + * Creates a Chart with multiple Series for the same X-Axis data with default style + * + * @param chartTitle the Chart title + * @param xTitle The X-Axis title + * @param yTitle The Y-Axis title + * @param seriesNames An array of the name of the multiple series + * @param xData An array containing the X-Axis data + * @param yData An array of double arrays containing multiple Y-Axis data * @return a Chart Object */ public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) { // Create Chart - Chart chart = new Chart(400, 280); + Chart chart = new Chart(WIDTH, HEIGHT); // Customize Chart chart.setTitle(chartTitle); @@ -75,4 +91,32 @@ public class QuickChart { return chart; } + /** + * Creates a Chart with default style + * + * @param chartTitle the Chart title + * @param xTitle The X-Axis title + * @param yTitle The Y-Axis title + * @param seriesNames The name of the series + * @param xData A Collection containing the X-Axis data + * @param yData A Collection containing Y-Axis data + * @return a Chart Object + */ + public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, Collection<Number> xData, Collection<Number> yData) { + + // Create Chart + Chart chart = new Chart(WIDTH, HEIGHT); + + // Customize Chart + chart.setTitle(chartTitle); + chart.setXAxisTitle(xTitle); + chart.setYAxisTitle(yTitle); + + Series series = chart.addSeries(seriesName, xData, yData); + series.setMarker(SeriesMarker.NONE); + + return chart; + + } + } diff --git a/src/test/java/com/xeiam/xchart/example/Example0.java b/src/test/java/com/xeiam/xchart/example/Example0.java new file mode 100644 index 00000000..142f568d --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example0.java @@ -0,0 +1,45 @@ +/** + * 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.example; + +import com.xeiam.xchart.BitmapEncoder; +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.QuickChart; +import com.xeiam.xchart.SwingWrapper; + +/** + * Creates a simple Chart using {@link com.xeiam.xchart.QuickChart} + * + * @author timmolter + */ +public class Example0 { + + public static void main(String[] args) throws Exception { + + double[] xData = new double[] { 0.0, 1.0, 2.0 }; + double[] yData = new double[] { 2.0, 1.0, 0.0 }; + + // Create Chart + Chart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", xData, yData); + + // Show it + new SwingWrapper(chart).displayChart(); + + // Save it + BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); + + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example1.java b/src/test/java/com/xeiam/xchart/example/Example1.java index 61236ebe..bf0ade23 100644 --- a/src/test/java/com/xeiam/xchart/example/Example1.java +++ b/src/test/java/com/xeiam/xchart/example/Example1.java @@ -44,5 +44,4 @@ public class Example1 { BitmapEncoder.saveJPG(chart, "./Sample_Chart.jpg", 0.95f); } - } -- GitLab