饼形图(ChartAction)
(1)需要传入的参数
title:eg:“农药企业销售收入城市分布图”
数组key:eg:“{"济南","泰安","滨州","菏泽","聊城"}”
数组value:eg:“{47000,38000,31000,29000,25000}”
(2)效果图如下:
(3)代码:
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
import com.opensymphony.xwork2.ActionSupport;
//生成饼形图
public class ChartAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private JFreeChart chart;
// 必须提供 getChart() 方法,且由该方法返回 JFreeChart 对象
public JFreeChart getChart() {
String title="农药企业销售收入地市分布图";
chart = ChartFactory.createPieChart3D(//调用JFreeChart工具类的静态方法生成饼图
title,
// 图表标题
getDataSet(), // 数据
true, // 是否显示图例
false, // 是否显示工具提示
false // 是否生成 URL
);
// 重新设置图表标题,改变字体
chart .setTitle(new TextTitle(title, new Font("黑体", Font.ITALIC,
22)));
// 取得统计图表的第一个图例
LegendTitle legend = chart.getLegend(0);
// 修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
// 获得饼图的 Plot 对象
PiePlot plot = (PiePlot) chart.getPlot();
plot.setStartAngle(290);//设置饼图各个板块的位置 默认值是90
plot.setDirection(Rotation.CLOCKWISE);//设置显示方式
// 设置饼图各部分的标签字体
plot.setLabelFont(new Font("隶书", Font.BOLD, 18));
// 设定背景透明度(0-1.0 之间)
plot.setBackgroundAlpha(0.9f);
// 设定前景透明度(0-1.0 之间)
plot.setForegroundAlpha(0.50f);
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
return chart;
}
//第一步: 获取生成统计图的 Dataset,并将其实例化
private DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();//Datasets是图标的信息容器,图表是以dataset为基础生成的
double [] value={47000,38000,31000,29000,25000};
String[] key={"济南","泰安","滨州","菏泽","聊城"};
for(int i=0;i
目录
工贸企业有限空间作业目录特种设备作业人员作业种类与目录特种设备作业人员目录1类医疗器械目录高值医用耗材参考目录
轴的显示标签
Y, // 数值轴的显示标签
getDataSet(), // 数据集
//PlotOrientation.HORIZONTAL , // 图表方向:水平
PlotOrientation.VERTICAL , // 图表方向:垂直
false, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle(title, new Font("黑体", Font.ITALIC , 22)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
//分类标签以45度角倾斜
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
return chart;
}
//返回一个CategoryDataset实例
private static CategoryDataset getDataSet()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
double[] value={4700,3800,3100,2900};//要传的
String[] key={"1","2","3","4"};
for(int i=0;i
OA.action.ChartAction">
800
600
cn.OA.action.LineChart">
800
600
cn.OA.action.SlidingCategoryAction">
800
600