java环形图_jfreechart环形图完美实现

importjava.awt.Color;importjava.awt.Font;importjava.awt.Paint;importjava.awt.image.BufferedImage;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importorg.jfree.chart.ChartFactory;importorg.jfree.chart.ChartUtilities;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.labels.StandardPieSectionLabelGenerator;importorg.jfree.chart.plot.DefaultDrawingSupplier;importorg.jfree.chart.plot.RingPlot;importorg.jfree.data.general.DefaultPieDataset;importorg.jfree.data.general.PieDataset;public classRingChart {publicRingChart() {this.createChart();

}

@SuppressWarnings("deprecation")public voidcreateChart() {

JFreeChart chart= ChartFactory.createRingChart(null, this.createDataset(), true, false, false);

chart.getLegend().setVisible(false);//环形图

RingPlot ringplot =(RingPlot) chart.getPlot();

ringplot.setOutlineVisible(false);//{2}表示显示百分比

ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));

ringplot.setBackgroundPaint(new Color(253,253,253));

ringplot.setOutlineVisible(false);//设置标签样式

ringplot.setLabelFont(new Font("宋体", Font.BOLD, 15));

ringplot.setSimpleLabels(true);

ringplot.setLabelLinkPaint(Color.WHITE);

ringplot.setLabelOutlinePaint(Color.WHITE);

ringplot.setLabelLinksVisible(false);

ringplot.setLabelShadowPaint(null);

ringplot.setLabelOutlinePaint(new Color(0,true));

ringplot.setLabelBackgroundPaint(new Color(0,true));

ringplot.setLabelPaint(Color.WHITE);

ringplot.setSectionOutlinePaint(Color.WHITE);

ringplot.setSeparatorsVisible(true);

ringplot.setSeparatorPaint(Color.WHITE);

ringplot.setShadowPaint(new Color(253,253,253));

ringplot.setSectionDepth(0.58);

ringplot.setStartAngle(90);

ringplot.setDrawingSupplier(newDefaultDrawingSupplier(newPaint[] {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("d:\\ring.png");

ChartUtilities.writeChartAsPNG(fos_jpg,chart,235, 230, null);

System.out.println("成功");//以下由于jfreechart设置背景色后,背景会有留白,直接将目标图片截取

ByteArrayOutputStream baos = newByteArrayOutputStream();

ChartUtilities.writeChartAsPNG(baos,chart,240, 230, null);

BufferedImage bi= ImageIO.read(newByteArrayInputStream(baos.toByteArray()));

BufferedImage sub= bi.getSubimage(5, 0, 230, 225);

ImageIO.write(sub,"png", new File("d:\\ring_sub.png"));

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

fos_jpg.close();

}catch(Exception e) {

System.out.println(e.getMessage());

}

}

}//测试数据

publicPieDataset createDataset() {

DefaultPieDataset dataSet= newDefaultPieDataset();inti, j;

String[] key= { "私募股权","房地产","基金","现金宝宝","股票"};int[] value = { 20, 20, 20, 30, 10};for (i = 0, j = 0; i < name.length && j < value.length; i++, j++) {

dataSet.setValue(name[i], value[j]);

}returndataSet;

}public static voidmain(String[] args) {newRingChart();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值