您的位置:首页 > 其它

jfreechar饼图及柱状图

2010-05-26 11:12 218 查看
整理了下jfreechart中产生统计图的细节配置

private static void createBarChart(StatVO statVo) throws IOException {
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart("水果产量图", // 图表标题
"水果", // 目录轴的显示标签
"产量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeCrosshairVisible(true);
categoryplot.setRangeCrosshairPaint(Color.blue);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
barrenderer.setSeriesItemLabelFont(1, new Font("宋体", Font.PLAIN, 12));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
/*------这句代码解决了底部汉字乱码的问题-----------*/
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F,
Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red,
0.0F, 0.0F, new Color(64, 0, 0));
barrenderer.setDrawBarOutline(false);
barrenderer.setSeriesPaint(0, gradientpaint);
barrenderer.setSeriesPaint(1, gradientpaint1);
barrenderer.setSeriesPaint(2, gradientpaint2);
barrenderer
.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator(
"Tooltip: {0}"));
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(0.52359877559829882D));
ChartFrame frame = new ChartFrame("TestPieChart", chart);
frame.pack();
frame.setVisible(true);
/*
* FileOutputStream fos_jpg = null; try { fos_jpg = new
* FileOutputStream("D://fruit_line.jpg");
* ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); }
* finally { try { fos_jpg.close(); } catch (Exception e) { } }
*/
}
/**
* 获取一个演示用的简单数据集对象
*
* @return
*/
private static CategoryDataset getDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, null, "苹果");
dataset.addValue(200, null, "梨子");
dataset.addValue(300, null, "葡萄");
dataset.addValue(400, null, "香蕉");
dataset.addValue(500, null, "荔枝");
return dataset;
}
/**
* 获取一个演示用的组合数据集对象
*
* @return
*/
private static CategoryDataset getDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(100, "上海", "苹果");
dataset.addValue(100, "广州", "苹果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "广州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "广州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "广州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "广州", "荔枝");
return dataset;
}
public static void createPieChart(StatVO statVo) {
// 用工厂类创建饼图
JFreeChart pieChart = ChartFactory.createPieChart("台湾偶像剧收视率2008-9-21",
createDataset(), true, true, false);
// RenderingHints做文字渲染参数的修改
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
pieChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
// 得到饼图的Plot对象
PiePlot piePlot = (PiePlot) pieChart.getPlot();
setSection(piePlot);
setLabel(piePlot);
setNoDataMessage(piePlot);
setNullAndZeroValue(piePlot);
// 将饼图显示在图像界面上
pieChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
pieChart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));
ChartFrame frame = new ChartFrame("TestPieChart", pieChart);
frame.pack();
frame.setVisible(true);
}
public static void createPieChart3d(StatVO statVo) {
// 用工厂类创建饼图
JFreeChart pieChart = ChartFactory.createPieChart3D(
"台湾偶像剧收视率2008-9-21", createDataset(), true, true, false);
// RenderingHints做文字渲染参数的修改
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
pieChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
// 得到饼图的Plot对象
PiePlot piePlot = (PiePlot) pieChart.getPlot();
piePlot.setStartAngle(290);
piePlot.setDirection(Rotation.CLOCKWISE);
piePlot.setForegroundAlpha(0.5f);
setSection(piePlot);
setLabel(piePlot);
setNoDataMessage(piePlot);
setNullAndZeroValue(piePlot);
// 将饼图显示在图像界面上
pieChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
pieChart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));
ChartFrame frame = new ChartFrame("TestPieChart", pieChart);
frame.pack();
frame.setVisible(true);
}
public static DefaultPieDataset createDataset() {
// 设置数据
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("篮球火", 2.80);
pieDataset.setValue("无敌珊宝妹", 3.63);
pieDataset.setValue("不良笑花", 2.84);
pieDataset.setValue("黑糖群侠传", null);
pieDataset.setValue("命中注定我爱你", 0);
return pieDataset;
}
public static void setSection(PiePlot pieplot) {
// 设置扇区颜色
pieplot.setSectionPaint("篮球火", new Color(160, 160, 255));
pieplot.setSectionPaint("无敌珊宝妹", new Color(128, 128, 223));
pieplot.setSectionPaint("不良笑花", new Color(96, 96, 191));
pieplot.setSectionPaint("命中注定我爱你", new Color(64, 64, 128));
// 设置扇区分离显示
pieplot.setExplodePercent("篮球火", 0.2D);
// 设置扇区边框不可见
pieplot.setSectionOutlinesVisible(false);
}
public static void setLabel(PiePlot pieplot) {
// 设置扇区标签显示格式:关键字:值(百分比)
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}:{1}({2} )"));
// 设置扇区标签颜色
pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));
pieplot.setLabelFont((new Font("宋体", Font.PLAIN, 12)));
}
public static void setNoDataMessage(PiePlot pieplot) {
// 设置没有数据时显示的信息
pieplot.setNoDataMessage("无数据");
// 设置没有数据时显示的信息的字体
pieplot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 14));
// 设置没有数据时显示的信息的颜色
pieplot.setNoDataMessagePaint(Color.red);
}
public static void setNullAndZeroValue(PiePlot piePlot) {
// 设置是否忽略0和null值
piePlot.setIgnoreNullValues(true);
piePlot.setIgnoreZeroValues(true);
}


图片代码直接运行就能显示
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: