Curvepaint. CS

Source: Internet
Author: User

Public class curvepaint
{

Public curvepaint (){}

// Number of dial lines
Private int _ x_keducount = 12;
Private int _ y_keducount = 12;

// Shift the pixel left of the dial line corresponding to the scale value position
Private float _ x_valuestrmoveleft = 5f;
Private float _ y_valuestrmoveleft = 45f;

// Format the scale value
Private string _ x_format = "#0.0 ";
Private string _ y_format = "#0.00 ";

// X axis scale value text direction
Private bool _ x_directionvertical = false;
Public int xkeducount
{
Get {return _ x_keducount ;}
Set {_ x_keducount = value ;}
}

Public int ykeducount
{
Get {return _ y_keducount ;}
Set {_ y_keducount = value ;}
}

Public float xvaluestrmoveleft
{
Get {return _ x_valuestrmoveleft ;}
Set {_ x_valuestrmoveleft = value ;}
}

Public float yvaluestrmoveleft
{
Get {return _ y_valuestrmoveleft ;}
Set {_ y_valuestrmoveleft = value ;}
}

Public bool xdirectionvertical
{
Get {return _ x_directionvertical ;}
Set {_ x_directionvertical = value ;}
}

Public String xformat
{
Get {return _ x_format ;}
Set {_ x_format = value ;}
}

Public String yformat
{
Get {return _ y_format ;}
Set {_ y_format = value ;}
}

Private void setextremevalues (float [] arr, ref float smallestvalue, ref float greatestvalue)
{
If (ARR = NULL | arr. Length = 0) throw new exception ("the array used to plot the graph is blank ");
Smallestvalue = arr [0];
Greatestvalue = arr [0];
For (INT I = 1; I <arr. length; I ++)
{
If (smallestvalue> arr [I]) smallestvalue = arr [I];
If (greatestvalue <arr [I]) greatestvalue = arr [I];
}
}

private void setkedustringarray (float [] keduarr, float increment)
{< br> for (INT I = 1; I {< br> keduarr [I] = keduarr [I-1] + increment;
}< BR >}< br> // Data normalization f (x) = ax + B
private float standard (float X, float a, float B, float C, float d)
{< br> return C * (A * x + B) + d;
}

Public bitmap drawcurve (float [] x_array, float [] y_array, string charttitle, string x_title, string y_title)
{
// drawing initialization
bitmap bmap = new Bitmap (500,500); // picture size
graphics gph = graphics. fromimage (bmap);
gph. clear (color. white);

// the gap between the left, bottom, right, and top of the curve is 60 pixels
pointf CPT = new pointf (60f, bmap. height-60f); // coordinate origin, coordinate axis start point (60,440)
pointf x_endpoint = new pointf (bmap. width-60f, CPT. y); // X axis end point (440,440)
pointf y_endpoint = new pointf (CPT. x, 60f); // end point of the Y axis (60, 60)

// Coordinate triangle arrow
Pointf [] xpt = new pointf [3] {New pointf (x_endpoint.x + 15, x_endpoint.y), new pointf (x_endpoint.x, x_endpoint.y-4), new pointf (x_endpoint.x, x_endpoint.y + 4)}; // X axis triangle
Pointf [] Ypt = new pointf [3] {New pointf (y_endpoint.x, y_endpoint.y-15), new pointf (y_endpoint.x-4, y_endpoint.y), new pointf (y_endpoint.x + 4, y_endpoint.y)}; // y-axis triangle

// Draw the chart title
Gph. drawstring (charttitle, new font ("", 14), brushes. Black, new pointf (y_endpoint.x + 60, y_endpoint.y-30); // chart title
// Draw the x-axis triangle arrow and title
Gph. drawline (pens. Black, Cpt. X, Cpt. Y, x_endpoint.x, x_endpoint.y );
Gph. drawpolygon (pens. Black, xpt );
Gph. fillpolygon (New solidbrush (color. Black), xpt );
Gph. drawstring (x_title, new font ("", 12), brushes. Black, new pointf (x_endpoint.x + 10, x_endpoint.y + 10 ));
// Draw the y-axis triangle arrow and title
Gph. drawline (pens. Black, Cpt. X, Cpt. Y, y_endpoint.x, y_endpoint.y );
Gph. drawpolygon (pens. Black, Ypt );
Gph. fillpolygon (New solidbrush (color. Black), Ypt );
Gph. drawstring (y_title, new font ("", 12), brushes. Black, new pointf (0, y_endpoint.y-30 ));

Float x_smallestvalue = 0f, x_greatestvalue = 0f;
Float y_smallestvalue = 0f, y_greatestvalue = 0f;
// Hunt for the maximum and minimum values of the horizontal and vertical coordinates
Setextremevalues (x_array, ref x_smallestvalue, ref x_greatestvalue );
Setextremevalues (y_array, ref y_smallestvalue, ref y_greatestvalue );

// Increment = (maximum value-Minimum value)
Float x_increment = (x_greatestvalue-x_smallestvalue );
Float y_increment = (y_greatestvalue-y_smallestvalue );

// Average increment = (maximum-minimum)/scale interval, scale interval = number of scale lines-1
Float x_avgincrement = x_increment/(xkeducount-1 );
Float y_avgincrement = y_increment/(ykeducount-1 );

Float [] x_keduarr = new float [xkeducount]; // scale value on the X axis
Float [] y_keduarr = new float [ykeducount]; // y axis scale value
X_keduarr [0] = x_smallestvalue;
Y_keduarr [0] = y_smallestvalue;

// assign a value to the scale value array
setkedustringarray (x_keduarr, x_avgincrement);
setkedustringarray (y_keduarr, y_avgincrement);

// start position of the dial line
pointf x_kedustart = new pointf (CPT. X + 30, CPT. y); // (90,440) The first dial of the X axis
pointf x_keduend = new pointf (x_endpoint.x-10, x_endpoint.y); // (430,440) last line of X axis
pointf y_kedustart = new pointf (CPT. x, CPT. y-30); // (60,410) The first dial of the Y axis
pointf y_keduend = new pointf (y_endpoint.x, y_endpoint.y + 10); // (60, 70) the last dial of the Y axis

// Average increment of coordinates at the dial line position
Float x_keduincrement = (x_keduend.x-x_kedustart.x)/(xkeducount-1 );
Float y_keduincrement = (y_kedustart.y-y_keduend.y)/(ykeducount-1 );

// Set the display direction of the scale value on the X axis.
Stringformat x_stringformat = new stringformat ();
If (xdirectionvertical)
{
X_stringformat.formatflags = stringformatflags. directionvertical;
}

// Draw the X axis dial and scale value
For (INT I = 1; I <= xkeducount; I ++)
{
If (I = 1)
{
Gph. drawstring (x_keduarr [I-1]. tostring (xformat), new font ("Times New Roman", 11), brushes. black, new pointf (x_kedustart.x-xvaluestrmoveleft, x_kedustart.y + 5), x_stringformat); // new stringformat (stringformatflags. directionvertical); // The last parameter is used to implement a vertical text layout. The default value is horizontal.
Gph. drawline (pens. lightgray, x_kedustart.x, x_kedustart.y, x_kedustart.x, y_endpoint.y );
}
Else
{
Gph. drawstring (x_keduarr [I-1]. tostring (xformat), new font ("Times New Roman", 11), brushes. black, new pointf (x_kedustart.x + (I-1) * x_keduincrement-xvaluestrmoveleft, CPT. Y + 5), x_stringformat); // new stringformat (stringformatflags. directionvertical); // The last parameter is used to implement a vertical text layout. The default value is horizontal.
Gph. drawline (pens. lightgray, x_kedustart.x + (I-1) * x_keduincrement, Cpt. Y, x_kedustart.x + (I-1) * x_keduincrement, y_endpoint.y );
}
}
// Draw the Y axis dial and scale value
For (INT I = 1; I <= ykeducount; I ++)
{
If (I = 1)
{
Gph. drawstring (y_keduarr [I-1]. tostring (yformat), new font ("Times New Roman", 11), brushes. black, new pointf (y_kedustart.x-yvaluestrmoveleft, y_kedustart.y-6 ));
Gph. drawline (pens. lightgray, y_kedustart.x, y_kedustart.y, x_endpoint.x, y_kedustart.y );
}
Else
{
Gph. drawstring (y_keduarr [I-1]. tostring (yformat), new font ("Times New Roman", 11), brushes. black, new pointf (y_kedustart.x-yvaluestrmoveleft, y_kedustart.y-(I-1) * y_keduincrement-6 ));
Gph. drawline (pens. lightgray, y_kedustart.x, y_kedustart.y-(I-1) * y_keduincrement, x_endpoint.x, role-(I-1) * y_keduincrement );
}
}

// (90,440) start point of the first dial line on the X axis, start point of the last dial line on the (430,440) x axis, start point of the first dial line on the (60,410) Y axis, and start point of the last dial line on the (60, 70) Y axis
// Normalization of the original two-dimensional data to a fixed drawing range (90-410, 70-) to facilitate drawing. Normalization function f (x) = A * x + B
// Because the orientation of the window Y axis is opposite to that of the normal Y axis, You need to convert the Y coordinate again. The transformation function g (x) = C * x + d
// The following are the transformation factors A, B, and y corresponding to the X coordinate transformation factors a, B, c, d
Float X_a = 340366f/x_increment;
Float x_ B = 90.0f-(340366f * x_smallestvalue)/x_increment;
Float y_a = 340366f/y_increment;
Float y_ B = 70.0f-(3401_f * y_smallestvalue)/y_increment;
Float y_c =-1f, y_d = 480f;

For (INT I = 1; I <= y_array.length; I ++)
{
// Draw point
Gph. drawellipse (pens. black, standard (x_array [I-1], X_a, x_ B, 1, 0)-1.5f, standard (y_array [I-1], y_a, y_ B, y_c, y_d) -1.5f, 3, 3 );
Gph. fillellipse (New solidbrush (color. black), Standard (x_array [I-1], X_a, x_ B, 1, 0)-1.5f, standard (y_array [I-1], y_a, y_ B, y_c, y_d) -1.5f, 3, 3 );
// Draw a value
Gph. drawstring (y_array [I-1]. tostring (), new font ("Times New Roman", 11), brushes. black, new pointf (Standard (x_array [I-1], X_a, x_ B, 1, 0), Standard (y_array [I-1], y_a, y_ B, y_c, y_d )));
// Draw line
If (I> 1) gph. drawline (pens. red, standard (x_array [I-2], X_a, x_ B, 1, 0), Standard (y_array [I-2], y_a, y_ B, y_c, y_d ), standard (x_array [I-1], X_a, x_ B, 1, 0), Standard (y_array [I-1], y_a, y_ B, y_c, y_d ));
}
Return bmap;
// Save the output image
}

Static public bitmap drawcurveonimage (float [] x_array, float [] y_array, string charttitle, string x_title, string y_title)
{
Curvepaint CI = new curvepaint ();
Return CI. drawcurve (x_array, y_array, charttitle, x_title, y_title );
}

static public bitmap drawcurveonimage (float [] x_array, float [] y_array, string charttitle, string x_title, string y_title, int x_keducount, int y_keducount)
{< br> curvepaint CI = new curvepaint ();
Ci. xkeducount = x_keducount;
Ci. ykeducount = y_keducount;
return CI. drawcurve (x_array, y_array, charttitle, x_title, y_title);
}

Static public bitmap drawcurveonimage (float [] x_array, float [] y_array, string charttitle, string x_title, string y_title, int x_keducount, int y_keducount, float limit, float limit, bool x_direvertical)
{
Curvepaint CI = new curvepaint ();
CI. xkeducount = x_keducount;
CI. ykeducount = y_keducount;
CI. xvaluestrmoveleft = x_valuestrmoveleft;
CI. yvaluestrmoveleft = y_valuestrmoveleft;
CI. _ x_directionvertical = x_directionvertical;
Return CI. drawcurve (x_array, y_array, charttitle, x_title, y_title );
}

}

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.