diff --git a/dist/xchart-1.0.0.jar b/dist/xchart-1.0.0.jar index 975bb4a371b0e9f5d73755946336444a7e9f1b5d..2daab78ba513d870852ff4ead846fa40ac904b95 100644 Binary files a/dist/xchart-1.0.0.jar and b/dist/xchart-1.0.0.jar differ diff --git a/src/com/xeiam/swing/QuickXChart.java b/src/com/xeiam/swing/QuickXChart.java new file mode 100644 index 0000000000000000000000000000000000000000..47c78698403fad051f24ad1fa10a8f9bd65b6316 --- /dev/null +++ b/src/com/xeiam/swing/QuickXChart.java @@ -0,0 +1,154 @@ +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 QuickXChart { + + 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 QuickXChart(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/SwingHelper.java index 8ec2783d969202b9d844d9ae1da22a1ece446f73..09076b7626e86105e06b3ac01cc473edaf11a1ad 100644 --- a/src/com/xeiam/swing/SwingHelper.java +++ b/src/com/xeiam/swing/SwingHelper.java @@ -20,35 +20,27 @@ public class SwingHelper { this.charts = charts; } - /** - * Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread. - */ - private void createAndShowGUI() { - - // Create and set up the window. - JFrame frame = new JFrame("XChart"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); - - for (int i = 0; i < charts.length; i++) { - JPanel chartPanel = new JChartPanel(charts[i]); - frame.getContentPane().add(chartPanel); - } - - // frame.setContentPane(newContentPane); - - // Display the window. - frame.pack(); - frame.setVisible(true); - } - public void displayChart() { // 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() { - createAndShowGUI(); + // Create and set up the window. + JFrame frame = new JFrame("XChart"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); + + for (int i = 0; i < charts.length; i++) { + JPanel chartPanel = new JChartPanel(charts[i]); + frame.getContentPane().add(chartPanel); + } + + // frame.setContentPane(newContentPane); + + // Display the window. + frame.pack(); + frame.setVisible(true); } }); } diff --git a/src/com/xeiam/xcharts/example/SwingChart5.java b/src/com/xeiam/xcharts/example/SwingChart5.java index 97297f2ee21edf74e4205726e33105687aa3cdca..edd12ae4d52c7d118ad76ef7511750206fdd1ce3 100644 --- a/src/com/xeiam/xcharts/example/SwingChart5.java +++ b/src/com/xeiam/xcharts/example/SwingChart5.java @@ -15,10 +15,7 @@ */ package com.xeiam.xcharts.example; -import com.xeiam.swing.SwingHelper; -import com.xeiam.xcharts.Chart; -import com.xeiam.xcharts.series.Series; -import com.xeiam.xcharts.series.SeriesMarker; +import com.xeiam.swing.QuickXChart; /** * Demonstrated/Tests plotting horizontal and vertical lines @@ -27,53 +24,46 @@ import com.xeiam.xcharts.series.SeriesMarker; */ public class SwingChart5 { - private static void createAndShowGUI() { - - Chart[] charts = new Chart[3]; - for (int i = 0; i < charts.length; i++) { - charts[i] = getRandomWalkChart(1000); - } - - SwingHelper swingHelper = new SwingHelper(charts); - swingHelper.displayChart(); - - } - - private static Chart getRandomWalkChart(int N) { + 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; } - // Create Chart - Chart chart = new Chart(600, 300); + return y; + + } - // Customize Chart - chart.setChartTitle("Random Walk"); - chart.setXAxisTitle("X"); - chart.setYAxisTitle("Y"); - chart.setChartTitleVisible(true); - chart.setChartLegendVisible(true); - chart.setAxisTitlesVisible(true); + public static void main(String[] args) { - // Series - Series series1 = chart.addSeries("y=0", null, y); - series1.setMarker(SeriesMarker.NONE); + int numRows = 2; + int numCols = 2; - return chart; + QuickXChart quickChart = new QuickXChart(2, 2); - } + for (int i = 0; i < numRows; i++) { + for (int j = 0; j < numCols; j++) { + quickChart.setChartPosition(i, j); + if (i == 1 && j == 1) { - public static void main(String[] args) { + 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); + } - // 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() { - createAndShowGUI(); } - }); + } + + quickChart.display(); + } }