Java generates the pie chart svg and ikevg

Source: Internet
Author: User

Java generates the pie chart svg and ikevg

Package com. tellhow. svg;

Import java. io. File;
Import java. io. FileOutputStream;

/**
*
* @ Author highlights 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. 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/5o/xlink '");
Sfile. append ("\ n ");
Sfile. append ("xml: space = 'default '");
Sfile. append ("\ n ");
Sfile. append ("version = '1. 1' width = '000000' height = '000000' viewBox = '0 0 100% 100% '> ");
Sfile. append ("\ n ");
Sfile. append ("<defs> </defs> ");
Sfile. append ("\ n ");
Sfile. append ("<g stroke-width = '1' stroke = '# ffff' transform = 'matrix (16.384, 9.83,-)' xmlns = 'HTTP: // www.w3.org/2000/svg'> ");
Sfile. append ("\ n ");
// Create path labels cyclically.
String path = creatPath (502,300,300, percents, names); // center point 503,300.
Sfile. append (path );
Sfile. append ("</g> ");
Sfile. append ("\ n ");
Sfile. append ("</svg> ");
Return sfile. toString ();
}
/**
*
* @ Param x0 center X coordinate
* @ Param y0 center ordinate
* @ Param r radius
* @ Param percents percentage Array
* @ Param names: name of the display color
* @ Return
*/
Public static String creatPath (double x0, double y0, double r, double [] percents, String [] names ){
StringBuffer sfile = new StringBuffer ();
Double x1 = 0; // x coordinate of the new sector
Double y1 = 0; // y coordinate of the new slice
Double middleX = 0; // coordinates displayed in the text, including coordinates displayed in the vertical bar
Double middleY = 0;
Double radian = 0; // radian
Double textRadian = 0; // text display location 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; // obtain the Coordinates precise to two decimal places.
K = Math. abs (middleY-y0)/(middleX-x0); // obtain the coordinates of the end point of the slice, and the slope of the straight line connected to the center. (take a positive value)
Double sita = Math. atan (k); // estimate the 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 the type is 0, the pie chart is displayed
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 = 'preserv' 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 = 'preserv' 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 = 'preserv' 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 = 'preserv' 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 =' # ffff' stroke-dasharray = '1, 1 '/> ");
Sfile. append ("\ n ");
Sfile. append ("<text x = '" + (x0 + 2 * r + 80) + "'Y ='" + (y0-r/2.0 + N + 25) + "'space = 'preserv' 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); // obtain the radians of the ing line and the X axis.
Radian = radian + getRadian (percents [I]); // The sum of radians before the slice 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) {// when the type is 0, the pie chart is displayed
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 = 'preserv' 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 = 'preserv' 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 = 'preserv' 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 = 'preserv' font-family = 'tahoma 'font-size = '21' fill = 'red' stroke = 'red' baseline-shift = 'baseline'> "+ percent +" % </text> ");
}
Sfile. append ("\ n ");
// Parameter 1 indicates the arc drawn greater than 180, and 0 indicates the arc drawn less than 180 (this is 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 = '60' height = '30' fill = '" + colors [I] + "'stroke =' # ffff' stroke-dasharray = '1, 1 '/> ");
Sfile. append ("\ n ");
Sfile. append ("<text x = '" + (x0 + 2 * r + 80) + "'Y ='" + (y0-r/2.0 + N + 25) + "'space = 'preserv' 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 ();
}
// Return radians
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 variable:" + data [0] + "," circuit breaker: "+ data [1] +", "line: "+ data [2] +", "Knife Gate:" + data [3] + "," Bus: "+ 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 ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.