Skip to content
Snippets Groups Projects
Commit ccea5194 authored by Alex Nugent's avatar Alex Nugent
Browse files

added QuickXChart

parent e6b323bc
No related branches found
No related tags found
No related merge requests found
No preview for this file type
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;
}
}
......@@ -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);
}
});
}
......
......@@ -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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment