一 生成饼状图
public class App {
public static void main(String[] args) {
try {
//构造饼图数据集
DefaultPieDataset pds = new DefaultPieDataset();
pds.setValue("weblogic", 500);
pds.setValue("sun", 200);
pds.setValue("oracle", 1000);
pds.setValue("ibm", 800);
pds.setValue("用友", 2000);
Font font = new Font("宋体",Font.BOLD,20);
//
JFreeChart chart = ChartFactory.createPieChart3D(
"jee应用服务器所占市场份额调查", pds, true, false, false);
//设定标题的字体
chart.getTitle().setFont(font);
PiePlot plot = (PiePlot) chart.getPlot();
//设置绘图区的字体
plot.setLabelFont(font);
//设置开始角
plot.setStartAngle(3.14 / 2);
//产生分裂效果
plot.setExplodePercent("weblogic", 0.1);
//设置前景色透明度
plot.setForegroundAlpha(0.7f);
//设置背景色为图片
Image image = ImageIO.read(new File("d:/sunset.jpg"));
plot.setBackgroundImage(image);
//设置背景透明度
plot.setBackgroundAlpha(0f);
//设置提示条的字体
chart.getLegend().setItemFont(font);
//创建标签生成器
StandardPieSectionLabelGenerator gor
= new StandardPieSectionLabelGenerator("{0}({2})");
//设置标签生成器
plot.setLabelGenerator(gor);
ChartUtilities.saveChartAsJPEG(new File("d:/kk.jpg"),
chart, 600, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
二 生成柱状图
public class AppBar {
public static void main(String[] args) {
try {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(100, "ibm", "1");
ds.addValue(200, "google", "1");
ds.addValue(500, "sun", "1");
ds.addValue(800, "oracle", "1");
ds.addValue(34, "ibm", "2");
ds.addValue(256, "google", "2");
ds.addValue(435, "sun", "2");
ds.addValue(667, "oracle", "2");
ds.addValue(123, "ibm", "3");
ds.addValue(346, "google", "3");
ds.addValue(732, "sun", "3");
ds.addValue(245, "oracle", "3");
Font font = new Font("宋体",Font.BOLD,20);
//
JFreeChart chart = ChartFactory.createBarChart3D("bar demo",
"月份", "销售额", ds,PlotOrientation.VERTICAL, true, false, false);
//设定标题的字体
chart.getTitle().setFont(font);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置域轴(种类轴)标签
plot.getDomainAxis().setLabelFont(font);
//设置范围轴(值轴)标签
plot.getRangeAxis().setLabelFont(font);
//设置前景色透明度
plot.setForegroundAlpha(0.7f);
//设置背景色为图片
//设置背景透明度
plot.setBackgroundAlpha(1f);
//设置提示条的字体
chart.getLegend().setItemFont(font);
ChartUtilities.saveChartAsJPEG(new File("d:/barDemo.jpg"),
chart, 600, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
三 生成线形图
public class AppLine {
public static void main(String[] args) {
try {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(100, "ibm", "1");
ds.addValue(200, "google", "1");
ds.addValue(500, "sun", "1");
ds.addValue(800, "oracle", "1");
ds.addValue(34, "ibm", "2");
ds.addValue(256, "google", "2");
ds.addValue(435, "sun", "2");
ds.addValue(667, "oracle", "2");
ds.addValue(123, "ibm", "3");
ds.addValue(346, "google", "3");
ds.addValue(732, "sun", "3");
ds.addValue(245, "oracle", "3");
Font font = new Font("宋体",Font.BOLD,20);
//
JFreeChart chart = ChartFactory.createLineChart3D("lie demo", "月份",
"销售额", ds,PlotOrientation.VERTICAL, true, false, false);
//设定标题的字体
chart.getTitle().setFont(font);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//设置域轴(种类轴)标签
plot.getDomainAxis().setLabelFont(font);
//设置范围轴(值轴)标签
plot.getRangeAxis().setLabelFont(font);
//设置前景色透明度
plot.setForegroundAlpha(0.7f);
//设置背景色为图片
//设置背景透明度
plot.setBackgroundAlpha(1f);
//设置提示条的字体
chart.getLegend().setItemFont(font);
ChartUtilities.saveChartAsJPEG(new File("d:/lineDemo.jpg"),
chart, 600, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}