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

所用控件:Custom Control

主要代码:

CXTPChartControl m_wndChartControl;

void CBarView::CreateChart(){BOOL bResult = UnzipAndShowAnswer(); //unzip and statistics the number of diff answersCXTPChartContent* 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 widthpSeries->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 YellowGreenint 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();}}

相关代码

相反,某天也许你会忽然发现,心早已沦陷。

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

相关文章:

你感兴趣的文章:

标签云: