Struts Development Practice - Cylindrical Chart, Pie Chart Example
The main function of this case is to complete the pillar chart, the pie chart drawn, and generate the column chart, the pie chart to the JPG graphic and display.
1. The main function of the call: Please refer to the description of the graph section
2. Curve drawing file
/ *********** Program statRectFrame Begin ******************** /
Package test;
Import java.awt. *;
Import java.awt.geom. *;
Import java.awt.image. *;
Import javax.swing.jframe;
Import java.lang.math;
/ **
* Drawing: column chart; pie chart
* /
Public Class StatRectFrame
Extends jframe {
Private int [] result
Private string [] Title;
Private int statness;
Private color [] color = {
Color.Blue, New Color (255, 92, 0), New Color (255, 0, 30),
New Color (0, 255, 30), New Color (255, 0, 240), Color.CYAN};
/ ** Entrance parameters: result set, title set, statistical type: column map OR pie chart * /
Public statrecTframe (int [] result, string [] title, int statright {
THIS.RESULT = Result;
THIS.TITLE = Title;
THIS.STATSTYLE = Statstyle;
}
Public void paint (graphics g) {
Graphics2D G2 = (Graphics2D) g;
Dimension DIM = this.getsize ();
IF (statstyle == 1) {// pie chart
G2.SetColor (color.white);
G2.FillRect (0, 0, Dim.Width, Dim.Height);
INT TOTAL = Result [Result.Length - 1];
INT begtangle = 0;
INT endtangle = 0;
g2.setcolor (color.black);
INT POINTX = Dim.Width / 2; // Central x
INT Pointy = dim.height / 2; //
INT R = 150; // radius
Long [] Pointsx = new long [result.length-1];
Long [] Pointsy = New long [Result.Length-1];
Pointsx [0] = POINTX R;
Pointsy [0] = POINTY;
For (int i = 1; i IF (i> = 6) { G2.SetColor (Color [I% 6]); } Else { G2.SetColor (Color [I]); } IF (Result [i]> 0) { INT percent = math.Round ((Result [i] * 100f) / total); EndTangle = Math.Round (Percent * 360F / 100F); Title [i] = Title [i] "(" percent "%)" G2.fillarc (PointX-R, Pointy-R, 2 * R, 2 * r, begtangle, endtangle); // New center point Pointsx [i] = math.round (Math.cos (Math.ToraDians (Begtangle Endtangle * 0.5) * R Pointx); Pointsy [I] = Math.Round (Pointy-Math.sin (Math.ToraDIns (Begtangle EndTangle * 0.5)) * R); / / Extended Long pointsxex = math.round (Math.cos (Math.ToraDians (begtangle endtangle * 0.5) * (R 50) Pointx); Long PointSyex = Math.Round (Pointy-Math.sin (Math.TORADIANS (Begtangle EndTangle * 0.5)) * (R 50)); g2.setcolor (color.black); G2.draw (New Line2D.Double (Pointsx [i], Pointsy [i], Pointsxex, PointSyex); // Painting box G2.SETCOLOR (New Color (251, 249, 206); IF ((int) pointsxex) G2.FillRect ((int) Pointsxex-110, (int) PointSyex - 10, 110, 20); g2.setcolor (color.black); g2.drawstring (Title [i], (int) Pointsxex-110, (int) PointSyex 5); } Else { G2.FillRect ((int) Pointsxex, (INT) PointSyex - 10, 110, 20); g2.setcolor (color.black); g2.drawstring (Title [i], (int) Pointsxex, (int) PointSyex 5); } Begtangle = begtangle endtangle; } } } Else {// column // Make Sure To Fill in the Background Color G2.SetColor (color.white); G2.FillRect (0, 0, Dim.Width, Dim.Height); // Draw the x and y axis G2.SETPAINT (color.black); G2.setStroke (New Basicstroke (2.0F)); g2.draw (New Line2D.double (10, 10, 10, Dim.Height - 120)); G2.draw (New Line2D.double (10, Dim.Height - 120, DIM.WIDTH, DIM.HEIGHT - 120)); // Paste column Font font = g2.getfont (). DeriveFont (12.0f); g2.setfont (font); // font.layoutglyphvector () INT Height = Math.Round (Dim.Height - 120 - 30) / Result [0]; For (int i = 1; i G2.SETPAINT (color.black); g2.drawstring ("" result [i], 20 (i - 1) * 30, Dim.height - 120 - Height * Result [i] - 20); // Draw the title //print one by one; For (int J = 1; j <= math.round (title [i] .length () / 2); J ) { g2.drawstring (title [i] .substring ((j - 1) * 2, j * 2), 20 (i - 1) * 30, DIM.HEIGHT - 120 J * 20); } IF (i> = 6) { G2.SetColor (Color [I% 6]); } Else { G2.SetColor (Color [I]); } G2.FillRect (20 (i - 1) * 30, Dim.Height - 120 - Height * Result [i], 20, Height * Result [I]); } } } } / **************** Program End ******************************* / 3. Generate a JPG graphic and display: similar to the portion described in the graph, no longer described again.