问题
JFreeChart seems to be working, except for all the text. It just doesn't show up at all, and I've no idea why. I attached a picture of a window with a pie graph that I got from a tutorial site. As you can see, the text isn't visible. (sorry my twitter feed was really long)
Thanks

Edit:
Here is the code that generates the above graph:
package analyzer_main;
import java.awt.Font;
public class FloatChart extends Composite implements Screen {
JFreeChart floatChart;
public FloatChart(Composite parent, int style){
super(parent,style);
createContents();
}
private void createContents(){
this.setLayout(new FormLayout());
floatChart = createChart(createDataset());
ChartComposite chartComposite = new ChartComposite(this,SWT.NONE,floatChart, true);
FormData fd_chartComposite = new FormData();
fd_chartComposite.left = new FormAttachment(0);
fd_chartComposite.right = new FormAttachment(100,0);
fd_chartComposite.top = new FormAttachment(0);
fd_chartComposite.bottom= new FormAttachment(100,0);
chartComposite.setLayoutData(fd_chartComposite);
}
/** * Creates the Dataset for the Pie chart */
private PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", new Double(43.2));
dataset.setValue("Two", new Double(10.0));
dataset.setValue("Three", new Double(27.5));
dataset.setValue("Four", new Double(17.5));
dataset.setValue("Five", new Double(11.0));
dataset.setValue("Six", new Double(19.4));
return dataset;
}
private JFreeChart createChart(PieDataset dataset) {
JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart
// title
dataset, // data
true, // include legend
true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setNoDataMessage("No data available");
plot.setCircular(false);
plot.setLabelGap(0.02);
return chart;
}
@Override
public void Load() {
}
}
As you can see, it's pretty much the same as from the tutorial.
回答1:
I was having the same problem using Linux Mint 11, Eclipse, and JFreeChart 1.0.14. I found that backing up to 1.0.13 resolved the problem.
回答2:
First of all, like Kevin Stembridge said, check your JDK. You are using Ubuntu, then try this command in a terminal:
sudo update-alternatives --config java
You can see the JDKs installed on your system and the JDK (is selected at left with a *
) you are using for this project.
If you have the Oracle JDK (from package java-6-sun
) and OpenJDK (the openjdk-6-jdk
package), try to select the Oracle JDK, because the OpenJDK has some graphical differences from the Oracle JDK and maybe these are the reason of this strange behavior on your JFreeChart. Select the Oracle JDK, recompile the Java project, and see if something in your JFreeChart is changed.
Check this Ubuntu Help page for details about Java, or read this old question of SO, or this page if you want to install the latest Oracle JDK.
About the code, it's very similar as from the tutorial, like you said. I have tested your code on Ubuntu, using Eclipse and Oracle JDK 6. The result is this:

adding the chart to a panel, editing only the createContents
method (and commenting the ChartComposite
parts, because I don't know what is ChartComposite
), with this code:
private void createContents(){
//this.setLayout(new FormLayout());
floatChart = createChart(createDataset());
ChartPanel chartPanel = new ChartPanel(floatChart);
// default size
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
// add it to our application
setContentPane(chartPanel);
/*ChartComposite chartComposite = new ChartComposite(this,SWT.NONE,floatChart, true);
FormData fd_chartComposite = new FormData();
fd_chartComposite.left = new FormAttachment(0);
fd_chartComposite.right = new FormAttachment(100,0);
fd_chartComposite.top = new FormAttachment(0);
fd_chartComposite.bottom= new FormAttachment(100,0);
chartComposite.setLayoutData(fd_chartComposite);*/
}
Then, my advice is to review all your code used for the GUI I saw in your screenshot, and you should solve the problem.
Comment this answer if you have questions or problems.
来源:https://stackoverflow.com/questions/8577680/why-is-there-no-text-in-jfreechart