Newer
Older
* Copyright 2015-2016 Knowm Inc. (http://knowm.org) and contributors.
* Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors.
*
* 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.
*/
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
* A convenience class used to display a Chart in a barebones Swing application
private String windowTitle = "XChart";
private int numRows;
private int numColumns;
/**
* Constructor
this.charts.add(chart);
timmolter
committed
/**
* Constructor - The number of rows and columns will be calculated automatically Constructor
timmolter
committed
* @param charts
*/
timmolter
committed
this.charts = charts;
this.numRows = (int) (Math.sqrt(charts.size()) + .5);
this.numColumns = (int) ((double) charts.size() / this.numRows + 1);
}
/**
* Constructor
* @param charts
timmolter
committed
* @param numRows - the number of rows
* @param numColumns - the number of columns
public SwingWrapper(List<T> charts, int numRows, int numColumns) {
this.charts = charts;
this.numRows = numRows;
this.numColumns = numColumns;
}
/**
* Display the chart in a Swing JFrame
* @param windowTitle the title of the window
*/
public JFrame displayChart(String windowTitle) {
this.windowTitle = windowTitle;
return displayChart();
}
public JFrame displayChart() {
// Create and set up the window.
final JFrame frame = new JFrame(windowTitle);
// 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() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel chartPanel = new XChartPanel<T>(charts.get(0));
frame.add(chartPanel);
// Display the window.
frame.pack();
frame.setVisible(true);
}
});
/**
* Display the charts in a Swing JFrame
* @param windowTitle the title of the window
* @return the JFrame
*/
public JFrame displayChartMatrix(String windowTitle) {
this.windowTitle = windowTitle;
return displayChartMatrix();
}
public JFrame displayChartMatrix() {
// Create and set up the window.
final JFrame frame = new JFrame(windowTitle);
// 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() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(numRows, numColumns));
if (chart != null) {
JPanel chartPanel = new JPanel();
frame.getContentPane().add(chartPanel);
}
// Display the window.
frame.pack();
frame.setVisible(true);
}
});