Reputation: 1
Im new to using APIs. I am trying to get a chart showing on a panel. I have already set up the libs and they are working. The API im using is JFreeCharts.
I made a class called JChart:
public class JChart extends JFrame
{
private static final long serialVersionUID = 1L;
public JChart(String applicationTitle, String chartTitle)
{
// This will create the dataset
PieDataset dataset = createDataset();
// based on the dataset we create the chart
JFreeChart chart = createChart(dataset, chartTitle);
// we put the chart into a panel
ChartPanel chartPanel = new ChartPanel(chart);
// default size
chartPanel.setPreferredSize(new java.awt.Dimension(200, 150));
}
/**
* Creates a sample dataset
*/
private PieDataset createDataset() {
DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Linux", 29);
result.setValue("Mac", 20);
result.setValue("Windows", 51);
return result;
}
/**
* Creates a chart
*/
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
}
In my form I have a method:
private void LoadMyGraphs()
{
JChart chart = new JChart("name", "title");
myGraphPanel.add(chart);
}
I am getting no errors, but the panel does not change. I can changed the background color of the panel so I know nothing is wring in that respect. Any info would be amazing, thanks!
Upvotes: 0
Views: 338
Reputation: 205765
At some point you must add the ChartPanel
to a container in the enclosing frame, e.g.
this.add(chartPanel);
As an aside, also consider overriding getPreferredSize()
, as shown here and suggested here. See also Initial Threads. More examples are cited here.
Upvotes: 1