Package com.tellhow.svg;
Import Java.io.File;
Import Java.io.FileOutputStream;
/**
*
* @author the Wind
*
*/
public class Cakysvgwithlabel {
Define a different color
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 tag.
String Path =creatpath (502, +, percents,names);//center point 503,300.
Sfile.append (path);
Sfile.append ("</g>");
Sfile.append ("\ n");
Sfile.append ("</svg>");
return sfile.tostring ();
}
/**
*
* @param x0 Center Point Horizontal
* @param y0 Center Point ordinate
* @param r radius
* @param percents percent Array
* @param names display the name of the color representation
* @return
*/
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 fan
Double y1=0; The y-coordinate of the new fan
Double middlex=0; The coordinates of the text display, including the coordinates of the vertical bar display
Double middley=0;
Double radian = 0; Radian
Double textradian=0; Text display position degree radians
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;//obtains coordinates that are accurate to two decimal places.
K=math.abs ((middley-y0)/(middlex-x0));//Gets the coordinate of the fan end, the slope of the line connected to the center point. (Take positive value)
Double Sita= Math.atan (k);//Bevel
Double linelen=50;
Double textlen=70;
if (radian<6) {
linelen=90;
textlen=110;//control indicates the length of the line, and the position of the text
}
if (percents[i]!=0) {//When a type is 0 o'clock, 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= ' 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= ' 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= ' 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= ' 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= ' height= '" fill= ' +colors[i]+ ' ' stroke= ' # FFFFFF ' stroke-dasharray= '/> ');
Sfile.append ("\ n");
Sfile.append ("<text x=" + (x0+2*r+80) + "' y= '" + (y0-r/2.0+n+25) + "' space= ' preserve ' font-family= ' Arial ' font-size= ' 28 ' Fill= ' "+colors[0]+" ' stroke= ' #000000 ' stroke-dasharray= ', ' baseline-shift= ' baseline ' > ' +names[0]+ ' </text > ");
Sfile.append ("\ n");
}else{
Textradian = radian+ (Getradian (Percents[i])/2);//Gets the arc of the indicator line and the x-axis.
Radian =radian+getradian (percents[i]);//sum of radians preceding the first sector
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 o'clock, 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= ' 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= ' 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= ' 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= ' fill= ' red ' stroke= ' red ' baseline-shift= ' baseline ' > ' +percent+ '% </text> ");
}
Sfile.append ("\ n");
The parameter 1 indicates that the arc is greater than 180, and 0 indicates that the arc is 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 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= ' height= '" fill= ' +colors[i]+ ' ' stroke= ' # FFFFFF ' stroke-dasharray= '/> ');
Sfile.append ("\ n");
Sfile.append ("<text x=" + (x0+2*r+80) + "' y= '" + (y0-r/2.0+n+25) + "' space= ' preserve ' font-family= ' Arial ' font-size= ' 28 ' Fill= ' "+colors[0]+" ' stroke= ' #000000 ' stroke-dasharray= ', ' baseline-shift= ' baseline ' > ' +names[i]+ ' </text > ");
Sfile.append ("\ n");
}
}
return sfile.tostring ();
}
Return radians
public static double Getradian (double Fenshu) {
Return (FENSHU*MATH.PI)/50;
}
return sine
public static double Getsin (double radian) {
Return Math.sin (Radian);
}
Return 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 change:" +data[0]+ "," Circuit breaker: "+data[1]+", "Line:" +data[2]+ "," "Knife Gate:" +data[3]+ "," Bus bar: "+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 Generation pie chart svg