Java to draw a pie chart

Source: Internet
Author: User

<% @ Page Language = "Java" contenttype = "image/PNG; charset = GBK" pageencoding = "GBK"
Import = "Java. AWT. *, javax. ImageIO. *, java. AWT. Geom. *, java. AWT. image. *" %>

<%! // Description of creating a pie chart

Public void drawtips (string tips, color, arc2d. Double arc2d, graphics2d g2d ){

Arc2d. Double position = arc2d;

Position. setangleextent (arc2d. getangleextent ()/2 );

Position. x = arc2d. X-15;

Position. Y = arc2d. Y-15;

Position. width = arc2d. getwidth () + 50;

Position. Height = arc2d. getheight () + 50;

Point2d. Double endpoint = (point2d. Double) position. getendpoint ();

G2d. setpaint (color );

Int stringlength = g2d. getfontmetrics (). stringwidth (TIPS );

If (endpoint. x <= arc2d. getcenterx ())

G2d. drawstring (TIPS, (float) endpoint. X-stringlength,

(Float) endpoint. y );

Else {

G2d. drawstring (TIPS, (float) endpoint. X, (float) endpoint. y );
}

}
%>

<%
// Clear the buffer
Response. Reset ();

// Note the MIME type here
Response. setcontenttype ("image/PNG ");

// Create an image of 500x375
Int width = 500, Height = 375;

Bufferedimage image = new bufferedimage (width, height,

Bufferedimage. type_int_rgb );

// Create a java2d object
Graphics2d g2d = image. creategraphics ();

// Fill in the entire background
G2d. setpaint (color. White );

G2d. fillrect (0, 0, width, height );

// Draw a shadow, which consists of a gray progressive rounded rectangle.
Gradientpaint graygp = new gradientpaint (0, 0, color. Gray, width,

Height, new color (218,214,212), false );

G2d. setpaint (graygp );

Roundrectangle2d. Float bgrr = new roundrectangle2d. Float (5, 5,

Width-5, height-5, 50, 50 );

G2d. Fill (bgrr );

// Draw a progressive blue rounded rectangle background
Gradientpaint bluegp = new gradientpaint (0, 0, new color (252,197,

113), width/2, height/2, new color (255,255,169), true );

G2d. setpaint (bluegp );

G2d. fillroundrect (0, 0, width-5, height-5, 50, 50 );

// Draw the rectangular contour of the dark blue rounded corner
Basicstroke BS = new basicstroke (1.2f );

G2d. setstroke (BS );

G2d. setpaint (new color (55, 71,105 ));

G2d. drawroundrect (0, 0, width-5, height-5, 50, 50 );

// Draw the chart title
// String charttitle = "customer composition analysis ";

G2d. setcolor (color. Black );

G2d. setfont (new font ("", Font. Plain, 30 ));

// Int stringlength = g2d. getfontmetrics (). stringwidth (charttitle );

// G2d. drawstring (charttitle, (width-stringlength)/2, 30 );

// Define the Arc
Arc2d. Double arc2d = new arc2d. Double ();

Double startangle = 30.0, arcangle = 0.0;

Double rectwidth = 295.0, rectheight = 245.0;

Point2d. Double P2D = new point2d. Double (width-rectwidth)/2, 70.0 );

Int arctype = arc2d. Pie;

Color color [] = new color [5];

Color [0] = new color (99, 99, 0 );

Color [1] = new color (255,169, 66 );

Color [2] = new color (33,255, 66 );

Color [3] = new color (33, 0,255 );

Color [4] = new color (255, 0, 66 );

Double booksales [] = new double [5];

Double totalsales = 0.0;

// Define the thickness
Int Thickness = 25;

For (INT I = 0; I <booksales. length; I ++ ){

Booksales [I] = 1 + math. Random () * 99;

Totalsales + = booksales [I];

}

// Draw an arc with the starting angle not greater than 90 degrees
Startangle = 30;

For (INT I = 0; I <booksales. length; I ++ ){

Arcangle = booksales [I];

If (startangle> = 90 ){

Break;

}

For (Int J = thickness; j> 0; j --){

G2d. setcolor (color [I]. Darker ());

Arc2d = new arc2d. Double (P2D. X, P2D. Y + J, rectwidth,

Rectheight, startangle, arcangle, arctype );

// Fill the Arc
G2d. Fill (arc2d );

}

// Update the starting angle of the arc
Startangle + = arcangle;

}

// Draw an arc with the starting angle not less than 270 degrees
Startangle = 390;

For (INT I = booksales. Length-1; I> 0; I --){

Arcangle = booksales [I];

If (startangle <= 270 ){

Break;

}

For (Int J = thickness; j> 0; j --){

G2d. setcolor (color [I]. Darker ());

Arc2d = new arc2d. Double (P2D. X, P2D. Y + J, rectwidth,

Rectheight, startangle,-arcangle, arctype );

// Fill the Arc
G2d. Fill (arc2d );

}

// Update the starting angle of the arc
Startangle-= arcangle;

}

// Draw an arc with the starting angle between 90 degrees and 270 degrees
Startangle = 30;

For (INT I = 0; I <booksales. length; I ++ ){

Arcangle = booksales [I];

If (startangle> 90 & startangle <270 ){

For (Int J = thickness; j> 0; j --){

G2d. setcolor (color [I]. Darker ());

Arc2d = new arc2d. Double (P2D. X, P2D. Y + J, rectwidth,

Rectheight, startangle + arcangle,-arcangle,

Arctype );

// Fill the Arc
G2d. Fill (arc2d );

}

}

// Update the starting angle of the arc
Startangle + = arcangle;

}

// Draw the Top Arc
Startangle = 30;

For (INT I = 0; I <booksales. length; I ++ ){

Arcangle = booksales [I]; // item. getpercent () * 3.6;

G2d. setcolor (color [I]);

Arc2d = new arc2d. Double (P2D. X, P2D. Y, rectwidth, rectheight,

Startangle, arcangle, arctype );

// Fill the Arc
G2d. Fill (arc2d );

// Depicts the arc Contour
G2d. setstroke (New basicstroke (1.2f ));

G2d. setpaint (color. Gray );

G2d. Draw (arc2d );

// Update the starting angle of the arc
Startangle + = arcangle;

G2d. setfont (new font ("", Font. Plain, 12 ));

}

// Deploy the image
G2d. Dispose ();

// Use the write method of the ImageIO class to encode the image
Servletoutputstream SOS = response. getoutputstream ();

ImageIO. Write (image, "PNG", SOS );

SOS. Close ();

Out. Clear ();

Out = pagecontext. pushbody ();
%>

Related Article

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.