Environment: Visual C++ 6.0,Win 95/98,NT4.0.
The print routine has been tested with HP Laserjet 4ML and Lexmark 3200, with Acrobat Writer by setting the resolution at 600 dpi.
Introduction
The CChart class is the class derived from CWnd class. The class provides the functionality of Windows plotting chart control . The chart plotted from this class will look like the output of an Oscilloscope . By the way, I found that there is an article like this already posted here . So CChart3d is the derived class from CChart that will be able to plot data in 3D style.The demo project will will plot data in 2D and 3D like a Waterfall plot found in an expensive Signal Analyser.
Implementing
CChart and CChart3d to your project First add these four files to your project. Chart3d.cpp , Chart3d.h , Chart.cpp and Chart.h . Then you can add the object to like this :
CChart m_Chart2d ;
CChart3d m_Chart3d;
After that you can customize,create and then update new data to chart respectively. In the demo project you can find the implementation of CChart and CChart3d in the routine
CWFDemoView::InitialUpdate(); // for customizing and creating chart
CWFDemoView::OnTimer(); // for updating data to chart
CWFDemoView::OnPrint(CDC *pDC); // for printing chart.
Customize Control
Customize control of chart can be done before and after the chart is created. If you change setting after the chart was created then call function Invalidate() to redrawn the chart.
Setting Chart Title can be done by calling the function
SetChartTitle(Cstring str)
Setting Range of each axis can be done by calling the following functions:
CChart::SetRange(double Xmin, double Xmax,
double Ymin, doubleYmax)
Default: SetRange(-10,10,-10,10)
Setting the Axis’s Label can be done by calling the functions:
CChart::SetAxisLabel(Cstring strLabelX , Cstring strLabelY)
Setting the number of grid scale for each axis and the labels to be plotted on screen can be done by calling the functions:
CChart::SetGridNumber(int nGridX , int nGridY)
CChart3d::SetGridNumber3D(int nGridX, int nGridY, int nGridZ)
Note: Grid labels will be automatic drawn according to the number of grid setting.
Setting the Axis style by calling the function:
CChart::SetAxisStyle(int nStyle)
//0: Single Quadrant
//1: Double Quadrant
//2: 4 Quadrant *default
Customize color on chart Background Color can be modified with variable: m_BGColor. Axis color can be modified with variable: m_AxisColor. Grid color can be modified with variable: m_GridColor. Series plot color can be modified with variable: CSerie::m_plotColor.
Example
mChart.m_BGColor = RGB(255,0,0,) //Set background color to red
mChart.m_AxisColor = RGB(0,0,0); // Set background color to black
mChart.m_GridColor = RGB(120,120,120); // Set grid color to gray .<
mChart.mpSerie[0].m_plotColor = RGB(0,255,0) ; //Set series 0 color to green
Set the number of series on chart by modify variable
CChart::nSerieCount.
Note: The maximum series on the code is 60 but you can assemble it and change to any number is your want.
Allocate number of points for all series by calling function:
CChart::AllocSerie(int nSerie)
Caution : Setting the number of series has to be done before calling this function
Working with the Chart
Creating Chart - After you finished customizing the chart then call the function:
Create(DWORD dwStyle, CRect &rect, CWnd *pParent, UINT id)
Updating Chart - You can update data for each series by calling function :
SetXYValue(double x , double y , int index , int nSerieIdx).
If you want chart to be redrawn function Invalidate() should be called . The chart background will be drawn as it was when the chart was first created then it will save the background in the buffer. But if you changed background color or grid color then you need to call the Invalidate() function with argument FALSE to force the chart background to be redrawn .
Printing Chart - In the demo project you can preview and print the chart . I have test the program with several printers. The function CChart::PrintChart(CDC *pDC,int x ,int y) is used for printing the chart.
In the demo project I added this function in OnPrint(CDC *pDC) in CFormView class as example :
Step 1 : Creating the Project
Start Visual C++ en create a simple dialog based application labelled "Graph"
Step 2 : Add the MSChart OCX to Your Project
Select "project menu" option and select "Components and contols" and then choose the MSChart component en click "add"
Step 3 : Add the MSChart OCX to Your Dialog
Select resources view tab en open the main dialog (It’s a simple dialog based application). Drop the ocx on your dialog.
Now, label your Chart "IDC_MSCAHRT1"
Now, choose menu option "Classwizard" to create a member variable of your chart labelled "m_Chart"
Step 4: Add the Code
Now add a bouton labeled "Go" to your dialog. Double click it to edit the code and add the following code in the On_Go function:
COleSafeArray saRet;
Microsoft Chart 控件是一个数据绑定控件,允许您以图形方式表示数值数据。不像其他数据绑定控件,Chart 控件不能同 Remote Data 控件一起使用,也不能同 Data 控件一起使用。它可以同 ADO Data Control、ADO Recordset 以及 Data Environment 一起使用。该示例将显示如何打开一个 ADO Recordset,该 ADO Recordset 包含了您想要显示的字段,并且 Chart 控件的 DataSource 属性设置为 Recordset 对象。如果第一个字段包含字符串数据,则该数据将作为 X 轴标签使用。
下面的示例显示了三组数据,先创建一个具有四个字段的 Recordset 对象;第一个字段包含 X 轴的标签,而其他的字段作为系列数据显示。
Option Explicit
' 确保设置一个对 Microsoft ActiveX Data
' Objects 2.0 Library 的引用。
Private rsProducts As New ADODB.Recordset
Private cn As New ADODB.Connection
Private Sub Form_Load()
Dim strQuery As String ' SQL 查询字符串。
CHART events occur when the user activates or changes a chart. Events on chart sheets are enabled by default. To view the event procedures for a sheet, right-click the sheet tab and select View Code from the shortcut menu. Select the event name from the Procedure drop-down list box.
Activate
BeforeDoubleClick
BeforeRightClick
Calculate
Deactivate
DragOver
DragPlot
MouseDown
MouseMove
MouseUp
Resize
Select
SeriesChange
Note To write event procedures for an embedded chart, you must create a new object using the WithEvents keyword in a class module. For more information, see Using Events with Embedded Charts.
This example changes a point's border color when the user changes the point value.
Private Sub Chart_SeriesChange(ByVal SeriesIndex As Long, _
ByVal PointIndex As Long)
Set p = ActiveChart.SeriesCollection(SeriesIndex). _
Points(PointIndex)
p.Border.ColorIndex = 3
End Sub
Calculate Event
Chart object: Occurs after the chart plots new or changed data.
Worksheet object: Occurs after the worksheet is recalculated.
Syntax
Private Sub object_Calculate()
object Chart or Worksheet. For information about using events with the Chart object, see Using Events with the Chart Object.