Java generated pie charts SVG and JFreeChart generated SVG charts

  • 2020-04-01 04:06:10
  • OfStack

Jfreechart itself cannot generate SVG graphics, but it can be generated with the help of another thing, like this :batik, please see the following for the specific code

One: Java generates SVG pie charts with a label to display the parts represented by each color


package com.tellhow.svg;
import java.io.File;
import java.io.FileOutputStream;

public class CakySvgWithLabel {
 //Define different colors
 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");
  //Loop to create the path label.
  String path =creatPath(502, 300, 300, percents,names);//Center point type 503,300.
  sfile.append(path);
  sfile.append("</g>");
  sfile.append("n");
  sfile.append("</svg>");
 return sfile.toString();
 }
  
 public static String creatPath(double x0,double y0,double r,double[]percents,String[]names){
  StringBuffer sfile =new StringBuffer();
  double x1=0; //The x-coordinate of the new sector
  double y1=0; //The y coordinate of the new sector
  double middleX=0; //Coordinates displayed by text, including those displayed by vertical lines
  double middleY=0;
  double radian =0; //radian
  double textRadian=0; // Text displays the degree of position radian
  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;//Obtain coordinates accurate to two decimal points.
   k=Math.abs((middleY-y0)/(middleX-x0));//Obtain the coordinates of the end of the sector, and the slope of the line connected with the central point.
   double sita= Math.atan(k);//O Angle
   double lineLen=50;
   double textLen=70;
   if(radian<6){
   lineLen=90;
   textLen=110;//Controls the length of the indicator line and the position of the text
   }
   if(percents[i]!=0){//When a type is 0, the pie chart shows
   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=' Song typeface ' 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);// Gets the indicator line and X The shaft radian.
  radian =radian+getRadian(percents[i]);// The first i In front of the fan radian The sum of the 
  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){//When a type is 0, the pie chart shows
   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");
  //Parameter 1 means draw an arc greater than 180, and parameter 0 means draw an arc less than 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=' Song typeface ' font-size='28' fill='"+colors[0]+"' stroke='#000000' stroke-dasharray='1,1' baseline-shift='baseline'>"+names[i]+"</text>");
   sfile.append("n");
  }
  }
  return sfile.toString();
 }
 // return radian
 public static double getRadian(double fenshu){
  return (fenshu*Math.PI)/50;
 }
 //Returns the sine
 public static double getSin(double radian){
 return Math.sin(radian);
 }
 //Returns the cosine
 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={" Main transformer :"+data[0]+" a "," The circuit breaker :"+data[1]+" a "," line :"+data[2]+" a "," breaker :"+data[3]+" a "," bus :"+data[4]+" a "};
 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 generated SVG 3D pie chart.

(this may generate graphics, but I don't perfect, I don't have to modify the code, because this thing is not worth, with jfreechart can better. More powerful, just a few programs, let me know more about the SVG, this thing, what's inside some of the labels., etc.) the 3 d, the generated renderings, will have the effect of fault, mainly appear in the phenomenon of the first and the fourth quadrant, i.e. if the first quadrant or the fourth quadrant, two fan, can appear the fault, can use this tool to adjust: SVGDeveloper. Use it to open up the SVG graphic, and then reorder the code in reverse order from the broken fan.


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(); 
 } 
  
 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]);//The sum from the ith sector to the first sector, radians.
   System.out.println(" radian 2:"+radian); 
    if (i==0){ 
    System.out.println(" radian 1:"+radian); 
    if(radian==Math.PI/2){ 
     xBottom = x0;//The x-coordinate of the base
     yBottom = y0-shortR;//The y coordinate of the base
    }else if(radian==Math.PI*3/2){ 
     xBottom = x0;//The x-coordinate of the base
     yBottom = y0+shortR;//The y coordinate of the base
    } 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);//The x-coordinate of the base
      yBottom = y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue);//The y coordinate of the base
     } 
     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){//Greater than PI radians, that is, the percentage is greater than 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;//The x-coordinate of the base
     yBottom1= y0-shortR;//The y coordinate of the base
    }else if(radian==Math.PI*3/2){ 
     xBottom1 = x0;//The x-coordinate of the base
     yBottom1 = y0+shortR;//The y coordinate of the base
    } 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);//The x-coordinate of the base
      yBottom1 = y0-(tanRadian*langR*shortR)/Math.sqrt(sqValue);//The y coordinate of the base
     } 
     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){//Greater than PI radians, that is, the percentage is greater than 50%
     System.out.println(" Is greater than 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(" Less than 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(); 
 } 
  //Returns the radian
 public static double getRadian(double percent){ 
  return (percent*Math.PI)/50; 
 } 
 //Returns the sine
 public static double getSin(double radian){ 
   return Math.sin(radian); 
 } 
  
 //Returns the cosine
 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()); 
  } 
 } 
} 

Iii. Use Jfreechart to generate SVG graphics dynamically:


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(); 
  //Create JFreeChart object, create the corresponding image in the middle of memory
  JFreeChart chart = ChartFactory.createBarChart3D(" Fruit yield chart ", //The chart title
    " fruit ", //Display label for directory axis
    " production ", //Display label for value axis
    dataset, //The data set
    PlotOrientation.VERTICAL, //Chart direction: horizontal, vertical
    true, //Whether to display legend (must be false for simple histogram)
    false, //Whether to generate tools or not
    false //Whether to generate URL links
    ); 
  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 { 
  //Get data Dataset
  CategoryDataset dataset = getDataSet(); 
  //Create JFreeChart object, create the corresponding image in the middle of memory
  JFreeChart chart = ChartFactory.createBarChart3D(" Fruit yield chart ", //The chart title
    " fruit ", //Display label for directory axis
    " production ", //Display label for value axis
    dataset, //The data set
    PlotOrientation.VERTICAL, //Chart direction: horizontal, vertical
    true, //Whether to display legend (must be false for simple histogram)
    false, //Whether to generate tools or not
    false //Whether to generate URL links
    ); 
 
  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) { 
   } 
  } 
 } 
 
  
 private static CategoryDataset getDataSet() { 
  DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
  dataset.addValue(100, "1", " grapes "); 
  dataset.addValue(200, "1", " pears "); 
  dataset.addValue(200, "1", " litchi "); 
  dataset.addValue(300, "2", " grapes "); 
  dataset.addValue(400, "2", " pears "); 
  dataset.addValue(500, "2", " litchi "); 
  return dataset; 
 } 
 
  
 private static CategoryDataset getDataSet2() { 
  DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
  dataset.addValue(100, " Beijing ", " apple "); 
  dataset.addValue(100, " Shanghai ", " apple "); 
  dataset.addValue(100, " Guangzhou ", " apple "); 
  dataset.addValue(200, " Beijing ", " pears "); 
  dataset.addValue(200, " Shanghai ", " pears "); 
  dataset.addValue(200, " Guangzhou ", " pears "); 
  dataset.addValue(300, " Beijing ", " grapes "); 
  dataset.addValue(300, " Shanghai ", " grapes "); 
  dataset.addValue(300, " Guangzhou ", " grapes "); 
  dataset.addValue(400, " Beijing ", " banana "); 
  dataset.addValue(400, " Shanghai ", " banana "); 
  dataset.addValue(400, " Guangzhou ", " banana "); 
  dataset.addValue(500, " Beijing ", " litchi "); 
  dataset.addValue(500, " Shanghai ", " litchi "); 
  dataset.addValue(500, " Guangzhou ", " litchi "); 
  return dataset; 
 } 
  
  
 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(); 
 } 
} 

There are two things to note when using this:

1. Jfreechart itself cannot generate SVG graphics, it needs to use batik. A Java toolkit,apache.

Batik awt - util. Jar
Batik - dom. The jar
  Batik - svggen. Jar
  Batik - util. Jar
Batik - XML. The jar
Jfreechart - 1.0.0. Jar

2. It is possible to generate SVG, which does not support Chinese when you view it. I remember it seems to be the viewer installed by adobe, which seems to be Chinese when browsing in IE.

The above content is all the content of Java generated pie chart SVG and JFreeChart generated SVG chart, I hope you like it.


Related articles: