net.maizegenetics.analysis.chart.HistogramPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tassel Show documentation
Show all versions of tassel Show documentation
TASSEL is a software package to evaluate traits associations, evolutionary patterns, and linkage
disequilibrium.
The newest version!
package net.maizegenetics.analysis.chart;
import net.maizegenetics.util.TableReport;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2004
* Company:
* @author Ed Buckler
* @version 1.0
*/
//todo catch errors on bins empty issue
public class HistogramPanel extends BasicChartPanel {
BorderLayout borderLayout1 = new BorderLayout();
IntervalXYDataset dataset;
ChartPanel chartPanel;
TableReport theTable;
JPanel controlPanel = new JPanel();
JComboBox series1ComboBox;
JComboBox series2ComboBox;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField binsTextField = new JTextField();
JLabel jLabel3 = new JLabel();
String[] columnNames;
int bins=25;
GridBagLayout gridBagLayout1 = new GridBagLayout();
public HistogramPanel(TableReport theTable) {
this.theTable=theTable;
try {
Object[] colNames = theTable.getTableColumnNames();
columnNames = new String[colNames.length+1];
columnNames[0] = "None";
for (int i = 1; i < columnNames.length; i++) {
columnNames[i] = (String) colNames[i - 1];
}
dataset = null;
chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
chartPanel.setMouseZoomable(true, false);
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public HistogramPanel(TableReport theTable, int series1, int series2, int bins) {
//this constructor is for the non-interactive version
this.theTable=theTable;
try {
this.bins=bins;
dataset = createDataset(series1, series2);
chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
this.setVisible(true);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.setLayout(borderLayout1);
controlPanel.setLayout(gridBagLayout1);
jLabel1.setText("Series 2");
jLabel2.setText("Series 1");
series1ComboBox = new JComboBox(columnNames);
series2ComboBox = new JComboBox(columnNames);
binsTextField.setText(""+bins);
binsTextField.addCaretListener(new javax.swing.event.CaretListener() {
public void caretUpdate(CaretEvent e) {
binsTextField_caretUpdate(e);
}
});
binsTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
binsTextField_actionPerformed(e);
}
});
jLabel3.setText("Bins");
series1ComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
series1ComboBox_actionPerformed(e);
}
});
series2ComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
series2ComboBox_actionPerformed(e);
}
});
this.add(chartPanel, BorderLayout.CENTER);
this.add(controlPanel, BorderLayout.NORTH);
controlPanel.add(jLabel2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 3, -4));
controlPanel.add(binsTextField, new GridBagConstraints(5, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 4), 23, 0));
controlPanel.add(jLabel3, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 6, -1));
controlPanel.add(series1ComboBox, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 84, 0));
controlPanel.add(series2ComboBox, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 80, 0));
controlPanel.add(jLabel1, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 3, -4));
}
/**
* Creates a sample {@link HistogramDataset}.
*
* @return The dataset.
*/
IntervalXYDataset createDataset(int series1Column, int series2Column) {
final HistogramDataset dataset = new HistogramDataset();
dataset.setType(HistogramType.FREQUENCY);
double[] series1Data, series2Data;
Object[] theNames = theTable.getTableColumnNames();
if(series1Column>=0) {
series1Data = getFilteredNumericData(series1Column);
if(series1Data.length>0) dataset.addSeries( (String) theNames[series1Column], series1Data, bins);
}
if(series2Column>=0) {
series2Data = getFilteredNumericData(series2Column);
if(series2Data.length>0) dataset.addSeries( (String) theNames[series2Column], series2Data, bins);
}
return dataset;
}
double[] getFilteredNumericData(int column) {
int countGood=0;
int numRows = (int) theTable.getRowCount();
double[] tempData=new double[numRows];
for(int i=0; i