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

refactored example classes and QuickChart

parent 61910313
Branches
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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();
}
......
......@@ -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;
}
}
/**
* 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();
}
}
/**
* 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;
}
}
......@@ -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();
......
......@@ -13,43 +13,69 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.examples;
package com.xeiam.xcharts;
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
* A convenience class for making Charts with one line of code.
*
* @author timmolter
*/
public class SwingChart4 {
public class QuickChart {
public static void main(String[] args) {
/**
* @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) {
// generates linear data
double[] yData1 = new double[] { 0.0, 0.0, 0.0, -10.0, 15.0, 15.0 };
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("Sample Chart");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.setChartTitleVisible(true);
chart.setChartLegendVisible(true);
chart.setAxisTitlesVisible(true);
chart.setChartTitle(chartTitle);
chart.setXAxisTitle(xTitle);
chart.setYAxisTitle(yTitle);
// Series
Series series1 = chart.addSeries("y=0", null, yData1);
series1.setMarker(SeriesMarker.NONE);
SwingHelper swingHelper = new SwingHelper(chart);
swingHelper.displayChart();
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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment