VC的一个简单问题!!!(Microsoft Chart Control 怎么用?)(我很急!!!)

dengred 2002-04-16 11:01:29
我要将得到的数据用图形的方式表示出来
在VC中有一个insert Active Control
在其中有一个Microsoft Chart Control ,
就是这个
我是否可以用它来实现?(我该如何用这个东东)
即将文本信息用图形方式(柱状图,柄状图,曲线等)表示。
请大虾指点!!!!

...全文
466 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengred 2002-04-25
  • 打赏
  • 举报
回复
请继续呀!!!
楼上这位!!
hd506lg 2002-04-20
  • 打赏
  • 举报
回复
用Chart控件绘制动态图表

---- 进行程序设计时,选用一个合适的ActiveX控件,有时可大大减少编程工作量。ActiveX 控件(又称OCX)基于COM技术,作为独立的软件模块,它可以在任何程序设计语言 中插入使用。本文仅以VC++为例说明Chart控件的使用。
---- Chart控件指Mschart.ocx(5.0版)或Mschrt20.ocx(6.0 版),是Visual Studio自带的ActiveX控件之一,其属性、事件很多,功能非常强大,可 实现柱状直方图、曲线走势图、饼状比例图等,甚至可以是混合图表,可以是二维或三 维图表,可以带或不带坐标系,可以自由配置各条目的颜色、字体等等。
一 安装和使用Chart控件
----在用到Chart控件的项目中安装该控件:从Project-> Add to Project-> Components And Controls-> Registered Active Xcontrols,选择Chart控件,则 ClassWizard会生成相应的C++类,其中类CMSChart是由CWnd派生来的,它是Chart 控件的主要类,其他的类全部是由COleDispatchDriver派生来,控制控件中的相应对象, 完成各部分相关功能,如CvcAxis类是实现坐标轴相关功能的源代码。同时在项目的控件 工具箱上会出现代表Chart控件的按钮,使用时把Chart控件按钮从工具箱拖到对话框中, 调整大小即可。
----Chart控件至少有45个属性、9个方法、49个事件,在这 里就不一一列举了。
---- 在设计中,我们可以在主要属性页里修改各属性的属性 值:右击对话框窗口中的Chart控件,选择“Properties”菜单项,就会弹出主要属性页对 话框,对其中各属性值进行设置。有些属性在主要属性页里没有列出,只能编程修改。 另外要动态绘制图表,必须掌握对控件的编程控制。
---- 首先在对话框类中定义控件变量,以便编程时操纵控件。 如对话框类定义如下:
class CAbcDlg : public CDialog{
public:

CAbcDlg(CWnd* pParent = NULL);
/{{AFX_DATA(CAbcDlg)
enum { IDD = IDD_ABC_DIALOG };
CMSChart m_Chart;
//}}AFX_DATA
......
;





----ActiveX控件的属性和方法在控件内部对应唯一一个整数 索引值,编程时可以通过索引来设置或获取控件的属性值,也可以通过调用控件的C+ +类(在这里就是CMSChart)的成员函数设置或获取控件的属性值及调用控件的方法。 例如:





----在CMSChart类实现中有如下代码:





CString CMSChart::GetData(){


CString result;


InvokeHelper(0x9, DISPATCH_PROPERTYGET,


VT_BSTR, (void*)&result, NULL);


return result;


}


void CMSChart::SetData(LPCTSTR lpszNewValue){


static BYTE parms[] =VTS_BSTR;


InvokeHelper(0x9, DISPATCH_PROPERTYPUT,


VT_EMPTY, NULL, parms,lpszNewValue);


}


void CMSChart::Refresh(){


InvokeHelper(DISPID_REFRESH,


DISPATCH_METHOD, VT_EMPTY, NULL, NULL);


}





----这段代码表明:属性“Data”索引值为0x9,我们可以调 用函数SetData对图表中某点的值进行设置。索引值为DISPID_REFRESH的方法 “Refresh”,调用它进行刷新。如:





CString str=“34.5";


m_Chart.SetData(str);


m_Chart.Refresh();


......





----阅读CMSChart类的实现会发现,有些属性的值不是普通 的BOOL、CString等数据类型,而是另一个控件驱动类的类变量,如:





CVcPlot CMSChart::GetPlot(){


LPDISPATCH pDispatch;


InvokeHelper(0x28, DISPATCH_PROPERTYGET,


VT_DISPATCH, (void*)&pDispatch, NULL);


return CVcPlot(pDispatch);


}





----在CVcPlot类的实现中有如下代码:





CVcAxis CVcPlot::GetAxis


(long axisID, const VARIANT& Index){


LPDISPATCH pDispatch;


static BYTE parms[] =VTS_I4 VTS_VARIANT;


InvokeHelper(0x1f, DISPATCH_PROPERTYGET,


VT_DISPATCH, (void*)&pDispatch, parms, axisID, &Index);


return CVcAxis(pDispatch);


}





----而CVcAxis类的实现中有如下代码:





CVcValueScale CVcAxis::GetValueScale(){


LPDISPATCH pDispatch;


InvokeHelper(0x9, DISPATCH_PROPERTYGET,


VT_DISPATCH, (void*)&pDispatch, NULL);


return CVcValueScale(pDispatch);


}





----而CVcValueScale类的实现中又有如下代码:





void CVcValueScale::SetMaximum(double newValue){


static BYTE parms[] =VTS_R8;


InvokeHelper(0x3, DISPATCH_PROPERTYPUT,


VT_EMPTY, NULL, parms,newValue);


}





----这正是Chart控件的灵活性所在,根据上述代码,如下的 调用:





VARIANT var;


m_Chart.GetPlot().GetAxis(1, var).GetValueScale()


.SetMaximum(50.0);


可实现把纵坐标的最大刻度设为50.0。





----控件触发的事件,如Click、MouseDown等,如果需要处 理,可以通过ClassWizard在对话框类中定义相应的处理函数,实现相关的处理功能。





二 动态绘制图表实例


---- 在一个温度采集系统中,希望把采集来的各项温度值 实时显示,用Chart控件绘制曲线走势图:


各温度项以不同颜色的曲线表示;


横坐标为时间,纵坐标为温度值,均要求滚动显示;


在每次采样完成后,刷新屏幕。








hd506lg 2002-04-20
  • 打赏
  • 举报
回复
再来一个,够了吧,做那东西应该可以了
到这里看:
http://codeguru.earthweb.com/controls/Waterfall.shtml


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)

CChart3d::SetRange3d(double Xmin, double Xmax,
double Ymin, double Ymax,
double Zmin , double Zmax)
Default: SetRange3d(0,100,0,20,0,100)

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)

Example:

mChart.Create(WS_CHILD|WS_VISIBLE,Rect,this,12000);

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 :

void CWFDemoView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
m_Chart2d.PrintChart(pDC,500,200);
m_Chart3d.PrintChart(pDC,500,1800);
}
hd506lg 2002-04-20
  • 打赏
  • 举报
回复
下面够你看了

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;

DWORD numElements[] = {10, 10}; // 10x10

// Create the safe-array...

saRet.Create(VT_R8, 2, numElements);

// Initialize it with values...

long index[2];

for(index[0]=0; index[0]<10; index[0]++) {
for(index[1]=0; index[1]<10; index[1]++) {
double val = index[0] + index[1]*10;
saRet.PutElement(index, &val);
}
}

// Return the safe-array encapsulated in a VARIANT...

m_Chart.SetChartData(saRet.Detach());

m_Chart.Refresh;

Step 5: Building and Running the Application
Build and execute your app, then click the "Go" button. Here is the result:
dengred 2002-04-20
  • 打赏
  • 举报
回复
hd506lg(让我们一起来吧) :
这是msdn上的,而且是VB的代码。
有谁知道在VC下代码怎么写???
渴望你的帮助!!!!!!!!
谢谢!!!!!!!!
dengred 2002-04-17
  • 打赏
  • 举报
回复
我怎么没看出怎么设置图形的属性呀??
请讲的详细一点!!
谢谢啦!!!!!!!
hd506lg 2002-04-16
  • 打赏
  • 举报
回复

MSChart 控件的数据

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 查询字符串。

' 首先将路径改为到您机器的一个有效路径。
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & _
"C:\Program Files\Microsoft Visual Studio\VB98\nwind.mdb" ' <-更改该路径。

' 打开连接。
cn.Open

' 创建一个只检索四个字段的查询。
strQuery = "SELECT ProductName, UnitPrice, " & _
"UnitsInStock, UnitsOnOrder FROM Products WHERE SupplierID = 1"
' 打开记录集。
rsProducts.Open strQuery, cn, adOpenKeyset
' 将 DataSource 设置为该记录集。
With MSChart1
.ShowLegend = True
Set .DataSource = rsProducts
End With
End Sub
hd506lg 2002-04-16
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000/html/xlhowChartEvents.asp


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.
kunjun_oy 2002-04-16
  • 打赏
  • 举报
回复
文档是你最大的助手,看看它自己的帮助信息。
dengred 2002-04-16
  • 打赏
  • 举报
回复
能讲得更仔细点吗?
谢谢啦!!!
vcguy 2002-04-16
  • 打赏
  • 举报
回复
看Excel的帮助!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧