天天看點

[VC] 在VC++中使用MSChart表格控件(小結)(轉載)

SChart,微軟的一個很不錯的畫圖控件,以下轉載自:http://blog.tom.com/easehaibo/article/1374.html

1. 在工程中添加MSChart控件

Project—〉Add to Project—〉Registered ActiveX Controls,選中Microsoft Chart Control 6.0(SP4)(OLEDB)

點選Insert,一路确定

2.         在用到控件的地方加上相應的頭檔案,mschart.h,還有其他比較常用的頭檔案:(重要!上面兩個例子沒有提到,可能都是在視窗中使用,在對話框應用中需要添加)

#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 "VcAxisScale.h"

#include "VcAxisGrid.h"

3.         定義并create控件對象

CMSChart m_Chart;

m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10);// this 為視窗指針

4.         設定控件的屬性

//設定标題

       m_Chart.SetTitleText(Title);//Title 為CString類型

//設定棧模式

       m_Chart.SetStacking(FALSE);

//設定行數及列數

m_Chart.SetRowCount(iRowCount);//iRowCount和iColumnCount為int型

m_Chart.SetColumnCount(iColummCount);

//設定控件的資料,int型的iRow,iColumn可看成是資料所在的行和列,Data即是所要設的數值型資料

       m_Chart.GetDataGrid().SetData(iRow, iColumn, Data, 0);

//設定圖例

       m_Chart.SetShowLegend(TRUE);//顯示圖例

       m_Chart.SetColumn(iColumn);

       m_Chart.SetColumnLabel(slegend);//slegend為CString型

//設定x軸下方顯示的标記

       m_Chart.SetRow(iRow);

       m_Chart.SetRowLabel(sLabel);//sLabel為CString型

//設定x軸及y軸的标題。xTitle和yTitle為CString型

       m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText(xTitle); //x軸

       m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText(yTitle); //y軸

//設定控件類型

       m_Chart.SetChartType(3);//3:曲線型;1:條形;14:餅圖

//設定背景顔色

       m_Chart.GetBackdrop().GetFill().SetStyle(1);

       m_Chart.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255);

//設定資料系列的顔色:如果是曲線圖則對應每條曲線的顔色

       for (int i = 1; i <= m_Chart.GetColumnCount(); i++ )

       {//這裡設定為随機顔色

              m_Chart.GetPlot().GetSeriesCollection().GetItem(i).GetPen().GetVtColor().Set(rand() * 230 / RAND_MAX, rand() * 230 / RAND_MAX, rand() * 230 / RAND_MAX);

              m_Chart.GetPlot().GetSeriesCollection().GetItem(i).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1);

       }

//設定x軸的其他屬性

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE); // 不自動标注X軸刻度

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);// 每刻度一個标注

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1); // 每刻度一個刻度線

//自動标注y軸

       m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(TRUE);

在這裡,也可以手動設定y軸,如下:

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);     // y軸最大刻度為100

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);         // y軸最小刻度為0

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(5);   // 将y軸刻度5等分

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);   // 每刻度一個刻度線

m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("YourTitle");     // y的軸名稱

//不要與x軸垂直的表格線

       m_Chart.GetPlot().GetAxis(0,var).GetAxisGrid().GetMajorPen().SetStyle(0);// no x grids

//隐藏第二y軸,即右邊的y軸

       m_Chart.GetPlot().GetAxis(2,var).GetAxisScale().SetHide(TRUE);

//重新整理控件

       m_Chart.Refresh();

繼續閱讀