您的位置:首页 > 编程语言 > PHP开发

mfc 利用CXTPChartControl画柱状图,响应柱状图单击事件

2013-10-30 17:39 651 查看
所用控件:Custom Control

主要代码:

CXTPChartControl m_wndChartControl;

void CBarView::CreateChart()

{
BOOL bResult = UnzipAndShowAnswer();    //unzip and statistics the number of diff answers
CXTPChartContent* pContent = m_wndChartControl.GetContent();

//pContent->GetLegend()->SetVisible(TRUE);    去除右上角坐标轴标注

CXTPChartTitle* pTitle = pContent->GetTitles()->Add(new CXTPChartTitle());
pTitle->SetText(_T("客观题统计"));

CXTPChartSeries* pSeries = pContent->GetSeries()->Add(new CXTPChartSeries());

//pSeries->SetName(_T("Quoted"));
m_pXTPChartBarStyle = new CXTPChartBarSeriesStyle();
m_pXTPChartBarStyle->SetBarWidth(0.24);    //set the histogram width
pSeries->SetStyle(m_pXTPChartBarStyle);

int nPersonNum = CStudentStatusView::s_nStuAnsNumber/*g_nSockCount*/;
if (0 == nPersonNum)
{
//AfxMessageBox("在线人数为0,请同学们重新建立连接!");
return;
}
int nPercent;
CString sLabel;

    //根据问题类型,分情况创建柱状图
if (QSN_CSTRING_TYPE_SINGLE == m_sQuesType || QSN_CSTRING_TYPE_MULTI == m_sQuesType)
{
//单选题,则创建相应的选项柱状图
int nPos = 0;
float ftmp = m_nAnswerA;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("A"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerA,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;

ftmp = m_nAnswerB;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("B"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerB,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;

ftmp = m_nAnswerC;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("C"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerC,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;

ftmp = m_nAnswerD;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("D"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerD,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;

ftmp = m_nAnswerE;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("E"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerE,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;

ftmp = m_nAnswerF;
nPercent = (ftmp/nPersonNum + 0.005)*100;
pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("F"), nPercent));
sLabel.Format("%d人,%d%s",m_nAnswerF,nPercent,"%");
pSeries->GetPoints()->GetAt(nPos)->SetLabelText(sLabel);
nPos ++;
//pSeries->GetPoints()->Add(new CXTPChartSeriesPoint(_T("G"), m_nAnswerG));
m_nGridcount = 5*6;    //7,柱状图数目,5,每个柱状图5个小格
}

//set the digram color  LimeGreen  OliveDrab YellowGreen
int nPointNum = pSeries->GetPoints()->GetCount();
for(int nIndex = 0;nIndex < nPointNum;nIndex ++)
{
pSeries->GetPoints()->GetAt(nIndex)->SetColor(CXTPChartColor::YellowGreen);

}

CXTPChartDiagram2D* pDiagram = DYNAMIC_DOWNCAST(CXTPChartDiagram2D, pSeries->GetDiagram());
ASSERT (pDiagram);

pDiagram->GetAxisX()->GetTitle()->SetVisible(TRUE);
//pDiagram->GetAxisX()->GetTitle()->SetText(_T("Answers"));

pDiagram->GetAxisY()->GetRange()->SetMaxValue(100.0);
pDiagram->GetAxisY()->GetRange()->SetAutoRange(FALSE);
pDiagram->GetAxisX()->GetRange()->SetZoomLimit(1);
//pDiagram->GetAxisY()->GetRange()->SetZoomLimit(10);
pDiagram->GetAxisY()->SetAllowZoom(FALSE);

}

void CBarView::OnChartClick(NMHDR* /*pNMHDR*/, LRESULT* /*pResult*/)

{
CPoint pt;
GetCursorPos(&pt);
m_wndChartControl.ScreenToClient(&pt);
CXTPChartElement* pElement = m_wndChartControl.HitTest(pt);

if (DYNAMIC_DOWNCAST(CXTPChartSeriesPoint, pElement))
{
CXTPChartSeriesPoint* pPoint = (CXTPChartSeriesPoint*)pElement;
CXTPChartString sValue = pPoint->GetArgument();

teaAskChart.m_sOrder = sValue;

teaAskChart.DoModal();
}

}

相关代码  http://download.csdn.net/detail/leighton_52/6477439
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息