A pie chart report

Source: Internet
Author: User
I recently made a graphical report on counting the number of votes. I used an example in 51aspx to show multiple images on one page, so I chose to dynamically Add the image control to the pannel. as follows:


/// <Summary>
/// Pie chart report
/// </Summary>
/// <Param name = "target"> Stream object instance </param>
/// <Param name = "p_dt"> data source </param>
/// <Param name = "p_strtitle"> title </param>
/// <Param name = "p_strtext"> subtitle field </param>
/// <Param name = "p_strcount"> count field </param>
Public void drawpic (stream target, p_able p_dt, string p_strtitle, string p_strtext, string p_strcount)
{
// Datatable m_dt = qvoteservice. instance (). getvotecount (p_strfid );
Float [] angle = new float [p_dt.rows.count];
String [] stxt = new string [p_dt.rows.count];
Float allcount = 0;

For (INT I = 0; I <p_dt.rows.count; I ++)
{
Datarow m_dr = p_dt.rows [I];
Allcount + = convert. tosingle (m_dr [p_strcount]. tostring ());

}
// Proportion of calculation options in the population
For (INT I = 0; I <p_dt.rows.count; I ++)
{
Datarow m_dr = p_dt.rows [I];
If (allcount! = 0)
{
Angle [I] = convert. tosingle (m_dr [p_strcount]. tostring () * 360.0f/allcount;
Stxt [I] = m_dr [p_strtext]. tostring () + "" + (math. round (convert. tosingle (m_dr [p_strcount]. tostring ()/allcount, 2) * 100 + "% ";
}
Else
{
Angle [I] = convert. tosingle (1 * 360.0f/p_dt.rows.count );
Stxt [I] = m_dr [p_strtext]. tostring () + "+ (convert. tosingle (1/p_dt.rows.count) * 100 +" % ";
}
}

Float x = 75.0f, y = 65.0f, D = 100366f, offset = 15.0f, X1 = 0.0f, Y1 = 0.0f; // Circular X, Y coordinate, radius, offset distance, X, offset Value in the Y direction
Float curangle = 0.0f; // current conversion Angle

Int ox = 100, oy = 100; // Center Coordinate
Int px1 = 0, py1 = 0, px2 = 0, py2 = 0; // the endpoint of the straight line

System. drawing. image bitmap = new Bitmap (385,200, system. drawing. imaging. pixelformat. format32bpppargb); // creates a bitmap object, which provides memory space for the image to be created
Graphics G = graphics. fromimage (Bitmap); // generates a graphics object, which encapsulates the GDI + drawing interface.

G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;

// 20 colors.
Color [] color = new color [] {color. papayawhip, color. red, color. salmon, color. thistle, color. blue, color. coral, color. darkgoldenrod, color. black, color. firebrick, color. gold, color. honeydew, color. indigo, color. khaki, color. lemonchiffon, color. maroon, color. navy,
Color. Orange, color. Violet, color. Wheat, color. yellowgreen };

G. Clear (color. fromargb (227,240,255); // clear the screen

Stringformat Sf = new stringformat ();
SF. Alignment = stringalignment. Center;

G. drawstring (p_strtitle, new font (fontfamily. genericsansserif, 8), new solidbrush (color. black), new rectanglef (0.0f, 10.0f, 420366f, 20366f), SF); // Title

// Draw the large rectangle on the right
G. fillrectangle (New solidbrush (color. fromargb (128,128,128), 457,105,150, (angle. Length + 1) * 12); // shadow
G. fillrectangle (New solidbrush (color. fromargb (253,245,230), 455,105,150, (angle. Length + 1) * 12); // foreground
G. drawrectangle (new pen (color. fromargb (128,128,128), 1), 455,105,150, (angle. Length + 1) * 12); // Contour
/**/
//////////////////////////////////////// /////

For (INT I = 0; I <angle. length; I ++)
{
SF. Alignment = stringalignment. Near;

Ox = convert. toint32 (x + D/2 );
Oy = convert. toint32 (Y + D/2 );

Px1 = convert. toint32 (D/2 + 20) * Math. Cos (curangle + angle [I]/2) * Math. PI/180.0f) + ox;
Py2 = py1 = convert. toint32 (D/2 + 20) * Math. sin (curangle + angle [I]/2) * Math. PI/180.0f) + Oy;

If (curangle + angle [I]/2) >=270 | (curangle + angle [I]/2) <90) px2 = px1 + 10;
Else px2 = px1-10;

// G. drawline (new pen (color. fromargb (120,120,120), 1), new point (OX, Oy), new point (px1, py1); // draw a straight line-release.
// G. drawline (new pen (color. fromargb (120,120,120), 1), new point (px1, py1), new point (px2, py2); // straight line

// If (curangle + angle [I]/2)> = 270 | (curangle + angle [I]/2) <90)
//{

// G. drawstring (stxt [I], new font (fontfamily. genericsansserif, 8), new solidbrush (color. fromargb (120,120,120), px2, py2-4, SF );
//}
// Else
//{

// G. drawstring (stxt [I], new font (fontfamily. genericsansserif, 8), new solidbrush (color. fromargb (120,120,120), px2-stxt [I]. length * 12-2, py2-4, SF );
//}

G. fillpie (New solidbrush (color. fromargb (128,128,128), X + 2, Y + 2, D, D, curangle, angle [I]);
G. fillpie (New solidbrush (color [I]), X, Y, D, D, curangle, angle [I]);

G. drawpie (new pen (color. fromargb (128,128,128), 0), X, Y, D, D, curangle, angle [I]);
Curangle + = angle [I];

// Draw a small rectangle on the right and text
G. fillrectangle (New solidbrush (color. fromargb (128,128,128), 427, I * 14 + 6 + 58 + 2, 20, 10); // shadow
G. fillrectangle (New solidbrush (color [I]), 207, I * 14 + 6 + 60, 20, 10); // foreground
G. drawrectangle (new pen (color. fromargb (120,120,120), 1), 207, I * 14 + 6 + 60, 20, 10); // Contour
G. drawstring (stxt [I], new font (fontfamily. genericsansserif, 8), new solidbrush (color. fromargb (80, 80, 80), 227, I * 14 + 6 + 60, SF );
}
Bitmap. Save (target, imageformat. GIF );
Bitmap. Dispose ();
G. Dispose ();
}
/// Dynamic addition in pannel
Datatable m_dt = Data Source
For (INT I = 0; I <m_dt.rows.count; I ++)
{
Image m_img = new image ();
M_img.style.add ("margin-top", "5px ");
M_img.style.add ("float", "Left ");
M_img.style.add ("margin-left", "5px ");
M_img.imageurl = "graphicstatements. aspx? FID = "+ m_dt.rows [I] [" FID "]. tostring () + "& ftitle =" + server. urlencode (m_dt.rows [I] ["ftitle"]. tostring ());
Panel1.controls. Add (m_img );
}

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.