I'd like to display a numbwr of JFreeCharts in a grid like structure (JTable/JXTable) , essentially 1 chart per cell, 1 chart per row in a vertical manner. The chart(s) will be getting dynamic updates. One post that I read looked like a graphic was being generated from the chart and that graphic was in turn inserted into the cell. My search thus far suggests that a custom TableCellRenderer might do the trick but I'm coming up empty on how to handle it. Any help would be greatly appreciated
Thanks in advance - dave
Insert a JFreeChart into a JTable cell
-
- Posts: 2
- Joined: Mon Jul 27, 2009 9:28 pm
- antibot: No, of course not.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Insert a JFreeChart into a JTable cell
Yes, a custom cell renderer is exactly what you need. I've done this a few times (it isn't a lot of code) but I don't seem to have any examples around sorry...
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Re: Insert a JFreeChart into a JTable cell
Code: Select all
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
/**
*
* @author NDUNN
* @date Jul 28, 2009
*/
public class JTableExample {
public JTableExample() {
JFreeChart chart1 = createChart(createSampleDataset());
JFreeChart chart2 = chart1; // Replace with a different thing
final JFreeChart[][] charts = {
{chart1, chart2}
};
String[] titles = {chart1.getTitle().getText(),
chart2.getTitle().getText()
};
TableModel model = new DefaultTableModel(charts, titles);
JTable table = new JTable(model) {
public TableCellRenderer getCellRenderer(int row, int column) {
return new JFreeChartRenderer(charts[row][column]);
}
};
final int WIDTH = 640;
final int HEIGHT = 480;
table.setPreferredSize(new Dimension(WIDTH, HEIGHT));
table.setRowHeight(HEIGHT);
JFrame frame = new JFrame("Testing charts in JTable");
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* From http://www.java2s.com/Code/Java/Chart/JFreeChartPieChart3DDemo1.htm
*
* Creates a sample dataset for the demo.
*
* @return A sample dataset.
*/
private PieDataset createSampleDataset() {
final DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Java", new Double(43.2));
result.setValue("Visual Basic", new Double(10.0));
result.setValue("C/C++", new Double(17.5));
result.setValue("PHP", new Double(32.5));
result.setValue("Perl", new Double(1.0));
return result;
}
/**
* From http://www.java2s.com/Code/Java/Chart/JFreeChartPieChart3DDemo1.htm
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private JFreeChart createChart(final PieDataset dataset) {
final JFreeChart chart = ChartFactory.createPieChart3D(
"Pie Chart 3D Demo 1", // chart title
dataset, // data
true, // include legend
true,
false
);
final PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
plot.setNoDataMessage("No data to display");
return chart;
}
public static void main(String[] args) {
JTableExample example = new JTableExample();
}
class JFreeChartRenderer extends DefaultTableCellRenderer {
private JFreeChart chart;
private ChartPanel panel;
public JFreeChartRenderer(JFreeChart chart) {
this.chart = chart;
panel = new ChartPanel(chart);
}
public Component getTableCellRendererComponent (JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
return panel;
}
}
}