In-depth introduction to CChart one lesson per day-the fifth lesson is back to the line chart.

Source: Internet
Author: User

The previous lesson demonstrates the first advanced function of CChart. This Lesson continues with the topic of the first lesson, introducing the most basic function of CChart-creating line charts.

In the first lesson, curve data was added at AddPoint2D at. Do you think this method is silly? If you have hundreds of thousands of data points, you must be exhausted!

The following describes a function of the CChart class.

intAddCurve(double *pX, double *pY, int nLen, int nPlotIndex=0); 

We can see from the name that this function is used to add curves.

First, let's look at the function parameters. PX and pY are two double-type arrays, which are used to store the x and y coordinates of the Curve. nLen indicates the number of data points. nIndex indicates the sequence number of the subview. CChart can process split views and hierarchical views. These views have subviews. nPlotIndex indicates that a curve needs to be added to the nPlotIndex subview.

Let's look at the return parameter of the function, which is an int value, indicating the ID of the curve.

CChart can draw multiple curves. In a program, multiple curves are stored in vector mode. Vector is similar to an array and can be accessed by subscript. However, CChart supports curve sorting, insertion, deletion, and other operations. After these operations, the subscript of the curve in the vector changes. How can we ensure the uniqueness of the curve logo? This is the ID of the curve.

Simply put, the ID number of the curve is equivalent to the ID card number of a person. The ID card number of a person is unique and unchanged throughout his life. If you are Zhao Haibin, you can have countless ID card numbers and numerous houses, but this is not part of our consideration ). The ID number of the curve remains unchanged no matter how many sort, insert, or delete operations the curve goes through.

The ID number of the curve is widely used in CChart.

The following uses AddCurve to increase the curve.

As with the previous course, create a VC6 project named Lesson05.

Write the response routine of the WM_CREATE message as follows.

Case WM_CREATE: chartWnd. attach (hWnd, kTypeXY); {double pX [360], pY [360]; double Pi = 3.1415926536; int I; for (I = 0; I <360; I ++) {pX [I] = I; pY [I] = 5.0 * sin (I * 2.0 * Pi/360.0*3.0);} chartWnd. getChart ()-> AddCurve (pX, pY, 360);} chartWnd. getChart ()-> SetTitle (_ T ("CChart line chart"); break;

Add the following sentence near the header of the Lesson05.cpp file.

#include <math.h> 

The purpose of this sentence does not need to be stupid.

The rest of the program is exactly the same as that of the previous lessons.

Run the program.

650) this. width = 650; "src =" http://img.blog.csdn.net/20130916210448562? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

A sine curve is drawn here.

The above code first prepares two arrays pX and pY to store data points, and then adds the curve with AddCurve.

Since the above function is called AddCurve, as the name suggests, the curve can continue to be added. Let's modify the response routine of the WM_CREATE message as follows.

Case WM_CREATE: chartWnd. attach (hWnd, kTypeXY); {double pX [360], pY [360]; double Pi = 3.1415926536; int I; for (I = 0; I <360; I ++) {pX [I] = I; pY [I] = 5.0 * sin (I * 2.0 * Pi/360.0*3.0);} chartWnd. getChart ()-> AddCurve (pX, pY, 360); for (I = 0; I <360; I ++) {pX [I] = I; pY [I] = 2.0 * cos (I * 2.0 * Pi/360.0*3.0);} chartWnd. getChart ()-> AddCurve (pX, pY, 360);} chartWnd. getChart ()-> SetTitle (_ T ("CChart line chart"); break;

A curve is added to the curve to show the running effect.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916210616125? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

Draw the second curve!

Theoretically, as long as your computer memory is large enough, you can add as many curves as you want. The CChart itself has no limit on the number of curves.

Some other issues are discussed below.

In Lesson 3, we use the right-click menu to bring up the legend on the screen. The following code is used to call up the legend.

In the response routine of WM_CREATE, add the following sentence before the line break.

chartWnd.GetChart()->SetUseLegend(true); 

Running effect.

650) this. width = 650; "src =" http://img.blog.csdn.net/20130916210833000? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

Now there are two curves on the screen, and there are two identifiers in the legend. The colors in the legend correspond to the colors of the curves.

A string of letters and numbers in the legend is the name of the curve. If necessary, it is no problem to change the name to Chinese. Next let's try to change the name of the first curve to "Faye Wong" and the name of the second curve to "Li yapeng ".

Add the following two sentences before the break of WM_CREATE; line.

ChartWnd. GetChart ()-> SetDataTitle (_ T ("Faye Wong"), 0); chartWnd. GetChart ()-> SetDataTitle (_ T ("Li yapeng"), 1 );

Run the program!
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916210952921? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

Here we will explain the SetDataTitle function of the CChart class. Its prototype is as follows.

voidSetDataTitle(const TCHAR* title, int nDataIndex, int nPlotIndex=0); 

The title of the first parameter is the name of the data to be set. The second parameter nDataIndex indicates the sequence number of the curve, which is calculated from 0 in the order of addition; the third parameter nPlotIndex indicates the sequence number of the subgraph. It is calculated from 0. The default value is 0, which is the first subgraph. this parameter is only valid for split views, layered views, and shared X-axis views.

Next, let's take a look at the coordinate axis. From the figure above, we can see that the coordinate axis has no title. Now we add a title to both axes.

Add the following two sentences before the break of WM_CREATE; line.

ChartWnd. GetChart ()-> SetAxisTitle (_ T ("horizontal axis"), 1); chartWnd. GetChart ()-> SetAxisTitle (_ T ("vertical axis"), 0 );

Running effect.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916211139468? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

SetAxisTitle is defined as follows.

voidSetAxisTitle(const TCHAR* title, int location, int nPlotIndex=0); 

The title of the first parameter is of course the name of the axis to be set. The location of the second parameter indicates the coordinate axis position. Here, we need to explain that the Left axis is 0 and the bottom axis is 1, the right axis is 2 and the top axis is 3. You can open the Chart. h. You can see the enum definitions commented out in the file header. The third parameter nPlotIndex indicates the sequence number of the subgraph. The default value is 0.

Finally, let's look at the background grid.

Add the following two sentences before the break of WM_CREATE; line.

chartWnd.GetChart()->SetGridLine(true, true, true, true);chartWnd.GetChart()->SetBkgndColor(RGB(224, 224, 224)); 

This is quite different from the original one.

650) this. width = 650; "src =" http://img.blog.csdn.net/20130916211228750? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>

SetGridLine is used to set the mesh drawing. The definition is as follows.

voidSetGridLine(bool MajorH=true, bool MajorV=true, bool MinorH=false, bool MinorV=false, int nPlotIndex=0); 

The CChart background mesh has two sets: The Master network and the secondary grid. The first four parameters of SetGridLine indicate whether to draw the main grid horizontal line, the main grid vertical line, the secondary grid horizontal line, and the secondary grid vertical line. The fifth parameter is the subgraph serial number, which has been explained previously.

SetBkgndColor: Specifies the background color, which is defined as follows.

voidSetBkgndColor(COLORREF color, int nPlotIndex=0); 

You don't have to be stupid enough to explain the parameters.

All right, this lesson is so stupid that you are tired and dizzy. Hurry to class.

Related Article

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.