java生成饼图svg及JFreeChart生成svg图表

时间:2021-05-19

Jfreechart本身不能生成SVG图形,但是可以借助另外一个东西,辅助生成.好像是这个:batik ,具体代码请看下文

一:Java生成svg饼图,附带了一个标签显示各个颜色代表的部分

package com.tellhow.svg;import java.io.File;import java.io.FileOutputStream;/** * * @author 风絮NO.1 * */public class CakySvgWithLabel { //定义不同的颜色 static String[] colors ={"#f2e692", "#aa1111", "#799AE1", "#3e941b", "#66cc00", "#297110", "#d6a97b", "#82522b", "#aaaaff", "#1111aa", "#ff2222", "#ffaaaa"}; static String initialize(double [] percents,String[]names){ StringBuffer sfile = new StringBuffer(); sfile.append("<?xml version='1.0' encoding='UTF-8'?>"); sfile.append("\n"); sfile.append("<svg xmlns:svg='http://.xj.svg; import java.io.File; import java.io.FileOutputStream; public class Caky3DSVG { static String[] colors ={"#d6a97b", "#22FF22", "#aaffaa", "#799AE1", "#9aabEe", "#3e941b", "#f2e692", "#66cc00", "#297110", "#d6a97b", "#82522b", "#aaaaff", "#1111aa", "#ff2222", "#ffaaaa", "#aa1111" }; public static void main(String[] args) { double data[] = {20,20,50}; try { createSVG("f:/f.svg",getPercent(data)); } catch (Exception e) { e.printStackTrace(); } } static String initialize(double [] percent){ double percents[] = {10,15,5,20,40,10}; StringBuffer sfile = new StringBuffer(); sfile.append("<?xml version='1.0' encoding='UTF-8'?>"); sfile.append("\n"); sfile.append("<svg xmlns:svg='http://www.w3.org/2000/svg'"); sfile.append("\n"); sfile.append("xmlns='http://www.w3.org/2000/svg'"); sfile.append("\n"); sfile.append("xmlns:xlink='http://www.w3.org/1999/xlink'"); sfile.append("\n"); sfile.append("xml:space='default'"); sfile.append("\n"); sfile.append("version='1.1' width='100%' height='100%' viewBox='0 0 1024 600'>"); sfile.append("\n"); sfile.append("<defs></defs>"); sfile.append("\n"); String path = createPath(502, 300,300, 150, percents); sfile.append(path); sfile.append("</g>"); sfile.append("\n"); sfile.append("</svg>"); return sfile.toString(); } /** * * @param x0 原点 X * @param y0 原点 Y * @param langR * @param shortR * @param fenshu * @return */ static String createPath(double x0,double y0,double langR,double shortR ,double percents[]){ StringBuffer sfile = new StringBuffer(); double xBottom =0; double yBottom =0; double xBottom1=0; double yBottom1=0; double radian =0; sfile.append("<g stroke-width='1' stroke='#000000' transform='matrix(1,0,0,1,1.638,-9.83)' xmlns='http://www.w3.org/2000/svg'>"); sfile.append("\n"); for(int i=0;i<percents.length;i++){ System.out.println("i:"+i); radian =radian+getRadian(percents[i]);//第i个扇形到 第一个扇形,弧度的总和. System.out.println("弧度2:"+radian); if (i==0){ System.out.println("弧度1:"+radian); if(radian==Math.PI/2){ xBottom = x0;//底面的x坐标 yBottom = y0-shortR;//底面的y坐标 }else if(radian==Math.PI*3/2){ xBottom = x0;//底面的x坐标 yBottom = y0+shortR;//底面的y坐标 } else{ double tanRadian = Math.abs(Math.tan(radian)); double sqValue=shortR*shortR+tanRadian*tanRadian*langR*langR; if(radian<Math.PI/2){ System.out.println("if1:"+radian); xBottom = x0+(langR*shortR)/Math.sqrt(sqValue);//底面的x坐标 yBottom = y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue);//底面的y坐标 } else if (radian>Math.PI/2&&radian<=Math.PI){ System.out.println("if2:"+radian); xBottom =x0-(langR*shortR)/Math.sqrt(sqValue); yBottom =y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue); }else if (radian>Math.PI&&radian<Math.PI*3/2){ System.out.println("if3:"+radian); xBottom =x0-(langR*shortR)/Math.sqrt(sqValue); yBottom =y0+(tanRadian*langR*shortR)/Math.sqrt(sqValue); }else if (radian>Math.PI*3/2&&radian<Math.PI*2){ System.out.println("if4:"+radian); xBottom = x0+(langR*shortR)/Math.sqrt(sqValue); yBottom = y0+(tanRadian*langR*shortR)/Math.sqrt(sqValue); } } if(getRadian(percents[0])>Math.PI){//大于 PI 弧度,即百分比超过50% sfile.append("<g fill='"+colors[i]+"' >"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+y0+" L "+(x0+langR)+" "+y0+" A "+langR+" "+shortR+" 0 1 0 "+xBottom+" "+yBottom+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+(x0+langR)+" "+(y0-50)+" A "+langR+" "+shortR+" 0 1 0 "+xBottom+" "+(yBottom-50)+" L "+xBottom+" "+yBottom+" A "+langR+" "+shortR+" 0 1 1 "+(x0+langR)+" "+y0+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+(y0-50)+" L "+(x0+langR)+" "+(y0-50)+" A "+langR+" "+shortR+" 0 1 0 "+xBottom+" "+(yBottom-50)+" z' />"); sfile.append("\n"); sfile.append("</g>"); sfile.append("\n"); }else{ sfile.append("<g fill='"+colors[i]+"' >"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+y0+" L "+(x0+langR)+" "+y0+" A "+langR+" "+shortR+" 0 0 0 "+xBottom+" "+yBottom+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+(x0+langR)+" "+(y0-50)+" A "+langR+" "+shortR+" 0 0 0 "+xBottom+" "+(yBottom-50)+" L "+xBottom+" "+yBottom+" A "+langR+" "+shortR+" 0 0 1 "+(x0+langR)+" "+y0+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+(y0-50)+" L "+(x0+langR)+" "+(y0-50)+" A "+langR+" "+shortR+" 0 0 0 "+xBottom+" "+(yBottom-50)+" z' />"); sfile.append("\n"); sfile.append("</g>"); sfile.append("\n"); } }else{ if(radian==Math.PI/2){ xBottom1= x0;//底面的x坐标 yBottom1= y0-shortR;//底面的y坐标 }else if(radian==Math.PI*3/2){ xBottom1 = x0;//底面的x坐标 yBottom1 = y0+shortR;//底面的y坐标 } else{ double tanRadian = Math.abs(Math.tan(radian)); double sqValue=shortR*shortR+tanRadian*tanRadian*langR*langR; if(radian<Math.PI/2){ System.out.println("if1:"+radian); xBottom1 = x0+(langR*shortR)/Math.sqrt(sqValue);//底面的x坐标 yBottom1 = y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue);//底面的y坐标 } else if (radian>Math.PI/2&&radian<=Math.PI){ System.out.println("if2:"+radian); xBottom1 =x0-(langR*shortR)/Math.sqrt(sqValue); yBottom1 =y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue); }else if (radian>Math.PI&&radian<Math.PI*3/2){ System.out.println("if3:"+radian); xBottom1 =x0-(langR*shortR)/Math.sqrt(sqValue); yBottom1 =y0+(tanRadian*langR*shortR)/Math.sqrt(sqValue); }else if (radian>Math.PI*3/2){ System.out.println("if4:"+radian); xBottom1 = x0+(langR*shortR)/Math.sqrt(sqValue); yBottom1 = y0+(tanRadian*langR*shortR)/Math.sqrt(sqValue); } } if(getRadian(percents[i])>Math.PI){//大于 PI 弧度,即百分比超过50% System.out.println("大于pi"); sfile.append("<g fill='"+colors[i]+"' >"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+y0+" L "+xBottom+" "+yBottom+" A "+langR+" "+shortR+" 0 1 0 "+xBottom1+" "+yBottom1+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+(xBottom)+" "+(yBottom-50)+" A "+langR+" "+shortR+" 0 1 0 "+xBottom1+" "+(yBottom1-50)+" L "+xBottom1+" "+yBottom1+" A "+langR+" "+shortR+" 0 1 1 "+xBottom+" "+yBottom+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+(y0-50)+" L "+(xBottom)+" "+(yBottom-50)+" A "+langR+" "+shortR+" 0 1 0 "+xBottom1+" "+(yBottom1-50)+" z' />"); sfile.append("\n"); sfile.append("</g>"); sfile.append("\n"); }else{ System.out.println("小于pi"); sfile.append("<g fill='"+colors[i]+"' >"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+y0+" L "+xBottom+" "+yBottom+" A "+langR+" "+shortR+" 0 0 0 "+xBottom1+" "+yBottom1+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+(xBottom)+" "+(yBottom-50)+" A "+langR+" "+shortR+" 0 0 0 "+xBottom1+" "+(yBottom1-50)+" L "+xBottom1+" "+yBottom1+" A "+langR+" "+shortR+" 0 0 1 "+xBottom+" "+yBottom+" z' />"); sfile.append("\n"); sfile.append("<path d='M "+x0+" "+(y0-50)+" L "+(xBottom)+" "+(yBottom-50)+" A "+langR+" "+shortR+" 0 0 0 "+xBottom1+" "+(yBottom1-50)+" z' />"); sfile.append("\n"); sfile.append("</g>"); sfile.append("\n"); } xBottom=xBottom1; yBottom=yBottom1; } } return sfile.toString(); } //返回弧度 public static double getRadian(double percent){ return (percent*Math.PI)/50; } //返回正弦 public static double getSin(double radian){ return Math.sin(radian); } //返回余弦 public static double getCos(double radian){ return Math.cos(radian); } private static double[] getPercent(double data[]){ double sum=0; double percents[] = new double[data.length]; for(int i=0;i<data.length;i++){ sum+=data[i]; } for(int i=0;i<data.length;i++){ percents[i] =(data[i]/sum)*100; } return percents; } public static void createSVG(String fileRealPath, double[] percents) throws Exception { String sFile = initialize(percents); try { byte[] byteFil = sFile.getBytes("UTF-8"); File svgFile = new File(fileRealPath); if (svgFile.exists()) { svgFile.delete(); } FileOutputStream fos = new FileOutputStream(svgFile); fos.write(byteFil); fos.close(); } catch (Exception ex) { System.out.print(ex.getMessage()); } } }

三.使用Jfreechart动态生成svg图形:

import java.awt.Rectangle; import java.io.*; import org.jfree.chart.*; import org.apache.batik.dom.GenericDOMImplementation; import org.apache.batik.svggen.SVGGraphics2D; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.chart.plot.*; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; /** * 该类用于演示最简单的柱状图生成 */ public class BarChartDemo { public static void main(String[] args) throws IOException { CategoryDataset dataset = getDataSet(); // 创建JFreeChart对象,在内存中间创建出对应的图像 JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题 "水果", // 目录轴的显示标签 "产量", // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) false, // 是否生成工具 false // 是否生成URL链接 ); File fo_svg = new File("D:\\fruit3.svg"); Rectangle bounds = new Rectangle(0,0,400,300); exportChartAsSVG(chart,bounds,fo_svg); } private static void exportChartAsJPG() throws FileNotFoundException, IOException { // 得到数据Dataset CategoryDataset dataset = getDataSet(); // 创建JFreeChart对象,在内存中间创建出对应的图像 JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题 "水果", // 目录轴的显示标签 "产量", // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) false, // 是否生成工具 false // 是否生成URL链接 ); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:/fruit3.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, "1", "葡萄"); dataset.addValue(200, "1", "梨子"); dataset.addValue(200, "1", "荔枝"); dataset.addValue(300, "2", "葡萄"); dataset.addValue(400, "2", "梨子"); dataset.addValue(500, "2", "荔枝"); 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; } /** * Exports a JFreeChart to a SVG file. * * @param chart JFreeChart to export * @param bounds the dimensions of the viewport * @param svgFile the output file. * @throws IOException if writing the svgFile fails. */ private static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException { // Get a DOMImplementation and create an XML document DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // draw the chart in the SVG generator chart.draw(svgGenerator, bounds); // Write svg file OutputStream outputStream = new FileOutputStream(svgFile); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); svgGenerator.stream(out, true ); outputStream.flush(); outputStream.close(); } }

用这个的时候需要注意两点:

1 .jfreechart本身不能生成svg图形,需要用到batik .一个java工具包,apache的.

batik-awt-util.jar
batik-dom.jar
batik-svggen.jar
batik-util.jar
batik-xml.jar
jfreechart-1.0.0.jar

2.就是可能生成svg,当你查看的时候不支持中文. 我记得好像是如果是安装的adobe的那个查看器,在IE里面浏览的话好像是中文乱码,如果用另外一个叫做 Renesis SVG Player ,这个查看器就是支持中文的.

以上内容就是java生成饼图svg及JFreeChart生成svg图表的全部内容,希望大家喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章