[VC] 在VC++中使用MSChart表格控制項(小結)

來源:互聯網
上載者:User

MSChart,微軟的一個很不錯的畫圖控制項,以下轉載自: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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.