我最近发现JFreeChart用于生成简单的饼图。基本上我想显示带有标签值(以百分比表示)的馅饼,但它只显示没有值的标签名称。
这是我的代码:
package testPie;
// Import the Swing classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Import the JFreeChart classes
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.*;
import org.jfree.data.general.*;
public class First extends JPanel
{
// Holds the data
private DefaultPieDataset dataset = new DefaultPieDataset();
// Create a set of charts
private JFreeChart chart1;
private JFreeChart chart2;
private JFreeChart chart3;
private JFreeChart chart4;
// Create a set of panels that can show charts
private ChartPanel panel1;
private ChartPanel panel2;
private ChartPanel panel3;
private ChartPanel panel4;
public First()
{
// Initialize the dataset
dataset.setValue( "California", new Double(10.0));
dataset.setValue( "Arizona", new Double(8.0));
dataset.setValue( "New Mexico", new Double(8.0));
dataset.setValue( "Texas", new Double(40.0));
dataset.setValue( "Louisiana", new Double(8.0) );
dataset.setValue( "Mississippi", new Double(4.0));
dataset.setValue( "Alabama", new Double(2.0));
dataset.setValue( "Florida", new Double(20.0));
// Create the charts
chart1 = ChartFactory.createPieChart(
"Driving Time Spent Per State (Flat Pie Chart)", // The chart title
dataset, // The dataset for the chart
true, // Is a legend required?
true, // Use tooltips
false // Configure chart to generate URLs?
);
chart2 = ChartFactory.createPieChart(
"Driving Time Spent Per State (Exploded Pie Chart)", // The chart title
dataset, // The dataset for the chart
true, // Is a legend required?
true, // Use tooltips
false // Configure chart to generate URLs?
);
PiePlot plot = ( PiePlot )chart2.getPlot();
plot.setExplodePercent( 3, 0.25 );
chart3 = ChartFactory.createPieChart3D(
"Driving Time Spent Per State (3D Pie Chart)", // The chart title
dataset, // The dataset for the chart
true, // Is a legend required?
true, // Use tooltips
false // Configure chart to generate URLs?
);
chart4 = ChartFactory.createPieChart3D(
"Driving Time Spent Per State (3D with Transparency)", // The chart title
dataset, // The dataset for the chart
true, // Is a legend required?
true, // Use tooltips
false // Configure chart to generate URLs?
);
PiePlot3D plot4 = ( PiePlot3D )chart4.getPlot();
plot4.setForegroundAlpha( 0.6f );
// Create this panel
this.setLayout( new GridLayout( 2, 2 ) );
this.panel1 = new ChartPanel( chart1 );
this.panel2 = new ChartPanel( chart2 );
this.panel3 = new ChartPanel( chart3 );
this.panel4 = new ChartPanel( chart4 );
this.add( panel1 );
this.add( panel2 );
this.add( panel3 );
this.add( panel4 );
}
public static void main( String[] args )
{
JFrame frame = new JFrame( "My Trip Driving From CA to FL..." );
First chart = new First();
frame.getContentPane().add( chart, BorderLayout.CENTER );
frame.setSize( 640, 480 );
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
我只是关注this tutorial,但我无法显示每个标签的值。
答案 0 :(得分:5)
简单方法是使用旧版jfreechart
,例如1.0.1
。使用这个版本我得到了这个结果:
另一方面,使用最新版本的jfreechart
(例如1.0.19
)正在更改PieSectionLabelGenerator
。默认值仅包含数据集的key
。
为此,您需要为标准标签生成器创建自定义标签格式,如下所示:
PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} = {1}");
更改所有图表的标签生成器,如下所示:
plot.setLabelGenerator(labelGenerator);
有了这个,我得到了这个结果:
如果您需要%
,请使用标签格式{0} = {2}
代替{0} = {1}
,并显示California = 10%
。
这是完整的例子:
public class First extends JPanel {
private static final long serialVersionUID = 4400735172378226919L;
public First() {
final DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("California", new Double(10.0));
dataset.setValue("Arizona", new Double(8.0));
dataset.setValue("New Mexico", new Double(8.0));
dataset.setValue("Texas", new Double(40.0));
dataset.setValue("Louisiana", new Double(8.0));
dataset.setValue("Mississippi", new Double(4.0));
dataset.setValue("Alabama", new Double(2.0));
dataset.setValue("Florida", new Double(20.0));
// Create the custom label generator
final PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} = {1}");
// Create the charts
final JFreeChart chart1 = ChartFactory.createPieChart("Driving Time Spent Per State (Flat Pie Chart)", dataset, true, true, false);
final PiePlot plot1 = (PiePlot) chart1.getPlot();
plot1.setLabelGenerator(labelGenerator);
final JFreeChart chart2 = ChartFactory.createPieChart("Driving Time Spent Per State (Exploded Pie Chart)", dataset, true, true, false);
final PiePlot plot2 = (PiePlot) chart2.getPlot();
plot2.setExplodePercent("3", 0.25);
plot2.setLabelGenerator(labelGenerator);
final JFreeChart chart3 = ChartFactory.createPieChart3D("Driving Time Spent Per State (3D Pie Chart)", dataset, true, true, false);
final PiePlot3D plot3 = (PiePlot3D) chart3.getPlot();
plot3.setLabelGenerator(labelGenerator);
final JFreeChart chart4 = ChartFactory.createPieChart3D("Driving Time Spent Per State (3D with Transparency)", dataset, true, true, false);
final PiePlot3D plot4 = (PiePlot3D) chart4.getPlot();
plot4.setForegroundAlpha(0.6f);
plot4.setLabelGenerator(labelGenerator);
// Create this panel
this.setLayout(new GridLayout(2, 2));
final ChartPanel panel1 = new ChartPanel(chart1);
final ChartPanel panel2 = new ChartPanel(chart2);
final ChartPanel panel3 = new ChartPanel(chart3);
final ChartPanel panel4 = new ChartPanel(chart4);
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
}
public static void main(final String[] args) {
final JFrame frame = new JFrame("My Trip Driving From CA to FL...");
final First chart = new First();
frame.getContentPane().add(chart, BorderLayout.CENTER);
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}