From 30bc91379c49f0df0fbf3b63575c5f7a0dc3bf3e Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Tue, 19 Jul 2011 13:48:12 +0200 Subject: [PATCH] refactored example classes and QuickChart --- src/com/xeiam/examples/Example2.java | 4 +- .../{SwingChart2.java => Example3.java} | 26 ++- .../{SwingChart5.java => Example4.java} | 61 +++---- src/com/xeiam/examples/SwingChart3.java | 56 ------ src/com/xeiam/examples/SwingChart4.java | 55 ------ src/com/xeiam/swing/QuickChart.java | 169 ------------------ .../{SwingHelper.java => SwingWrapper.java} | 55 +++++- src/com/xeiam/xcharts/QuickChart.java | 81 +++++++++ 8 files changed, 176 insertions(+), 331 deletions(-) rename src/com/xeiam/examples/{SwingChart2.java => Example3.java} (64%) rename src/com/xeiam/examples/{SwingChart5.java => Example4.java} (51%) delete mode 100644 src/com/xeiam/examples/SwingChart3.java delete mode 100644 src/com/xeiam/examples/SwingChart4.java delete mode 100644 src/com/xeiam/swing/QuickChart.java rename src/com/xeiam/swing/{SwingHelper.java => SwingWrapper.java} (54%) create mode 100644 src/com/xeiam/xcharts/QuickChart.java diff --git a/src/com/xeiam/examples/Example2.java b/src/com/xeiam/examples/Example2.java index 9125583e..a7495933 100644 --- a/src/com/xeiam/examples/Example2.java +++ b/src/com/xeiam/examples/Example2.java @@ -15,7 +15,7 @@ */ package com.xeiam.examples; -import com.xeiam.swing.SwingHelper; +import com.xeiam.swing.SwingWrapper; import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.SeriesColor; @@ -56,7 +56,7 @@ public class Example2 { series1.setMarkerColor(SeriesColor.GREEN); series1.setMarker(SeriesMarker.SQUARE); - SwingHelper swingHelper = new SwingHelper(chart); + SwingWrapper swingHelper = new SwingWrapper(chart); swingHelper.displayChart(); } diff --git a/src/com/xeiam/examples/SwingChart2.java b/src/com/xeiam/examples/Example3.java similarity index 64% rename from src/com/xeiam/examples/SwingChart2.java rename to src/com/xeiam/examples/Example3.java index 41f841ce..5c4c2de1 100644 --- a/src/com/xeiam/examples/SwingChart2.java +++ b/src/com/xeiam/examples/Example3.java @@ -15,24 +15,25 @@ */ package com.xeiam.examples; -import com.xeiam.swing.SwingHelper; +import com.xeiam.swing.SwingWrapper; import com.xeiam.xcharts.Chart; -import com.xeiam.xcharts.series.Series; /** + * Create 14 different curves on one chart + * * @author timmolter */ -public class SwingChart2 { +public class Example3 { public static void main(String[] args) { // Create Chart - Chart chart = new Chart(800, 600); + Chart chart = new Chart(700, 500); for (int i = 1; i <= 14; i++) { // generates linear data - int b = 50; + int b = 20; double[] xData = new double[b + 1]; double[] yData = new double[b + 1]; for (int x = 0; x <= b; x++) { @@ -44,18 +45,13 @@ public class SwingChart2 { chart.setChartTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); - // chart.setChartTitleVisible(false); - // chart.setChartLegendVisible(false); - // chart.setAxisTitlesVisible(false); - - Series series = chart.addSeries("y=" + 2 * i + "x-" + i * b + "b", xData, yData); - // series.setLineColor(SeriesColor.PURPLE); - // series.setLineStyle(SeriesLineStyle.NONE); - // series.setMarkerColor(SeriesColor.GREEN); - // series.setMarker(SeriesMarker.NONE); + + String seriesName = "y=" + 2 * i + "x-" + i * b + "b"; + chart.addSeries(seriesName, xData, yData); + } - SwingHelper swingHelper = new SwingHelper(chart); + SwingWrapper swingHelper = new SwingWrapper(chart); swingHelper.displayChart(); } diff --git a/src/com/xeiam/examples/SwingChart5.java b/src/com/xeiam/examples/Example4.java similarity index 51% rename from src/com/xeiam/examples/SwingChart5.java rename to src/com/xeiam/examples/Example4.java index eb5d2bbf..986d822a 100644 --- a/src/com/xeiam/examples/SwingChart5.java +++ b/src/com/xeiam/examples/Example4.java @@ -15,53 +15,54 @@ */ package com.xeiam.examples; -import com.xeiam.swing.QuickChart; +import com.xeiam.swing.SwingWrapper; +import com.xeiam.xcharts.Chart; +import com.xeiam.xcharts.QuickChart; /** * @author timmolter */ -public class SwingChart5 { - - private static double[] getRandomWalk(int N) { - - double[] y = new double[N]; - for (int i = 1; i < y.length; i++) { - y[i] = y[i - 1] + Math.random() - .5; - } - - return y; - - } +public class Example4 { public static void main(String[] args) { int numRows = 2; int numCols = 2; + Chart[] charts = new Chart[numRows * numCols]; - QuickChart quickChart = new QuickChart(2, 2); - + int chartCount = 0; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { - quickChart.setChartPosition(i, j); - if (i == 1 && j == 1) { - double[][] y = new double[10][1000]; - String[] legend = new String[10]; - for (int k = 0; k < y.length; k++) { - y[k] = getRandomWalk(1000); - legend[k] = "" + k; - } - quickChart.setChart(i + "," + j, "X", "Y", null, y, legend); - } else if (i == 0 && j == 1) { - // nothing - } else { - quickChart.setChart(i + "," + j, "X", "Y", null, getRandomWalk(1000), null); - } + // double[][] yData2d = new double[10][1000]; + // for (int k = 0; k < yData2d.length; k++) { + // yData2d[k] = getRandomWalk(1000); + // } + // charts[chartCount++] = QuickChart.getChart(i + "," + j, "X", "Y", null, null, yData2d); + charts[chartCount++] = QuickChart.getChart(i + "," + j, "X", "Y", "random walk", null, getRandomWalk(1000)); } } - quickChart.display(); + SwingWrapper swingWrapper = new SwingWrapper(charts); + swingWrapper.displayChartMatrix(numRows, numCols); + + } + + /** + * Generates a set of random walk data + * + * @param numPoints + * @return + */ + private static double[] getRandomWalk(int numPoints) { + + double[] y = new double[numPoints]; + for (int i = 1; i < y.length; i++) { + y[i] = y[i - 1] + Math.random() - .5; + } + + return y; } } diff --git a/src/com/xeiam/examples/SwingChart3.java b/src/com/xeiam/examples/SwingChart3.java deleted file mode 100644 index de0087d3..00000000 --- a/src/com/xeiam/examples/SwingChart3.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2011 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.examples; - -import com.xeiam.swing.SwingHelper; -import com.xeiam.xcharts.Chart; -import com.xeiam.xcharts.series.Series; - -/** - * Demonstrated/Tests plotting horizontal and vertical lines - * - * @author timmolter - */ -public class SwingChart3 { - - public static void main(String[] args) { - - // generates linear data - double[] xData1 = new double[] { 0.0, 1.0, 2.0 }; - double[] yData1 = new double[] { 0.0, 0.0, 0.0 }; - double[] xData2 = new double[] { 0.0, 0.0, 0.0 }; - double[] yData2 = new double[] { 0.0, 1.0, 2.0 }; - - // Create Chart - Chart chart = new Chart(800, 600); - - // Customize Chart - chart.setChartTitle("Sample Chart"); - chart.setXAxisTitle("X"); - chart.setYAxisTitle("Y"); - // chart.setChartTitleVisible(false); - // chart.setChartLegendVisible(false); - // chart.setAxisTitlesVisible(false); - - // Series - Series series1 = chart.addSeries("y=0", xData1, yData1); - Series series2 = chart.addSeries("x=0", xData2, yData2); - - SwingHelper swingHelper = new SwingHelper(chart); - swingHelper.displayChart(); - } - -} diff --git a/src/com/xeiam/examples/SwingChart4.java b/src/com/xeiam/examples/SwingChart4.java deleted file mode 100644 index 43765a8d..00000000 --- a/src/com/xeiam/examples/SwingChart4.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright 2011 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.examples; - -import com.xeiam.swing.SwingHelper; -import com.xeiam.xcharts.Chart; -import com.xeiam.xcharts.series.Series; -import com.xeiam.xcharts.series.SeriesMarker; - -/** - * Demonstrated/Tests plotting horizontal and vertical lines - * - * @author timmolter - */ -public class SwingChart4 { - - public static void main(String[] args) { - - // generates linear data - double[] yData1 = new double[] { 0.0, 0.0, 0.0, -10.0, 15.0, 15.0 }; - - // Create Chart - Chart chart = new Chart(600, 300); - - // Customize Chart - chart.setChartTitle("Sample Chart"); - chart.setXAxisTitle("X"); - chart.setYAxisTitle("Y"); - chart.setChartTitleVisible(true); - chart.setChartLegendVisible(true); - chart.setAxisTitlesVisible(true); - - // Series - Series series1 = chart.addSeries("y=0", null, yData1); - series1.setMarker(SeriesMarker.NONE); - - SwingHelper swingHelper = new SwingHelper(chart); - swingHelper.displayChart(); - - } - -} diff --git a/src/com/xeiam/swing/QuickChart.java b/src/com/xeiam/swing/QuickChart.java deleted file mode 100644 index ac350b39..00000000 --- a/src/com/xeiam/swing/QuickChart.java +++ /dev/null @@ -1,169 +0,0 @@ -/** - * Copyright 2011 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.swing; - -import java.awt.GridLayout; - -import javax.swing.JFrame; -import javax.swing.JPanel; - -import com.xeiam.xcharts.Chart; -import com.xeiam.xcharts.JChartPanel; -import com.xeiam.xcharts.series.Series; -import com.xeiam.xcharts.series.SeriesMarker; - -public class QuickChart { - - Chart[] charts; - int numRows; - int numCols; - - int chartIdx = 0; - - int height = 300; - int width = 600; - - SeriesMarker seriesMarker = SeriesMarker.NONE; - - boolean chartTitleVisible = true; - boolean chartLegendVisible = true; - boolean axisTitlesVisible = true; - - public QuickChart(int numRows, int numCols) { - charts = new Chart[numRows * numCols]; - this.numRows = numRows; - this.numCols = numCols; - } - - public void setChartPosition(int row, int col) { - chartIdx = row * numCols + col; - System.out.println("chartIdx set=" + chartIdx); - } - - public void setChartSize(int width, int height) { - this.height = height; - this.width = width; - } - - public void setSeriesMarker(SeriesMarker seriesMarker) { - this.seriesMarker = seriesMarker; - } - - public void setChart(String chartTitle, String xTitle, String yTitle, double[] x, double[] y, String legend) { - // Create Chart - Chart chart = new Chart(width, height); - - // Customize Chart - chart.setChartTitle(chartTitle); - chart.setXAxisTitle(xTitle); - chart.setYAxisTitle(yTitle); - chart.setChartTitleVisible(chartTitleVisible); - chart.setChartLegendVisible(chartLegendVisible); - chart.setAxisTitlesVisible(axisTitlesVisible); - // Series - Series series; - if (legend != null) { - series = chart.addSeries(legend, x, y); - } else { - chart.setChartLegendVisible(false); - series = chart.addSeries(" ", x, y); - } - - series.setMarker(seriesMarker); - - charts[chartIdx] = chart; - } - - public void setChart(String chartTitle, String xTitle, String yTitle, double[] x, double[][] y, String[] legend) { - // Create Chart - Chart chart = new Chart(width, height); - - // Customize Chart - chart.setChartTitle(chartTitle); - chart.setXAxisTitle(xTitle); - chart.setYAxisTitle(yTitle); - chart.setChartTitleVisible(chartTitleVisible); - chart.setChartLegendVisible(chartLegendVisible); - chart.setAxisTitlesVisible(axisTitlesVisible); - // Series - for (int i = 0; i < y.length; i++) { - Series series; - if (legend != null) { - series = chart.addSeries(legend[i], x, y[i]); - } else { - chart.setChartLegendVisible(false); - series = chart.addSeries(" " + i, x, y[i]); - } - series.setMarker(seriesMarker); - } - - charts[chartIdx] = chart; - } - - public void display() { - // Schedule a job for the event-dispatching thread: - // creating and showing this application's GUI. - javax.swing.SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - // Create and set up the window. - JFrame frame = new JFrame("XChart"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.getContentPane().setLayout(new GridLayout(numRows, numCols)); - - for (int i = 0; i < charts.length; i++) { - - if (charts[i] != null) { - JPanel chartPanel = new JChartPanel(charts[i]); - frame.getContentPane().add(chartPanel); - } else { - JPanel chartPanel = new JPanel(); - frame.getContentPane().add(chartPanel); - } - - } - - // frame.setContentPane(newContentPane); - - // Display the window. - frame.pack(); - frame.setVisible(true); - } - }); - } - - /** - * @param chartTitleVisible the chartTitleVisible to set - */ - public void setChartTitleVisible(boolean chartTitleVisible) { - this.chartTitleVisible = chartTitleVisible; - } - - /** - * @param chartLegendVisible the chartLegendVisible to set - */ - public void setChartLegendVisible(boolean chartLegendVisible) { - this.chartLegendVisible = chartLegendVisible; - } - - /** - * @param axisTitlesVisible the axisTitlesVisible to set - */ - public void setAxisTitlesVisible(boolean axisTitlesVisible) { - this.axisTitlesVisible = axisTitlesVisible; - } - -} diff --git a/src/com/xeiam/swing/SwingHelper.java b/src/com/xeiam/swing/SwingWrapper.java similarity index 54% rename from src/com/xeiam/swing/SwingHelper.java rename to src/com/xeiam/swing/SwingWrapper.java index 59f3235a..4eac453b 100644 --- a/src/com/xeiam/swing/SwingHelper.java +++ b/src/com/xeiam/swing/SwingWrapper.java @@ -15,6 +15,8 @@ */ package com.xeiam.swing; +import java.awt.GridLayout; + import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JPanel; @@ -25,19 +27,32 @@ import com.xeiam.xcharts.JChartPanel; /** * @author timmolter */ -public class SwingHelper { +public class SwingWrapper { Chart[] charts; - public SwingHelper(Chart chart) { + /** + * Constructor + * + * @param chart + */ + public SwingWrapper(Chart chart) { this.charts = new Chart[1]; charts[0] = chart; } - public SwingHelper(Chart[] charts) { + /** + * Constructor + * + * @param charts + */ + public SwingWrapper(Chart[] charts) { this.charts = charts; } + /** + * Display the chart in a Swing JFrame + */ public void displayChart() { // Schedule a job for the event-dispatching thread: @@ -55,7 +70,39 @@ public class SwingHelper { frame.getContentPane().add(chartPanel); } - // frame.setContentPane(newContentPane); + // Display the window. + frame.pack(); + frame.setVisible(true); + } + }); + } + + /** + * Display the chart in a Swing JFrame + */ + public void displayChartMatrix(final int numRows, final int numColumns) { + + // Schedule a job for the event-dispatching thread: + // creating and showing this application's GUI. + javax.swing.SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + // Create and set up the window. + JFrame frame = new JFrame("XChart"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new GridLayout(numRows, numColumns)); + + for (int i = 0; i < charts.length; i++) { + + if (charts[i] != null) { + JPanel chartPanel = new JChartPanel(charts[i]); + frame.getContentPane().add(chartPanel); + } else { + JPanel chartPanel = new JPanel(); + frame.getContentPane().add(chartPanel); + } + + } // Display the window. frame.pack(); diff --git a/src/com/xeiam/xcharts/QuickChart.java b/src/com/xeiam/xcharts/QuickChart.java new file mode 100644 index 00000000..bf89137c --- /dev/null +++ b/src/com/xeiam/xcharts/QuickChart.java @@ -0,0 +1,81 @@ +/** + * Copyright 2011 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.xcharts; + +import com.xeiam.xcharts.series.Series; +import com.xeiam.xcharts.series.SeriesMarker; + +/** + * A convenience class for making Charts with one line of code. + * + * @author timmolter + */ +public class QuickChart { + + /** + * @param chartTitle + * @param xTitle + * @param yTitle + * @param seriesName + * @param xData + * @param yData + * @return + */ + public static Chart getChart(String chartTitle, String xTitle, String yTitle, String seriesName, double[] xData, double[] yData) { + + double[][] yData2d = { yData }; + if (seriesName == null) { + return getChart(chartTitle, xTitle, yTitle, null, xData, yData2d); + } else { + return getChart(chartTitle, xTitle, yTitle, new String[] { seriesName }, xData, yData2d); + } + } + + /** + * @param chartTitle + * @param xTitle + * @param yTitle + * @param seriesNames + * @param xData + * @param yData + * @return + */ + public static Chart getChart(String chartTitle, String xTitle, String yTitle, String[] seriesNames, double[] xData, double[][] yData) { + + // Create Chart + Chart chart = new Chart(600, 300); + + // Customize Chart + chart.setChartTitle(chartTitle); + chart.setXAxisTitle(xTitle); + chart.setYAxisTitle(yTitle); + + // Series + for (int i = 0; i < yData.length; i++) { + Series series; + if (seriesNames != null) { + series = chart.addSeries(seriesNames[i], xData, yData[i]); + } else { + chart.setChartLegendVisible(false); + series = chart.addSeries(" " + i, xData, yData[i]); + } + series.setMarker(SeriesMarker.NONE); + } + + return chart; + } + +} -- GitLab