📄 mschartintroduce.txt
字号:
//修正水平滚动条的显示
DrawPic();//调用函数,刷新曲线显示
CDialog::OnTimer(nIDEvent);
}
void CAbcDlg::DrawPic() {
char s[10];
UINT row = 1;
pDataDB->MoveFirst();
pDataDB->Move(theApp.nBottomPos);
//只从数据库中取某时间段的数据进行显示
while ((!pDataDB->IsEOF()) && (row <= 10)){
m_Chart.SetRow(row);
m_Chart.SetRowLabel((LPCTSTR)pDataDB
->m_date_time.Format(“%H:%M"));
//以采样时刻做x轴的标注
m_Chart.SetColumn(1);
sprintf(s, “%6.2f", pDataDB->m_No1);
m_Chart.SetData((LPCSTR)s);
m_Chart.SetColumn(2);
sprintf(s, “%6.2f", pDataDB->m_No2);
m_Chart.SetData((LPCSTR)s);
m_Chart.SetColumn(3);
sprintf(s, “%6.2f", pDataDB->m_No3);
m_Chart.SetData((LPCSTR)s);
pDataDB->MoveNext();
row++;
}
while ((row <= 10)){
m_Chart.SetRow(row);
m_Chart.SetRowLabel((LPCTSTR)“");
m_Chart.GetDataGrid().SetData(row, 1, 0, 1);
//采样数据不足10个点, 对应的位置不显示
m_Chart.GetDataGrid().SetData(row, 2, 0, 1);
m_Chart.GetDataGrid().SetData(row, 3, 0, 1);
row++;
}
m_Chart.Refresh();
}
void CAbcDlg::OnHScroll(UINT nSBCode,
UINT nPos, CScrollBar* pScrollBar)
{
if (pDataDB->GetRecordCount()>10)
theApp.nBottomRange = pDataDB->GetRecordCount()-10;
else
theApp.nBottomRange = 0;
m_ScrBottom.SetScrollRange(0, theApp.nBottomRange);
switch (nSBCode){
case SB_LINERIGHT:
if (theApp.nBottomPos < theApp.nBottomRange){
theApp.nBottomPos = theApp.nBottomPos + 1;
m_ScrBottom.SetScrollPos(theApp.nBottomPos);
DrawPic();
}
break;
case SB_LINELEFT:
if (theApp.nBottomPos > 0){
theApp.nBottomPos = theApp.nBottomPos - 1;
m_ScrBottom.SetScrollPos(theApp.nBottomPos);
DrawPic();
}
break;
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CAbcDlg::OnVScroll(UINT nSBCode,
UINT nPos, CScrollBar* pScrollBar) {
VARIANT var;
double max1,min1,f;
switch (nSBCode){
case SB_LINEDOWN:
f = m_Chart.GetPlot().GetAxis(1, var).
GetValueScale().GetMinimum() - 1;
if (f>=0) {//最小刻度大于等于0, 则可以滚动
m_Chart.GetPlot().GetAxis(1, var).GetValueScale().
SetMinimum(f);
f = m_Chart.GetPlot().GetAxis
(1, var).GetValueScale().GetMaximum() - 1;
m_Chart.GetPlot().GetAxis(1, var).GetValueScale().
SetMaximum(f);
pScrollBar->SetScrollPos(pScrollBar->GetScrollPos() + 1);
m_Chart.Refresh();
}
break;
case SB_LINEUP:
f = m_Chart.GetPlot().GetAxis(1, var).
GetValueScale().GetMaximum() + 1;
if (f <= 50) {//最大刻度小于等于50, 则可以滚动
m_Chart.GetPlot().GetAxis
(1, var).GetValueScale().SetMaximum(f);
f = m_Chart.GetPlot().GetAxis(1, var).
GetValueScale().GetMinimum() + 1;
m_Chart.GetPlot().GetAxis(1, var).GetValueScale().
SetMinimum(f);
pScrollBar->SetScrollPos(pScrollBar->GetScrollPos() - 1);
m_Chart.Refresh();
}
break;
}
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
----特别注意,程序中用到的关于控件的类,如CVcAxis等, 需要在AbcDlg.cpp文件的开始处说明:#include “VcAxis.h"。
----限于篇幅,文中仅仅是一个简单示例的部分代码。在实际 应用中,一般会有更多的需求,比如:对坐标轴进行缩放显示;采样有可能得不到正确 的采样值时曲线显示不连续等等,这时需要根据需求编写相应代码。
Passing an Array of Values to the Visual C++ MSChart OCX
Great step-by-step instructions to passing data to the VC++ MSChart component
Published July 27, 2000
By JL Colson
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 and click "add."
Step 3 : Add the MSChart OCX to Your Dialog
Select resources view tab and open the main dialog. (It抯 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 button 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 .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -