Many functions of CChart have been introduced in the previous lessons. In this lesson, let's take a look at it and make a review. The review method is a comprehensive exercise: Using CChart to display real-time data.
Because it is an exercise, we can set the problem background as needed. Now we assume that two temperature data records are sent from an instrument every Ms. One temperature data is the temperature outside the instrument, basically a constant value; the other temperature data is the internal temperature of the instrument, the temperature changes with time, first heating, then cooling, keep repeating.
We program based on the method in the previous lesson. For the sake of simplicity, this lesson will not deal with the interaction of CChart.
Continue step by step.
From step 1 to Step 7, follow the method in the previous lesson to create a VC6 project named Lesson09.
Step 8: Add menu resources to the main window of Lesson09.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130918173804968? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Two menu items are added in the figure, and their corresponding IDs are as follows.
Start (S) -- ID_REALTIME_START
Stop (T) -- ID_REALTIME_STOP
Step 9: Add header file reference near the header of the Lesson09.cpp file.
#include <time.h>#include <math.h>
These two header files will be used later.
Step 10: Add the following code to the next line of the Code staticCChartchart added in Step 7.
static unsigned int nCount;static bool bRun;static int nTimer;
Since we use code to simulate the input of the instrument, this nCount can control the output value of the instrument.
BRun indicates whether the timer is running, and nTimer is the timer code.
Step 2: Create a response routine for the WM_CREATE message.
Case WM_CREATE: chart. setType (kTypeXY); chart. setUseLegend (true); chart. setAxisTitle (_ T ("Time (minute)"), 1); chart. setAxisTitle (_ T ("temperature (degree)"), 0); chart. setTitle (_ T ("oven temperature change Chart"); nCount = 0; bRun = false; nTimer = 0; srand (unsigned) time (NULL); break;
Set some parameters of the image and initialize the timer status and Random Seed.
Step 2: Create a response routine for WM_TIMER.
Case WM_TIMER: if (bRun) {double Pi = 3.1415926536; double x, y1, y2; nCount ++; x = nCount/10.0; y1 = 20.0 * sin (2.0 * Pi/400.0 * nCount) + 40.0 + (2.0 * rand ()/RAND_MAX-1.0); y2 = 20.0 + (2.0 * rand ()/RAND_MAX-1.0) /2.0; if (nCount = 1) {double pX [1], pY [1]; pX [0] = x; pY [0] = y1; chart. addCurve (pX, pY, 1); pY [0] = y2; chart. addCurve (pX, pY, 1); chart. setDataTitle (_ T ("Heating Temperature"), 0); chart. setDataTitle (_ T ("ambient temperature"), 1); break;} chart. addPoint2D (x, y1, 0); chart. addPoint2D (x, y2, 1); RECT rt; GetClientRect (hWnd, & rt); InvalidateRect (hWnd, & rt, TRUE);} break;
The simulated temperature data is generated and added to the image. A random number is added to the simulated data to generate a real feeling.
Step 2: Create a menu response function. Enter the following code in the curly brackets behind the switch (wmid.
case ID_REALTIME_START:nTimer = SetTimer(hWnd, 1, 200, 0);bRun = true;break;case ID_REALTIME_STOP:if(nTimer>0){KillTimer(hWnd, nTimer);nTimer = 0;}bRun = false;break;
Start the timer and stop the timer respectively.
Step 3: Respond to the WM_ERASEBKGND message.
case WM_ERASEBKGND:return 0;
This step prevents image flickering.
Step 2: Compile and run the program.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130918174339203? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Nothing!
Well, don't worry. Come on.
Step 2: click "real-time data display"> "start" to display the curve. Is the image at a certain time point.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130918174501687? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Yeah, the effect is pretty good.
Summary: This course uses the knowledge learned above to simulate a real-time data display function, hoping to help students.