Programming implementation of coordinate curve
Lipku@pku.org.cn
Due to the intuitive display of the chart, various statistics are often possible in practical applications. The coordinate curve is one of which is commonly used. The coordinate curve should be more difficult in various charts, mainly, it is difficult to hold the coordinate value from the position in the figure. I have encountered a painting coordinate curve in the project that is a ASP website. Since there is no easy use of free controls, I have to implement themselves. Therefore, there is a little experience in the painting coordinate curve.
The main idea of painting coordinate curve is: first find the minimum minx, maximum Maxx in the coordinate value. Also take the smallest value of Y, MINY, the maximum MAXY. The graphical width is Width, which is height, so the position corresponding to the coordinate (x, y) is:
((x-minx) * Width / (MAXX-minx), (Y-miny) * Height / (MAXY-Miny))
The specific code is explained in detail below. This code is basically suitable for painting a variety of coordinate curves, which can both paint on the coordinate axis or not painted on the coordinate axis. In order to facilitate everyone to debug operation, I all changed to the VC code, as long as you create a new Project in the VC, copy the following code to the onDraw () function. Everyone can be implemented in other programming languages as long as you have changed slightly.
Void CDRAWCOORVIEW :: Ondraw (CDC * PDC)
{
CDRAWCOORDOC * PDOC = getDocument ();
Ask_VALID (PDOC);
// Todo: Add Draw Code for Native Data HERE
// Initialize the coordinate value, usually taken from the database. Here is convenient for example to make a relatively simple
Const int Num = 10;
Float INITX [NUM] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Float inity [NUM] = {0.5, 2, 2.8, 4, 5.6, 6, 7, 9.4, 13.8, 23.4};
// Get x and y maximum, minimum
Float maxx = initX [0];
Float minx = INITX [0];
FLOAT MAXY = INITY [0];
Float miny = inity [0];
For (int i = 0; i { IF (INITX [I]> MAXX) Maxx = INITX [I]; IF (INITX [I] Minx = INITX [I]; IF (Inity [i]> maxy) MAXY = Inity [i]; IF (Inity [i] miny = inity [i]; } // If the origin must be on the X axis, add the following 2 line, otherwise comment IF (minx> 0) minx = 0; // If the origin must be on the Y-axis, add the following 2 line, otherwise comment IF (miny> 0) miny = 0; / / Determine the image display size INT width = 500; INT height = 300; / / Determine the blank size reserved around the coordinate map Const int mytop = 10; Const Int mybottom = 40; Const int myleft = 80; Const int myright = 50; / / Determine X, Y-axis per unit display width Float intervalX = (width-myveft-myright) / (maxx-minx); Float Interval = (HEIGHT-MyBottom-mytop) / (MAXY-Miny); // Draw a curve. Since the Y-axis of the drawing coordinates is downward, the Y value of each point is used here. // The image height minus the y value size. PDC-> Moveto (int (Myleft (INTX [0] -minx) * IntervalX, Int (Height- (INTYTOM (INTY [0] -Miny) * Interval)))))) For (i = 0; i { PDC-> Lineto (Int (Myleft (INTX [I] -minx) * Interval, Int (Height- (MyBottom (INITTOM (INITY) * Interval)) } // Draw X, Y axis // X axis from the leftmost end to the right end from the graph FLOAT BOTTOMY = 0; Float leftx = 0; // Bottomy represents the Y value of the X axis, and Leftx represents the x value of the Y-axis. IF (miny> 0) Bottomy = miny; IF (minx> 0) Leftx = minx; PDC-> Moveto (Int (Myleft), INT (Height- (MyBottomy-miny) Interval))) PDC-> Lineto (INT (Width-myright), INT (Height- (mybottomy-miny * interval)))); // Y axis from the bottom end to the top PDC-> MoveTo (int (Myleft (Leftx-minx) * IntervalX, int (Height-mybottom); PDC-> LineTo (int (Myleft (Leftx-minx) * IntervalX, int (mytop)); / / Determine the number of display scales Const int count = 5; / / Determine the width between each display scale Float SpaceX = (width-myleft-myright) / count; Float Spacey = (Height-mybottom-mytop) / count; // Draw scale and scale value CString Str; // x axis For (i = 0; i <= count; i ) { Str.Format ("%. 1f", minX i * (maxx-minx) / count); PDC-> MoveTo (Int (Myleft Spacex * i), INT (Height- (mybottomy-miny) intervaly)); PDC-> LINETO (INT (Myleft Spacex * i), INT (Height- (MyBottomy-miny) * Intervaly 5))) PDC-> Textout (Int (Myleft Spacex * I-10), Int (HEIGHT- (Bottomy-miny) * Interval-5)), STR); } // Y axis For (i = 0; i <= count; i ) { Str.Format ("%. 1f", miny i * (maxy-miny) / count); PDC-> MoveTo (int (My FTX-minx) * IntervalX, int (HEIGHT- (MyBottom Spacey * i))) PDC-> Lineto (Int (Myleft (Leftx-minx * IntervalX 5), int (HEIGHT- (MyBottom Spacey * i))) PDC-> TextOut (int (Myleft (Leftx-minx) * IntervalX-30), Int (HEIGHT- (Mybottom Spacey * i 8)), STR); } / / Draw the variable name of the X, Y axis PDC-> Textout (width / 2, height-20, "time (h)"); PDC-> TextOut (0, Height / 2, "Yield (kg)"); }