<!--用于生成图片 -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
所需依赖
创建工具类
public static void createChartImg( List<List<String>> list , String filePath ) {
File file;
int i = 0;
DefaultPieDataset pds = new DefaultPieDataset();
boolean flag = false;
if (list != null && list.size() > 0) {
for (i = 1; i < list.size(); i++) {
//设置饼图数据
pds.setValue(list.get(i).get(0), Double.parseDouble(tableList.get(i).get(1)));
}
}
if (i > 0) {
createChart(pds, filePath, null , flag);
}
}
创建环形图
/**
* 创建环形图并保存到本地
*
* @return
*/
public static void createChart(DefaultPieDataset pds, String filePath, String title , boolean flag) {
JFreeChart chart = ChartFactory.createRingChart(null, pds, true, false, false);
chart.getLegend().setVisible(false);
// 如果不使用Font,中文将显示不出来
java.awt.Font font = new java.awt.Font("宋体", Font.BOLD, 12);
// 环形图
RingPlot ringplot = (RingPlot) chart.getPlot();
ringplot.setOutlineVisible(false);
//{2}表示显示百分比
if (flag) {
ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}占{2})"));
} else {
ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}为(0)占{2}"));
}
ringplot.setBackgroundPaint(new Color(253,253,253));
ringplot.setOutlineVisible(false);
java.awt.Font LabelFont = new java.awt.Font("宋体", Font.BOLD, 10);
//设置标签样式
ringplot.setLabelFont(LabelFont);
ringplot.setSimpleLabels(false);
ringplot.setLabelLinkPaint(Color.BLACK);
ringplot.setLabelOutlinePaint(Color.WHITE);
ringplot.setLabelLinksVisible(true);
ringplot.setLabelShadowPaint(null);
ringplot.setLabelOutlinePaint(new Color(0x0D9D56C,false));
ringplot.setLabelBackgroundPaint(new Color(0x267630,false));
ringplot.setLabelPaint(Color.WHITE);
ringplot.setSeparatorsVisible(false);
ringplot.setSeparatorPaint(Color.BLACK);
ringplot.setShadowPaint(new Color(253,253,253));
ringplot.setSectionDepth(0.58);
ringplot.setStartAngle(90);
ringplot.setDrawingSupplier(new DefaultDrawingSupplier(
new Paint[] {
new Color(134, 212, 222),
new Color(174, 145, 195),
new Color(255, 162, 195),
new Color(249, 163, 86),
new Color(119, 173, 195)
},
DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream(filePath.replace("jpg" , "png"));
ChartUtilities.writeChartAsPNG(fos_jpg,chart, 235, 230, null);
//以下由于jfreechart设置背景色后,背景会有留白,直接将目标图片截取
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ChartUtilities.writeChartAsPNG(baos,chart, 530, 250, null);
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
BufferedImage sub = bi.getSubimage(5, 0, 525, 250);
ImageIO.write(sub, "png", new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
生成如图