1. MSChart制图类
1.1 添加MSChart控件
MSChart是VC++6.0中自带的一个特殊控件类,用于绘制坐标曲线图。如果要使用这个控件,则可以按下图的示意进行添加此控件。
1.2 MSChart控件的使用方法
首先在要使用的类的实现文件中包含如下头文件:
#include "VcPlot.h"
#include "VcAxis.h"
#include "VcValueScale.h"
#include "VcSeriesCollection.h"
#include "VcSeries.h"
#include "VcPen.h"
#include "VcCategoryScale.h"
#include "VcColor.h"
#include "VcDataGrid.h"
#include "VcBackdrop.h"
#include "VcFill.h"
#include "VcBrush.h"
#include "VcDataPoints.h"
#include "VcDataPoint.h"
#include "VcDataPointLabel.h"
#include "VcAxisTitle.h"
#include "math.h"
在要使用的类的头文件中包含:
#include "mschart.h"
本系统中按照如下函数调用来实现MSChart类绘制故障树重要度曲线的功能(CDrawImp是调用MSChart的类)。
1.2.1 类中变量定义
class CDrawImp : public CDialog
{
// Construction
public:
void DrawChart(int type);
void initmschart();
CMSChart m_Chart;
……
}
1.2.2 类中MSChart的初始化函数
void CDrawImp::initmschart()
{
// 下面两句改变背景色
m_Chart.GetBackdrop().GetFill().SetStyle(1);
// 显示图例
m_Chart.SetShowLegend(FALSE);
m_Chart.SetColumn(1);
m_Chart.SetChartType(3);
// 栈模式
m_Chart.SetStacking(FALSE);
// Y轴设置
VARIANT var;
//不自动标注Y轴刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);
//Y轴刻度10等分
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(10);
//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);
//不自动标注X轴刻度
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);
//每刻度一个标注
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(0);
//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("基本故障事件(对应表2中的序号)");// X轴名称
// 1条曲线
m_Chart.SetColumnCount(1);
// 数据点类型显示数据值的模式(对柱柱状图和点线图有效)
// 0: 不显示 1: 显示在柱状图外
// 2: 显示在柱状图内上方 3: 显示在柱状图内中间 4: 显示在柱状图// 内下方
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(0);
}
void CDrawImp::DrawChart(int type)
{
int nRowCount =bs.FactDictoryList.GetCount();
VARIANT var;
m_Chart.SetRowCount(nRowCount);
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 0, 255);
char buf[32];
/* double max = 0.0;
double min = 10.0;
double temp;
for (int i=0; i<nRowCount; i++)
{
switch(type)
{
case 0:
temp = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(i)).FactCF;
break;
case 1:
temp = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(i)).cfimportance;
break;
case 2:
temp =bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(i)).structimportance;
break;
case 3:
temp = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(i)).edgeimportance;
break;
default:
break;
}
if (max < temp)
max = temp;
if (min > temp)
min = temp;
}*/
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(10); // Y轴最大刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0); // Y轴最小刻度
double c;
for(int row = 1; row <= nRowCount; ++row)
{
m_Chart.SetRow(row);
sprintf(buf, "%d", row);
m_Chart.SetRowLabel((LPCTSTR)buf);
switch(type)
{
case 0:
c =bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(row-1)).FactCF;
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 0, 255);
// Y轴名称
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("基本事件概率(-lg)");
break;
case 1:
c = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(row-1)).cfimportance;
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(255, 0, 0); // Y轴名称
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("概率重要度(-lg)");
break;
case 2:
c = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(row-1)).structimportance;
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 255, 0);
//Y轴名称
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("结构重要度");
break;
case 3:
c = bs.FactDictoryList.GetAt(bs.FactDictoryList.FindIndex(row-1)).edgeimportance;
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(255, 0, 255);
// Y轴名称
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("临界重要度(-lg)");
break;
default:
break;
}
m_Chart.GetDataGrid().SetData(row, 1, -log10(c), 0);
}
m_Chart.Refresh();
}
1.1.2.3 添加ON_SIZE消息响应函数
void CDrawImp::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if( m_Chart.GetSafeHwnd() )
m_Chart.MoveWindow( 0, 0, cx, cy );
}
1.1.2.4 CDrawImp的OnInitDialog函数种创建MSChart
BOOL CDrawImp::OnInitDialog()
{
CDialog::OnInitDialog();
m_radio = 0;
m_combo.SetCurSel(0);
UpdateData(FALSE);
CRect rc;
GetClientRect(&rc);
rc.top += 80;
rc.bottom -= 15;
rc.left += 15;
rc.right -= 15;
m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10);
initmschart();
DrawChart(0);
CString s;
s.Format("顶事件发生概率为: %f",topcf);
GetDlgItem(IDC_TOPCFSTATIC)->SetWindowText(s);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}