JFREECHART itself cannot generate SVG graphics, but you can use another thing to help generate. It seems to be this: Batik, please see below for specific code.
One: Java generates SVG cake map, which comes with a label to show the part of each color representative
package com.tellhow.svg; Import Java.file; Import Java.fileoutPutstream;/** * * @author Wind Wind NO.1 * */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.appen d ("<? Xml Version = '1.0' Encoding = 'UTF-8'?> "); sfile.append ("/n "); sfile.append (<svg xmlns: svg = 'http: //www.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" );; "<" <"<" <DEFS> </DEFS> "); sfile.append ("/n "); sfile.append (" <g Stroke-Width = '1' Stroke = '#FFFFFFFORM =' matrix (1,0,0,16.384, -9.83) 'xmlns =' http://www.w3.org/2000/svg '> "); sfile.append ("/n "); // cyclic Create a path tag. String Path = Creatpath (502, 300, 300, Percents, Names); // Center point formula 503,300. SFILE.APPEND (PATH); sfile.append ("</g>"); sfile.append ( "/n"); sfile.append ("</svg>"); Return sfile.tostring ();}/**** @param x0 center point horizontal coordinates* @param y0 center point vertical coordinates* @param r Radius* @Param Percents percentage array* @param names Display the name of the color representative* @Return*/ Public Stric String Creatpath (Double X0, Double Y0, Double [] Percents, String [] names) {stringbuffer sfile = new StringBuffer (); Double x1 = 0; // The new fan -shaped x coordinates double y1 = 0; // The new fan -shaped y coordinate double micropx = 0; // The coordinates displayed by the text, including the coordinates displayed by the vertical line, ; Double Radian = 0; // Arc Double Textradian = 0; // Text Display positions 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 ((midey-y0)/(midex-x0)); // Get the coordinates of the fan-shaped end point, the straight line of the straight line connected to the center point. k); // Find the diagonal angle dough linelen = 50; double textlen = 70; if (Radian <6) {linelen = 90; textlen = 110; // The length of the control indicator line, the position of the text} if (percents [percents [percents [ i]! = 0) {when a type is 0, the cake map display if (textradian <(math.pi/2))) {sfile.append ("<line x1 = '"+middlex+"' y1 = '"+MIDDLEY+"' X2 = '"+(Middlex+Math.cos (SITA)*Linelen)+"' y2 = '"+(math.sin (Sita)*linelen))+"' Stroke = '#000000'/> "); sfile.append ("/n "); sfile.append (" <text x = '"+(middlex+math.cos (sita)*textlen)+"' y = ' "+(math.sin (sita)*textLen))+" 'Space =' Preserve 'FONT-FAMILY =' TAHOMA 'FONT-SIZE = '21' Fill = 'Stroke =' Red eline- 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)*linen)+" 'y2 =' "+(math.sin (sita)*linlen ))+"'Stroke ='#000000 '/>"); sfile.append ("/n"); sfile.append ("<text x ='"+(middlex-math.cos (Sita)*TextLen) +"'y ='"+(math.sin (sita)*textlen))+"'space =' preserve 'font-family =' tahoma 'font-siZe =' 21 'fill =' red 'strike = '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 = '"+(midgey+ (Math.sin (sita)*linlen)+"'Stroke ='#000000 '/>"); sfile.append ("/n"); sfile.append ("<text x ='"+(middlex- Math.cos (Sita)*Textlen)+"'y ='"+(math.sin (sita)*textlen)+"'space =' preserve 'font-font-font-size =' 21 'Fill =' Red 'Stroke =' Red 'Baseline-Shift =' Baseline '> "+Percent+"%</Text> "); an <<< (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 = '"+(math.sin (sita)*textlen)+"' space = 'PRESERVE' Font-Family = 'TAHOMA' FONT-SIZE = '21 'Fill =' Red 'Stroke =' Red 'Baseline-Shift =' Baseline '> "+Percent+"%</Text> "");} SFile.append ("/N" ); (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+"+x1+" "+y1+" l "+x0+" "+y0+" z 'fill =' "+colors [0]+" '/> ");} sfile.append ("/n "); Rect x = '"+(x0+2*r)+"' y = '"+(y0-r/2.0+n)+"' width = '60 'height =' 30 'fill =' "+color [ i]+"'Stroke ='#FFFFFFF 'Stroke-Dasharray =' 1,1' />"); sfile.append (" /n"); 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> "); ("/n");} Else {textradian = Radian+(getradian (percents [i])/2); // Get the arc of the indicator line and the X -axis. Radian = Radian+Getradian (percents [i]); /// The sum of the arc in front of the i-shaped fan-(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 cake map display if (TextRadian <(math.pi/2))) {sfile.append ("" <line x1 = '"+midex+"' y1 = '"+middley+"' x2 = '"+(middlex+math.cos (sita)*linelen)+"' y2 = '"+(math.sin (Sita)*Linelen)+"'Stroke ='#000000 '/>"); sfile.append ("/n"); sfile.append ("<text x ='"+(middlex+math.cos ( Sita)*Textlen)+"'y ='"+(math.sin (sita (sita)*textLen))+"'space =' preserve 'font-font-font-size = '21' fill = 'Red' Stroke = 'Red' Baseline-Shift = 'Baseline'> "+Percent+"%</Text> "); {sfile.append ("<line x1 = '"+middlex+"' y1 = '"+middley+"' x2 = '"+(middlex-math.cos (sita)*linelen)+"' y2 = '"+( middey- (math.sin (sita)*linelen)+"'Stroke ='#000000 '/>"); sfile.append ("/n"); sfile.append ("<text x ='"+( MIDDLEX-Math.cos (Sita)*Textlen)+"'y ='"+(math.sin (sita)*textlen)+"'space =' preserve 'font-family =' tahoma 'font- SIZE = '21 'Fill =' Red 'Stroke =' Red 'Baseline-Shift =' Baseline '> "+Percent+"%</Text> "); adian <( Math.pi*3/2)))))))) {sfile.append ("<line x1 = '"+middlex+"' y1 = '"+middley+"' x2 = '"+(middlex-math.cos (Sita)*linelen )+"'y2 ='"+(mthdley+(math.sin (sita)*linlen))+"'strong ='#000000 '/>"); sfile.append ("/n"); "<text x = '"+(midex-math.cos (sita)*textlen)+"' y = '"+(math.sin (sita)*textlen)+"' space = 'preserve' font -Family = 'TAHOMA' FONT-SIZE = '21 'Fill =' Red 'Stroke =' Red 'Baseline-Shift =' Baseline '> "+Percent+"%</Text> "); Radian> (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 = '"+(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 indicates that the arc with a painting is greater than 180, and 0 means that the arc with less than 180 (this place is more important) if (Getradian (percents [i])> math.pi ) {sfile.append ("<path d = 'm"+x0+""+y0+"l"+x1+""+y1+"a"+r+""+r+"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+"+r+" "+r+" 0 0 "+(x1 = (x1 = x0+getcos (Radian)*R)+"+(y1 = y0-getsin (radian)*r)+" l "+x0+" "+y0+" z 'fill =' "+color [i]+" ' /");} sfile.append ("/n ");} n+= 50; sfile.append (" <rad = '"+(x0+2*r)+"' y = '"+(y0 (y0 -R /2.0+n)+"'width = '60' height = '30 'fill ='"+colors [i]+"'stroke ='#fffFFFF 'dasharray =' 1,1 ' />" "" );; "/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.tostration ();} // Return the arc Public Static Double Getradian (Double Fenshu) {Return (Fenshu*Math.pi)/50;} // Return to the sine Public Static Double Getsin (Double Radian) {return math.sin (Radian);} // Return Back to Yu Xian Public Static Double 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]+"one", "Dispeller:"+Data [1]+"", "Line:"+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; D OUBLE 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 FileRerealPath, Double [] Percents, String [] Names) Tion {string sfile = initialize (percents, names ); Try {byte [] bytefil = sfile.getbytes ("UTF-8"); File SVGFile = New File (FileRerealPath); if (svgfile.exist ()) {svgfile.delete ();} Fileoutp UtStream Fos = New FileoutPutstream (SVGFILE); fos.write (bytefil); fos.close ();} Catch (Exception EX) {system.out.print (ex.getMessage ());}}}}2. Java generates SVG 3D cake map.
(This can generate graphics, but imperfect, I did not modify the code anymore, because I think this thing is not worth it, and JFREECHART may be better. The function is stronger, but these programs have made me know the SVG more. What are some of the labels in it?) 3D.) The effect of the generated effect will have a fault effect. It mainly appears in the first phenomen If there are two fan -shaped, the fault will appear. You can use this tool to adjust: SVGDEVELOPER. Use it to open the SVG graphics, and then arrange the fan -shaped code of the fault.
package com.xj.svg; Import java.io.file; Import Java.fileoutPutstream; Public class caky3dsvg {static string [] color = {"#d6A97B", "#22F F22 "," #aaffaa ","#799ae1 "#9AABEE", "#3E941B", "#F2E692", "#66cc00", "#297110", "#D6A97B", "#82522B", "#aaaaff", "#1111aa", "#ff22222 "," #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.org/1999/xlink '"); n "); sfile.append (" xml: space = 'default' "); sfile.append ("/n "); sfile.append (" Version = '1.1' width = '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.tstring ();} / ** * * @param x0 origin x * @param y0 origin y * @param Langr * @param shortr * @param fenshu * @Return * / Static String C Reatepath (Double X0 , double y0, double langr, double shortr, double percents []) {stringbuffer sfile = new stringbuffer (); double xbottom = 0; double ybottom = 0; double xbot tom1 = 0; double ybottom1 = 0; double radian = 0; sfile .appnd ("<g Stroke-Width = '1' Stroke = '#000000' Transform = 'Matrix (1,0,0,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 first fan -shaped to the first fan, the sum of the arc. System.out.println ("Arc 2:"+Radian); if (i == 0) { System.out.println ("Arc 1:"+Radian); if (radian == math.pi/2) {xbottom = x0; // X coordinate ybottom = y0-shortr; // The y coordinate of the bottom surface} else if (radian == math.pi*3/2) {xbottom = x0; // X coordinate ybottom = y0+shortr; // The y coordinate of the bottom surface} else {double tanradian = math.tan (math.tan (Radian); Double SQvalue = Shortr*Shortr+Tanradian*Tanradian*Langr*Langr; if (Radian <math.pi/2) {System.out.println (""+Radian); XB OTTOM = 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); *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) {systemm .out.println ("IF4:"+Radian); xbottom = x0+(Langr*Shortr)/Mth.sqrt (SQVALUE); ybottom = y0+(tanradian*langr*shortr)/Mth.sqrt (SQVV Alue);}} if (Getradian (Percents [0])> math.pi {// greater than the PI arc, that is, a percentage of more than 50% sfile.append ("<gry = '"+color [i]+"'>"); sfile. Append ("/n"); sfile.append ("<path d = 'm"+x0+""+y0+"l"+(x0+langr)+""+y0+"+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 file = '"+colors [i]+"'> "); sfile.append ("/n "); sfile.append (" <path d = 'm "+x0+" "+y0+" l "+(x0 (x0 +Langr)+""+Y0+"A"+Langr+""+Shortr+"0 0 0"+Xbottom+""+Ybottom+"Z ' />"); sfile.append (" /n"); "<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 ybottom1 = y0-shortr; // The y coordinate of the bottom surface} Else if ( radian==Math.PI*3/2){ xBottom1 = x0;//底面的x坐标yBottom1 = y0+shortR;//底面的y坐标} else{ double tanRadian = Math.abs(Math.tan(radian) );; SHORTR*Shortr+Tanradian*Tanradian*Langr*Langr; if (Radian <math.pi/2) {system.out.println ("" ""+Radian); X0+(Langr*Shortr) /Math.sqrt (Sqvalue) ;/ The x coordinate ybottom1 = y0- (tanradian*langr*shortr)/math.sqrt (sqvalue); adian <= Math.pi) {System.out.println ("IF2:"+Radian); xbottom1 = x0- (LANGR*SHORTR)/Math.SQRT (sqvalue); Ortr)/ 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 ("I" f4: " +radian); xbottom1 = x0+(Langr*SHORTR)/Math.SQRT (sqvalue); ybottom1 = y0+(tanradian*langr*shortr)/math.sqrt (sqvalue);}} tradian (Percents [i])>> Math.pi) {// greater than Pi arc, that is, a percentage of more than 50% system.out.println ("greater than Pi"); sfile.append ("<gry = '"+colors [i]+"'>") ; sfile.append ("/n"); sfile.append ("<path d = 'm"+x0+""+y0+"l"+xbottom+""+ybottom+"+langr+" "" "+shortr+" 0 1 0 "+xbottom1+" "+ybottom1+" z ' /> "); sfile.append (" /n "); sfile.append (" <path d =' m "+(xbottom)+" "" "" "" "" )+"A"+langr+""+shortr+"0 1 0"+xbottom1+"+(ybottom1-50)+" L "+xbottom1+" "+ybottom1+"+langr+"" "" "" 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 ' /> "); ("/n"); sfile.append ("</g>"); sfile.append ("/n");} else {system.out.println ("less than Pi"); 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"); = 'M "+(xbottom)+" ""+(ybottom-50)+"a"+langr+""+shortr+"0 0"+xbottom1+"+(ybottom1-50)+" l "+xbottom1+" " +ybottom1+"a"+langr+""+shortr+"0 0 1"+xbottom+""+ybottom+"z ' />"); sfile.append (" /n"); 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 ();} // Return to the arc Public Static Double Double Double (Double Percent) {RETURN (Percent*Math.pi)/50;} // Return Back to the sine state static double getsin (Double Radian ) {RETURN Math.sin (Radian);} // Return to the string Public Static Double Getcos (Double Radian) {Return math.cos (Radian);} Private Static Double [] Getpercent (DOU BLE 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 FileRerealPath, Double [] Percents) {String sfile = initialize (percents); TRY {byte [] bytefil = sfile.getbytes ("UTF-8"); File SVGFile = New File (FileRerealPath); if (svgfile.exists ()) {svgfile.delete ();} FileoutPutStream FOS = New FileoutPutstream (SVGFILE) ; fos.write (bytefil); fos.Close ();} Catch (Exception EX) {System.out.print (ex.getMessage ());}}}}3. Use JFREECHART to dynamically generate SVG graphics:
Import java.awt.Rectangle; Import Java.io.*; Import org.jfree.chart.*; Import org.apache.batik.den.gnerICDOMIMPLEMATION; Import k.svggen.svggraphics2d; Import org.jfree. data.category.categorydataset; Import org.jfree.data.category.defaultCategorydataset; Import org.jfree.chart.plot.*; Import ATION; Import org.w3c.Docume; /*** This class is used to demonstrate the simplest pillar chart to generate*/ Public Class BarchartDemo {Public Static Void Main (String [] Args) Throws IOEXCEPTION {CategoryDataSet DataSet = GetDataSet (); // Create the JFREECHART object and create the corresponding in the middle of the memory Image jfreeChart Chart = ChartFactory.createBarchart3d ("Fruit Outlet Chart", // Chart title "Fruit", // Display label "yield", // The display tags of the numerical shaft dataset, // data set plotorientation.vertical, // Chart direction: horizontal, vertical transmission, // Whether to display a legend (for simple column diagrams must be false) false, // Whether to generate a tool false // Whether the URL link is generated); //fruit3.svg "); Rectangle Bounds = New Rectangle (0,0,400,300); ExportChartassvg (Chart, Bounds, FO_SVG);} Private Static void ExportChartasjpg () T Hrows FilenotFoundexception, IOEXception {// get data DataSet CategoryDataSet dataset = GetDataSet ( ); // Create the JFREECHART object and create a corresponding image jfreeChart Chart = Chartfactory.createBarchart3d ("Fruit Outlet", // The display tags "output", // Display label dataset, // data set plotorientation.vertical, // chart direction: horizont Whether to generate a URL link); jpg, 1, chart, 400, 300, null);} Finally {try { FOS_JPG.CLOSE ();} Catch (Exception E) {}} / ** * Get a simple data set object for demonstration * * @return * / private staticdatasetaset () {) {) ydataset dataset = new defaultCategorydataset (); DataSet.addvalue (100, "1", "Grape"); DataSet.addvalue (200, "1", "Pear"); DataSet.addvalue (200, "1", "lychee"); DataSet.addvalue (300 , "2", "Grape"); DataSet.addvalue (400, "2", "Pear"); dataset.addvalue (500, "2", "lychee"); Return dataset;} /*** Get a one The combination data set used by the demonstration * * * @Return */ Private Static Categorydatasetasetdataset2 () {DefaultCategorydatasetaset = New DEFAULTCATEGORYDASET (); DataSet.add Value (100, "Beijing", "Apple"); DataSet.addvalue (100, " Shanghai, "Apple"); DataSet.addvalue (100, "Guangzhou", "Apple"); DataSet.addvalue (200, "Beijing", "Pear"); "); DataSet.addvalue (200," Guangzhou "," Pear "); DataSet.addvalue (300," Beijing "," Grape "); DataSet.addvalue (300," Shanghai "," Grape "); DataSet. addvalue (300, "Guangzhou", "Grape"); DataSet.addvalue (400, "Beijing", "Banana"); dataset.addvalue (400, "Shanghai", "Banana"); Guangzhou "," Banana "); DataSet.addvalue (500," Beijing "," Litchi "); DataSet.addvalue (500," Shanghai "," Litchi "); "); Return dataset;} /** * exports a jfreechart to a svg file. * * @param chart jfreechart to export * @param bounds the dimensions of the viewport * @para m svgfile the output file. * @Throws IOEXCEPTION If Writing The SVGFile Fails. */ Private Static Void ExportChartassvg (JFREEChart Chart, Rectangle Bounds, File SVGFile) Throws IOEEXCEPTION Create An XML DOCUMENT DOMIMIMPLEMENTATION DOMIMPL = GenericDomImplementation.getDomimplementation (); , "SVG", Null); // Create An Instance of the SVG Generator SVGGRAPHICS2D SVGGENATOR = New SVGGRAPHICS2D (Document); // Draw the Chart in the SVG Generator Chart.dr. AW (SVGGENATOR, BOUNDS); // Write SVG File OutputStream OutputStream = New FileoutPutstream (SVGFILE); writer out = new outputStreamwriter (outputStream, "UTF-8"); );; Outputstream.close (); }}When using this, you need to pay attention to two points:
1. JFREECHART itself cannot generate SVG graphics, you need to use Batik. A Java toolkit, Apache.
Batik-AWT-Util.jar
batik-dom.jar
batik-svggen.jar
Batik-Util.jar
batik-xml.jar
jfreechart -.0.0.jar
2. It is possible to generate SVG. When you view it, you do n’t support Chinese. I remember it seems that if it is the installed ADOBE viewer, it seems that it is a Chinese garbled in the IE. If you use another one called Renesis SVG Player, this The viewer supports Chinese.
The above content is all the contents of Java generated cakes SVG and JFREECHART generated SVG charts. I hope everyone likes it.