Object-Oriented Programming ideas from the production of a public opinion survey (4)

Source: Internet
Author: User

Public MyChart ()
{
//
// TODO: Add Constructor Logic here
//
M_arrItems = new ArrayList ();
M_strTitle = "\";
M_objBackColZ failed? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vciA9IENvbG9yLldoaXRlIDs8QlI + scheme + bV9zdHJVbml0ID0g "";
}

/// <Summary>
/// Overload Constructor
/// </Summary>
/// <Param name = "a_strTitle"> </param>
/// <Param name = "a_objBackColor"> </param>
/// <Param name = "a_intWidth"> </param>
/// <Param name = "a_intHeight"> </param>
/// <Param name = "a_intChartType"> </param>
/// <Param name = "a_strUnit"> </param>
Public MyChart (string a_strTitle, System. Drawing. Color a_objBackColor,
Int a_intWidth, int a_intHeight, ChartType a_intChartType, string a_strUnit)
{
M_arrItems = new ArrayList ();
M_strTitle = a_strTitle;
M_objBackColor = a_objBackColor;
M_intWidth = a_intWidth;
M_intHeight = a_intHeight;
M_intChartType = a_intChartType;
M_intTotalCount = 0;
M_strUnit = a_strUnit;
}

/// <Summary>
/// Add a new statistical chart item
/// </Summary>
/// <Param name = "a_objItem"> </param>
Public void AddItem (object a_objItem)
{
If (a_objItem is MyClass. Util. ChartItem)
{
M_arrItems.Add (a_objItem );
M_intTotalCount + = (ChartItem) a_objItem). Count;
}
Else
{
Throw (new Exception ("object type error, which must be a ChartItem object "));
}
}

/// <Summary>
/// Generate a Statistical Chart Image
/// </Summary>
/// <Param name = "a_strFileName"> name of the file to be saved </param>
/// <Remarks>
/// A_strFileName must be an absolute physical path
/// </Remarks>
Public void Create (string a_strFileName)
{
// If no Statistical Chart item is defined, an exception is thrown.
If (m_arrItems.Count = 0)
{
Throw (new Exception ("No Statistical Chart item defined "));
}


// Select a function based on different statistical chart types
Switch (m_intChartType)
{
Case ChartType. Bar:
DrawBar (a_strFileName );
Break;

Case ChartType. Pie:
DrawPie (a_strFileName );
Break;

Case ChartType. Curve:
DrawCurve (a_strFileName );
Break;

Default:
DrawPie (a_strFileName );
Break;
}

}

/// <Summary>
/// Draw a bar chart
/// </Summary>
/// <Param name = "a_strFileName"> name of the image to be saved </param>
Protected void DrawBar (string a_strFileName)
{
// Prepare the drawing. Create an image object and a graphics object.
System. Drawing. Image myBmp = new Bitmap (m_intWidth, m_intHeight );
System. Drawing. Graphics g = Graphics. FromImage (myBmp );

// Fill in the background
G. FillRectangle (new System. Drawing. SolidBrush (m_objBackColor), 0, 0, m_intWidth, m_intHeight );

// If no answer is provided, no result is displayed.
If (this. m_intTotalCount = 0)
{
G. DrawString ("no statistics! ", New Font (" ", m_intWidth/14 ),
New SolidBrush (Color. Red), (m_intWidth-m_intWidth/8*6)/2,
M_intHeight/2-m_intWidth/8 );
}
Else
{

// Write a question

// G. DrawString (m_strTitle, new System. Drawing. Font ("", m_intWidth/30 ),
// New System. Drawing. SolidBrush (Color. Black), (m_intWidth-m_strTitle.Length * (m_intWidth/30)/2, 10,
// System. Drawing. StringFormat. GenericDefault );


// Draw the rectangle of the Statistical Chart item
// Calculate the width of each rectangle
Int intWidth = m_intWidth/(m_arrItems.Count * 2-1 );

// Define a pixel-wide black pen
System. Drawing. Pen pen = new System. Drawing. Pen (new System. Drawing. SolidBrush (Color. Black), 1 );

For (int I = 0; I <m_arrItems.Count; I ++)
{
ChartItem item = (ChartItem) m_arrItems [I];

// Percentage calculated
Float intPercent = (float) Decimal. Divide (item. Count * 100, m_intTotalCount );

// Calculate the rectangle height
Int intHeight = (int) (m_intHeight/3*2 * intPercent/100-10 );

// Calculate the rectangle Coordinate
Int ix = intWidth * I * 3/2 + intWidth;
Int iy = m_intHeight/3*2-intHeight;


// Draw a rectangle
G. FillRectangle (new System. Drawing. SolidBrush (item. Color ),
Ix, iy, intWidth, intHeight + 10 );

// Write
// Calculate the font size
Int intFontSize = intWidth/this. TotalCount. ToString (). Length;
// Limit the font size to 12
IntFontSize = intFontSize> 12? 12: intFontSize;
G. DrawString (item. Count. ToString () + m_strUnit,
New System. Drawing. Font ("", intFontSize ),
New System. Drawing. SolidBrush (item. Color ),
Ix, iy-intFontSize * 2 );

// Draw a legend

// Calculate the font size
IntFontSize = m_intHeight/3/m_arrItems.Count/2-1;
IntFontSize = intFontSize> 12? 12: intFontSize;
G. FillRectangle (new System. Drawing. SolidBrush (item. Color), 20,
M_intHeight/3*2 + intFontSize * (I * 2 + 1) + 5,
IntFontSize, intFontSize );
G. DrawString (intPercent. ToInt32 (). ToString () + "% ",
New System. Drawing. Font ("", intFontSize ),
New System. Drawing. SolidBrush (item. Color ),
20 + intFontSize,
M_intHeight/3*2 + intFontSize * (I * 2 + 1) + 5 );
G. DrawString (item. Text,
New System. Drawing. Font ("", intFontSize ),
New System. Drawing. SolidBrush (item. Color), 80,
M_intHeight/3*2 + intFontSize * (I * 2 + 1) + 5 );

}

// Draw a line
G. DrawLine (pen, 0, 10, 0, m_intHeight/3*2 + 10 );
G. DrawLine (pen, 0, m_intHeight/3*2 + 10, m_intWidth,
M_intHeight/3*2 + 10 );
For (int I = 0; I <10; I ++)
{
G. DrawLine (pen, 0, m_intHeight/3*2/10 * I + 10,
5, m_intHeight/3*2/10 * I + 10 );
}

// Write unit
// G. DrawString ("unit:" + m_strUnit, new System. Drawing. Font ("", m_intWidth/40 ),
// New SolidBrush (Color. Black), 10, 10 );

// Clear
Pen. Dispose ();
}

// Clear control object
G. Dispose ();

// Save as jpg
Try
{
MyBmp. Save (a_strFileName, System. Drawing. Imaging. ImageFormat. JPEG );
}
Catch (Exception e)
{
Throw (new Exception ("failed to save the image:" + e. ToString ()));
}

MyBmp. Dispose ();

}

/// <Summary>
/// Pie chart
/// </Summary>
/// <Param name = "a_strFileName"> </param>
/// <Remarks>
/// This program is difficult to understand, because the size of the pie chart and text needs to be determined based on the image size, so all Calculation Methods
/// All are tested.
/// </Remarks>
Protected void DrawPie (string a_strFileName)
{
// Prepare the drawing. Create an image object and a graphics object.
System. Drawing. Image myBmp = new Bitmap (m_intWidth, m_intHeight );
System. Drawing. Graphics g = Graphics. FromImage (myBmp );

// Fill in the background
G. FillRectangle (new System. Drawing. SolidBrush (m_objBackColor), 0, 0, m_intWidth, m_intHeight );

// If no answer is provided, no result is displayed.
If (this. m_intTotalCount = 0)
{
G. DrawString ("no statistics! ", New Font (" ", m_intWidth/14 ),
New SolidBrush (Color. Red), (m_intWidth-m_intWidth/8*6)/2,
M_intHeight/2-m_intWidth/8 );
}
Else
{
// Define a floating point variable and remember the end point of the previous project
Int intAngel = 180;

// Create a pie chart
For (int I = 0; I <m_arrItems.Count; I ++)
{
ChartItem item = (ChartItem) m_arrItems [I];

// Starts from 180 degrees.

// Percentage calculated
Float intPercent = (float) Decimal. Divide (item. Count * 100, m_intTotalCount );
If (I = m_arrItems.Count-1)
{
G. FillPie (new SolidBrush (item. Color), 0, 0, m_intWidth * 2/3,
M_intHeight * 2/3, intangels, 540-intAngel );
}
Else
{
G. FillPie (new SolidBrush (item. Color), 0, 0, m_intWidth * 2/3,
M_intHeight * 2/3, intAngel, 360 * intPercent/100 );
IntAngel + = (int) (360 * intPercent/100 );
}

// Percentage of legends on the right of the pie chart
// Calculate the rectangle size
Int intRectSize = m_intHeight/30;

// Draw color blocks
G. FillRectangle (new SolidBrush (item. Color), m_intWidth * 2/3 + intRectSize,
IntRectSize * (I * 2 + 1) + m_intHeight/2-m_arrItems.Coun

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.