Skip to content
Snippets Groups Projects
Commit 2d943400 authored by Tim Molter's avatar Tim Molter
Browse files

cleaned up QuickChart, added an example

parent 5ba1abd2
No related branches found
No related tags found
No related merge requests found
...@@ -15,20 +15,34 @@ ...@@ -15,20 +15,34 @@
*/ */
package com.xeiam.xchart; package com.xeiam.xchart;
import java.util.Collection;
/** /**
* A convenience class for making Charts with one line of code * A convenience class for making Charts with one line of code
* *
* @author timmolter * @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 * Creates a Chart with default style
* @param xTitle *
* @param yTitle * @param chartTitle the Chart title
* @param seriesName * @param xTitle The X-Axis title
* @param xData * @param yTitle The Y-Axis title
* @param yData * @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 * @return a Chart Object
*/ */
public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) { public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) {
...@@ -42,18 +56,20 @@ public class QuickChart { ...@@ -42,18 +56,20 @@ public class QuickChart {
} }
/** /**
* @param chartTitle * Creates a Chart with multiple Series for the same X-Axis data with default style
* @param xTitle *
* @param yTitle * @param chartTitle the Chart title
* @param seriesNames * @param xTitle The X-Axis title
* @param xData * @param yTitle The Y-Axis title
* @param yData * @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 * @return a Chart Object
*/ */
public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) { public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) {
// Create Chart // Create Chart
Chart chart = new Chart(400, 280); Chart chart = new Chart(WIDTH, HEIGHT);
// Customize Chart // Customize Chart
chart.setTitle(chartTitle); chart.setTitle(chartTitle);
...@@ -75,4 +91,32 @@ public class QuickChart { ...@@ -75,4 +91,32 @@ public class QuickChart {
return chart; 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;
}
} }
/**
* 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");
}
}
...@@ -44,5 +44,4 @@ public class Example1 { ...@@ -44,5 +44,4 @@ public class Example1 {
BitmapEncoder.saveJPG(chart, "./Sample_Chart.jpg", 0.95f); BitmapEncoder.saveJPG(chart, "./Sample_Chart.jpg", 0.95f);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment