ASP. NET line chart-(3) Specific Drawing

Source: Internet
Author: User

1. Create a bitmap and use graphics to draw and annotate text. The specific code is as follows:

Bitmap image = new Bitmap (width, height );
Graphics graphics = graphics. fromimage (image );
Try
{
Graphics. Clear (color. White );
Font font = new font ("Arial", 9, fontstyle. Regular); // font used for the coordinate scale
Font font1 = new font ("", 15, fontstyle. Regular); // font used for the image title
Font font2 = new font ("", 10, fontstyle. Regular); // font used to annotate Information
Lineargradientbrush brush = new lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. gray, color. gray, 1.2f, true); // draw the lineargradientbrush of the gridline.
Lineargradientbrush brush2 = new lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. red, color. red, 1.2f, true); // lineargradientbrush for commenting Information
Lineargradientbrush brush3 = new lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. green, color. green, 1.2f, true); // lineargradientbrush for commenting Information
Lineargradientbrush brush4 = new lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. brown, color. brown, 1.2f, true); // lineargradientbrush for commenting Information

Graphics. fillrectangle (brushes. White, 0, 0, width, height );
Brush brush1 = new solidbrush (color. Black );
If (manqpfl. length> 0 & manqpfl1.length> 0 & manqpfl2.length> 0)
{
Graphics. drawstring ("from" + minyear. tostring () + "year" + minmonth. tostring () + "month to" + maxyear. tostring () + "year" + maxmonth. tostring () + "full month line chart", font1, brush1, new pointf (width/2-200, 30); // mark the image title
Graphics. drawstring ("business insurance is a red line", font2, brush2, new pointf (width/2 + 200,500); // annotation information
Graphics. drawstring ("", font2, brush3, new pointf (width/2 + 200,530); // annotation information
Graphics. drawstring ("car insurance is brown line", font2, brush4, new pointf (width/2 + 200,560); // annotation information
}
// Draw the border line of the image
Graphics. drawrectangle (new pen (color. Black), 0, 0, image. Width-1, image. Height-1 );

// Draw the Y axis
Int x = 60;
Pen mypen1 = new pen (color. Black, 2 );
Graphics. drawline (mypen1, X, 120, X, 458 );
// Draw the grid vertical line
Pen mypen = new pen (brush, 1 );
For (INT I = 0; I <totalmonths; I ++)
{
Graphics. drawline (mypen, X, 120, X, 458 );
X = x + 60; // each 60 pixels on the Y axis is a unit scale.
}
// Draw a grid line
Int y= 158;
For (INT I = 0; I <5; I ++)
{
Graphics. drawline (mypen, 60, Y, width-15, y );
Y = Y + 60; // every 60 pixels on the X axis is a unit scale.
}
// Draw the X axis
Graphics. drawline (mypen1, 60, Y, width-15, y );

// Mark the x-axis text
X = 35;
For (INT I = 0; I <totalmonths; I ++)
{
String month = Ds. tables [0]. rows [I] ["YY"]. tostring () + ". "+ Ds. tables [0]. rows [I] ["mm"]. tostring ();
Graphics. drawstring (month. tostring (), Font, brushes. Red, X, 460); // you can specify the text content and output position.
X = x + 60; // every 60 pixels on the X axis is a unit scale.
}
// Y-axis text
String [] M = {"90", "80", "70", "60", "50", "40 "};
Y = 150;
For (INT I = 0; I <6; I ++)
{
Graphics. drawstring (M [I]. tostring (), Font, brushes. Red, 25, Y); // you can specify the text content and output position.
Y = Y + 60; // every 60 pixels on the Y axis is a unit scale.
}

Int [] Count = new int [totalmonths];
Int [] count1 = new int [totalmonths];
Int [] count2 = new int [totalmonths];
For (Int J = 0; j <totalmonths; j ++)
{
Count [J] = convert. toint32 (manqpfl [J] * 360/60); // calculates the relative displacement of the data point to the coordinate axis.
Count1 [J] = convert. toint32 (manqpfl1 [J] * 360/60 );
Count2 [J] = convert. toint32 (manqpfl2 [J] * 360/60 );
}

Point [] mypoint = caculatepoints (count, totalmonths); // obtain the absolute position of the data point on the screen.
Point [] mypoint1 = caculatepoints (count1, totalmonths );
Point [] mypoint2 = caculatepoints (count2, totalmonths );

Pen mypen2 = new pen (color. Red, 2); // The manqpfl line is red
Pen mypen3 = new pen (color. Green, 2); // The manqpfl1 line is green
Pen mypen4 = new pen (color. Brown, 2); // The manqpfl2 line is brown

Graphics. drawlines (mypen2, mypoint); // draw a manqpfl line
Graphics. drawlines (mypen3, mypoint1); // draw the manqpfl1 line
Graphics. drawlines (mypen4, mypoint2); // draw the manqpfl2 line

// Value of the labeled data point
For (INT I = 0; I <totalmonths; I ++)
{
Graphics. drawstring (manqpfl [I]. tostring (), Font, brushes. Blue, mypoint [I]. X-8, mypoint [I]. y );
Graphics. drawstring (manqpfl1 [I]. tostring (), Font, brushes. Blue, mypoint1 [I]. X-8, mypoint1 [I]. Y-15 );
Graphics. drawstring (manqpfl2 [I]. tostring (), Font, brushes. Blue, mypoint2 [I]. X-8, mypoint2 [I]. Y-15 );
}

// Outputs bitmap
System. Io. memorystream mstream = new system. Io. memorystream ();
Image. Save (mstream, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (mstream. toarray ());
// This. Mess. innerhtml = mstream. toarray (). tostring ();
}
Finally
{
Graphics. Dispose ();
Image. Dispose ();
}

 

// Calculate the coordinates of the midpoint of a line
Protected point [] caculatepoints (INT [] Count, int totalnumber)
{
Point [] mypoint = new point [totalnumber];
For (INT I = 0; I <totalnumber; I ++)
{
Mypoint [I]. x = 60 + I * 60; mypoint [I]. Y = 700-count [I];
}
Return mypoint;
}

 

This article is original works, reprinted please indicate the source: http://www.cnblogs.com/luzx/

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.