C # GDI + draw a line chart (for the first version)

Source: Internet
Author: User

/// <Summary>
/// Create a line chart
/// </Summary>
/// <Param name = "X"> center x value </param>
/// <Param name = "Y"> center point y value </param>
/// <Param name = "xylength"> X, Y axis length </param>
/// <Param name = "xstring"> X axis description </param>
/// <Param name = "ystring"> Y axis description </param>
/// <Param name = "xinfo"> X Data </param>
/// <Param name = "yinfo"> Y data </param>
/// <Param name = "maxpoint"> obtain the maximum Y axis data from the database </param>

Public bitmap createline (float X, float y, float xylength, string xstring, string ystring, string [] xinfo, float [] yinfo, float maxpoint)
{
Bitmap bitmap = new Bitmap (this. Width, this. Height); // defines the canvas
Graphics G = graphics. fromimage (Bitmap );
G. Clear (color. White );

Pointf P = new pointf (x, y); // center point
Pointf [] xpt = new pointf [3] {New pointf (P. X + xylength + 15, p. y), new pointf (P. X + xylength, P. y-8), new pointf (P. X + xylength, P. Y + 8)}; // X axis triangle
Pointf [] Ypt = new pointf [3] {New pointf (P. x, p. y-xylength-15), new pointf (P. x-8, p. y-xylength), new pointf (P. X + 8, p. y-xylength)}; // y-axis triangle

// X
G. drawline (pens. Black, p. x, P. Y, p. x + xylength, P. y );
G. drawpolygon (pens. Black, xpt );
G. fillpolygon (brushes. Black, xpt );
G. drawstring (xstring, new font ("", 12), new solidbrush (color. Red), p. x + xylength + 10, p. Y + 10 );

// Y
G. drawline (pens. Black, p. x, P. Y-xylength, p. x, P. y );
G. drawpolygon (pens. Black, Ypt );
G. fillpolygon (brushes. Black, Ypt );
G. drawstring (ystring, new font ("", 12), new solidbrush (color. Red), p. x-40, p. Y-xylength-30 );
// Pixel
Int point = (INT) (p. Y/xinfo. Length );

Int ypoint = 0;
If (maxpoint. tostring (). substring (0, 1) = "1 ")
{
Ypoint = 10;
}
Else
{
Int sale = convert. toint32 (maxpoint );
Int length = sale. tostring (). length;
String max = maxpoint. tostring (). substring (0, 1 );
String cz = "1 ";
For (int A = 1; A <length; A ++)
{
CZ + = "0 ";
Max + = "0 ";
}
Maxpoint = convert. tosingle (CZ) + convert. tosingle (max );
Ypoint = (INT) (maxpoint/convert. tosingle (CZ ));

}

// Draw the y Scale
For (INT Kd = 1; KD <= ypoint; KD ++)
{
Float kdy = P. Y-(360/maxpoint) * (maxpoint/ypoint * kD );
G. drawstring (convert. tostring (maxpoint/ypoint * kD), new font ("", 10), brushes. Black, p. x-40, kdy-5 );
G. drawline (pens. Black, p. x-5, kdy, p. x, kdy );
}

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

// Draw the X axis Project
G. drawstring (xinfo [I], new font ("", 10), brushes. black, new pointf (P. X + I * point-5, p. Y + 5 ));

G. drawellipse (pens. Black, p. x + I * point-1.5f, P. Y-360/maxpoint * yinfo [I], 3, 3 );
G. fillellipse (New solidbrush (color. Black), p. x + I * point-1.5f, P. Y-360/maxpoint * yinfo [I], 3, 3 );

// Draw a value
G. drawstring (yinfo [I]. tostring (), new font ("", 10), brushes. black, P. X + I * point, P. y-360/maxpoint * yinfo [I]);
// Draw line
If (I + 1 <xinfo. length)
G. drawline (pens. red, P. X + I * point, P. y-360/maxpoint * yinfo [I], p. X + (I + 1) * point, P. y-360/maxpoint * yinfo [I + 1]);
}
Return bitmap;
}

 

// Call in the form

Dataset DS = dbuitily. executequerysql ("select * From t_users"); // X-axis data source
String [] users = new string [Ds. Tables [0]. Rows. Count];
Float [] sales = new float [Ds. Tables [0]. Rows. Count];
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Users [I] = Ds. Tables [0]. Rows [I] [0]. tostring ();
Sales [I] = convert. tosingle (dbuitily. executescalarsql ("select sum (salemoney) from t_sales where userid = '" + users [I] + "'"); // ordinate the data source
}
Float maxvalue = convert. tosingle (dbuitily. executescalarsql ("select sum (salemoney) from t_sales group by userid order by sum (salemoney) DESC "));

Picchart. Image = createline (300,450,400, "employee ID", "sales price (RMB)", users, sales, maxvalue );

Slice:

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.