欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 14:39:24
jfreechart本身不能生成svg图形,但是可以借助另外一个东西,辅助生成.好像是这个:batik ,具体代码请看下文 一:java生成svg饼图,附带了一个标签显示...

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://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 2024 570'>");
  sfile.append("\n");
  sfile.append("<defs></defs>");
  sfile.append("\n");
  sfile.append("<g stroke-width='1' stroke='#ffffff' transform='matrix(1,0,0,1,16.384,-9.83)' xmlns='http://www.w3.org/2000/svg'>");
  sfile.append("\n");
  //循环创造path标签.
  string path =creatpath(502, 300, 300, percents,names);//中心点式503,300.
  sfile.append(path);
  sfile.append("</g>");
  sfile.append("\n");
  sfile.append("</svg>");
 return sfile.tostring();
 }
 /**
 * 
 * @param x0 中心点横坐标
 * @param y0 中心点纵坐标
 * @param r 半径
 * @param percents 百分比数组
 * @param names 显示颜色代表的名称
 * @return
 */ 
 public static string creatpath(double x0,double y0,double r,double[]percents,string[]names){
  stringbuffer sfile =new stringbuffer();
  double x1=0; //新扇形的x坐标
  double y1=0; //新扇形的y坐标
  double middlex=0; //文本显示的坐标,包括竖线显示的坐标
  double middley=0;
  double radian =0; //弧度
  double textradian=0; //文本显示位置度弧度
  double k=0;
  int n=10;
  for(int i=0;i<percents.length;i++){
  if(i==0){
   radian =getradian(percents[0]);
   textradian=radian/2;
   x1 = (x0+getcos(radian)*r);
   y1 = (y0-getsin(radian)*r);
   middlex=(x0+getcos(textradian)*r);
   middley=(y0-getsin(textradian)*r);
   double percent = math.round(percents[0]*100)/100.0;//获得精确到两位小数点的坐标.
   k=math.abs((middley-y0)/(middlex-x0));//获得扇形终点的坐标,与中心点连成的直线的斜率.(取正值)
   double sita= math.atan(k);//求斜角
   double linelen=50;
   double textlen=70;
   if(radian<6){
   linelen=90;
   textlen=110;//控制指示线的长度,与文字的位置
   }
   if(percents[i]!=0){//当一个类型为0时,饼图展示
   if((textradian<(math.pi/2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex+math.cos(sita)*linelen)+"' y2='"+(middley-(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex+math.cos(sita)*textlen)+"' y='"+(middley-(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if ((textradian>(math.pi/2)&&textradian<math.pi)){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex-math.cos(sita)*linelen)+"' y2='"+(middley-(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex-math.cos(sita)*textlen)+"' y='"+(middley-(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if ((textradian>(math.pi)&&textradian<(math.pi*3/2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex-math.cos(sita)*linelen)+"' y2='"+(middley+(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex-math.cos(sita)*textlen)+"' y='"+(middley+(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if((textradian>(math.pi*3/2)&&textradian<(math.pi*2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex+math.cos(sita)*linelen)+"' y2='"+(middley+math.sin(sita)*linelen)+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex+math.cos(sita)*textlen)+"' y='"+(middley+(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }
   sfile.append("\n"); 
   if(getradian(percents[0])>math.pi){
   sfile.append("<path d='m "+x0+" "+y0+" l "+(x0+r)+" "+r+" a "+r+" "+r+" 0 1 0 "+x1+" "+y1+" l "+x0+" "+y0+" z' fill='"+colors[0]+"'/>");
   }else{
   sfile.append("<path d='m "+x0+" "+y0+" l "+(x0+r)+" "+r+" a "+r+" "+r+" 0 0 0 "+x1+" "+y1+" l "+x0+" "+y0+" z' fill='"+colors[0]+"'/>");
   }
   sfile.append("\n");
   }
   sfile.append("<rect x='"+(x0+2*r)+"' y='"+(y0-r/2.0+n)+"' width='60' height='30' fill='"+colors[i]+"' stroke='#ffffff' stroke-dasharray='1,1' />");
   sfile.append("\n"); 
   sfile.append("<text x='"+(x0+2*r+80)+"' y='"+(y0-r/2.0+n+25)+"' space='preserve' font-family='宋体' font-size='28' fill='"+colors[0]+"' stroke='#000000' stroke-dasharray='1,1' baseline-shift='baseline'>"+names[0]+"</text>");
   sfile.append("\n");
  }else{
  textradian = radian+(getradian(percents[i])/2);//获取指示线与x轴的弧度.
  radian =radian+getradian(percents[i]);//第i个扇形前面的弧度的总和
  middlex=(x0+getcos(textradian)*r);
   middley=(y0-getsin(textradian)*r);
   double percent = math.round(percents[i]*100)/100.0;
   k=math.abs((middley-y0)/(middlex-x0));
   double linelen=50;
   double textlen=70;
   if(radian<6){
   linelen=90;
   textlen=110;
   }
   double sita= math.atan(k);
   if(percents[i]!=0){//当一个类型为0时,饼图展示
   if((textradian<(math.pi/2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex+math.cos(sita)*linelen)+"' y2='"+(middley-(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex+math.cos(sita)*textlen)+"' y='"+(middley-(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if ((textradian>(math.pi/2)&&textradian<math.pi)){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex-math.cos(sita)*linelen)+"' y2='"+(middley-(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex-math.cos(sita)*textlen)+"' y='"+(middley-(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if ((textradian>(math.pi)&&textradian<(math.pi*3/2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex-math.cos(sita)*linelen)+"' y2='"+(middley+(math.sin(sita)*linelen))+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex-math.cos(sita)*textlen)+"' y='"+(middley+(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }else if((textradian>(math.pi*3/2)&&textradian<(math.pi*2))){
    sfile.append("<line x1='"+middlex+"' y1='"+middley+"' x2='"+(middlex+math.cos(sita)*linelen)+"' y2='"+(middley+math.sin(sita)*linelen)+"' stroke='#000000'/>");
    sfile.append("\n");
    sfile.append("<text x='"+(middlex+math.cos(sita)*textlen)+"' y='"+(middley+(math.sin(sita)*textlen))+"' space='preserve' font-family='tahoma' font-size='21' fill='red' stroke='red' baseline-shift='baseline' >"+percent+"%</text>");
   }
  sfile.append("\n");
  // 参数 1 表示 画大于180的弧, 0 表示画小于180的弧 (这个地方比较重要)
  if(getradian(percents[i])>math.pi){
   sfile.append("<path d='m "+x0+" "+y0+" l "+x1+" "+y1+" a "+r+" "+r+" 0 1 0 "+(x1=x0+getcos(radian)*r)+" "+(y1=y0-getsin(radian)*r)+" l "+x0+" "+y0+" z' fill='"+colors[i]+"'/>");
  }else{
    sfile.append("<path d='m "+x0+" "+y0+" l "+x1+" "+y1+" a "+r+" "+r+" 0 0 0 "+(x1=x0+getcos(radian)*r)+" "+(y1=y0-getsin(radian)*r)+" l "+x0+" "+y0+" z' fill='"+colors[i]+"'/>");
  }
  sfile.append("\n");
  }
  n+=50;
   sfile.append("<rect x='"+(x0+2*r)+"' y='"+(y0-r/2.0+n)+"' width='60' height='30' fill='"+colors[i]+"' stroke='#ffffff' stroke-dasharray='1,1' />");
   sfile.append("\n"); 
   sfile.append("<text x='"+(x0+2*r+80)+"' y='"+(y0-r/2.0+n+25)+"' space='preserve' font-family='宋体' font-size='28' fill='"+colors[0]+"' stroke='#000000' stroke-dasharray='1,1' baseline-shift='baseline'>"+names[i]+"</text>");
   sfile.append("\n");
  }
  }
  return sfile.tostring();
 }
 //返回弧度
 public static double getradian(double fenshu){
  return (fenshu*math.pi)/50;
 }
 //返回正弦
 public static double getsin(double radian){
 return math.sin(radian);
 }
 //返回余弦
 public static double getcos(double radian){
 return math.cos(radian);
 }
 public static void main(string[] args) {
 int[] data= {3,64,0,284,10};
 string[] names={"主变:"+data[0]+"个","断路器:"+data[1]+"个","线路:"+data[2]+"个","刀闸:"+data[3]+"个","母线:"+data[4]+"个"};
 create(data,names);
 }
 private static void create(int[] data,string[] names) {
 try {
 createsvg("d:/a.svg",getpercent(data),names);
 } catch (exception e) {
 e.printstacktrace();
 }
 }
 private static double[] getpercent(int 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,string[] names) throws exception {
 string sfile = initialize(percents,names);
 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());
 }
 }
}

二.java生成svg 3d饼图.

(这个可以生成图形,但是不完善,我没有再修改代码啦,因为觉得这个东西不值,用jfreechart可能更好.功能更强到,只是这几个程序,让我更加了解了svg这个东西,里面的一些标签都干什么用的.等等.) 3d的这个,生成的效果图,会有断层的效果,主要是出现在第一现象和第四象限,即如果第一象限或第四象限,有两个扇形的话,就会出现断层,可以用这个工具进行调整:svgdeveloper. 用它打开svg图形,然后将断层的扇形的代码,重新倒序排列一下.

package com.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 /* use css */);       
  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图表的全部内容,希望大家喜欢。