VC中使用TeeChart控制元件
在標頭檔案中要加上
#include "tchart.h"
#include "series.h"
#include "valuelist.h"
#include "axes.h"
#include "axis.h"
#include "pen.h"
#include "axislabels.h"
#include "teefont.h"
#include "axistitle.h"
#include "aspect.h"
#include "fastlineseries.h"
#include "titles.h"
#include "fastlineseries.h"
#include "panel.h"
#include "legend.h"
#include "tools.h"
#include "toollist.h"
#include "annotationtool.h"
#include "page.h"
TeeChart
TeeChart的主類是TChart。TChart中使用了眾多的屬性方法和事件,隨著版本的升級將越來越豐富。這使得TChart具有非常強大的功能。本文僅簡單地介紹其中一些重要類的屬性和方法。
●TChart.Height:圖表的高度(畫素);
●TChart.Width:圖表的寬度(畫素);
●TChart.Header:圖表的題頭(Ititles類);
●TChart.Series:序列(Series類的陣列);
●TChart.Axes:座標軸(Iaxes類);
●TChart.Legend:圖例(Legend類);
●TChart.Panel:面板(Ipanel
●TChart.Canvas:畫布(Canvas類)。
Series是要顯示的資料的主體。在一個圖表中可以有一個或多個序列,每個序列可以有不同的顯示型別,如Line、Bar、Pie等等。
Axes控制圖表座標軸的屬性,在預設的情況下,座標軸可以自動地根據不同的資料設定好標度範圍和間隔,當然也可以手工調整。
Legend控制圖表的圖例顯示。Legend是圖表中的一個長方形的用來顯示圖例標註的區域。可以標註Series的名稱或者Series中的專案和數值。
Panel可以設定圖表的背景。可以使用漸變的顏色或者影象檔案作為整個圖表的背景
Canvas可以讓設計者繪製自己的圖形。使用方法和Delphi中的Canvas一樣。有TextOut、 LineTo、Arc等各種畫圖的方法可以呼叫。
TChart的一些屬性實際上是其他類的變數,這些類又具有自己的屬性和方法。如Ititles類又具有Text、Color、Font等屬性,我們可以用這些屬性來設定題頭的文字、顏色和字型。
TeeChart和其他的圖表控制元件相比,有一個非常重要的特點是TeeChart可以把圖表儲存為一個JPEG格式的圖形檔案。呼叫格式如下:
TChart.Export.SaveToJPEGFile (FileName,Gray,Performance,Quality,Width,Height)
其中FileName是JPEG檔案的儲存路徑和檔名,路徑應該是作業系統中的絕對路徑,而不是IIS中的相對路徑,IIS對相應的儲存目錄應該具有寫許可權。Gray指明是否儲存為黑白影象。Performance指明JPEG是生成質量優先還是速度優先。Quality是一個0到100的整數,100時JPEG質量最好,但檔案最大;Quality越小則生成的檔案越小,但影象質量也隨之下降。
設定資訊如下(該CHART控制元件名稱為:m_Chart)
//清空chart -----------------------------------
m_Chart.ClearChart();
m_Chart.RemoveAllSeries();
//CHART框架
m_Chart.GetFrame().SetVisible(true);
m_Chart.GetFrame().SetColor(RGB(255,255,255));
/*根據自定義背景色來設定,我這裡設定背景色為白色,所以也設定為邊框為白色,這樣就看不到的,如果不加入,該顏色就預設為黑色的
//重要,否則出現邊框為預設黑色的.如果要改變背景顏色,那麼對應的此處顏色要進行更改的
*/
m_Chart.GetPanel().SetColor(RGB(255,255,255));
m_Chart.GetLegend().SetVisible(false);
// 新增3條曲線 ---------------------------------
m_Chart.AddSeries(0);
m_Chart.AddSeries(0);
m_Chart.AddSeries(0);
// 設定3條曲線的座標軸 -------------------------
m_Chart.Series(0).SetVerticalAxis(0);
m_Chart.Series(1).SetVerticalAxis(0);
m_Chart.Series(2).SetVerticalAxis(0);
m_Chart.Series(0).SetHorizontalAxis(1);
m_Chart.Series(1).SetHorizontalAxis(1);
m_Chart.Series(2).SetHorizontalAxis(1);
m_Chart.Series(0).GetXValues().SetDateTime(true);
m_Chart.Series(1).GetXValues().SetDateTime(true);
m_Chart.Series(2).GetXValues().SetDateTime(true);
// 設定3條曲線的顏色 ---------------------------
m_Chart.Series(0).SetColor(RGB(255,0,0));
m_Chart.Series(1).SetColor(RGB(0,255,0));
m_Chart.Series(2).SetColor(RGB(0,0,255));
// 設定3條曲線的名稱
m_Chart.Series(0).SetName("ZongFengGuan");
m_Chart.Series(1).SetName("LieCheGuan");
m_Chart.Series(2).SetName("ZhiDongGang");
//-----設定最大最小值
m_Chart.GetAxis().GetBottom().SetMinMax(minStar,minEnd);
// minStar,minEnd要求自己去新增,這裡用的是時間的範圍
//一般為起始時間和結束時間的範圍的
// 連線資料庫 ----------------------------------
CXDatabasedb;
_RecordsetPtrpRs;
CStringstrSql;
if(!db.Connect("ACCESS","",GetRootDir()+"//db1.mdb","",""))
{
AfxMessageBox("連線資料庫失敗!");
return;
}
strSql.Format("select * from %s where RunTime>=#%s# and RunTime<=#%s#",m_strTableName,strMinTime,strMaxTime);
pRs = db.ExecuteSql(strSql);
while(pRs!=NULL && !pRs->adoEOF)
{
// 新增資料點 ------------------------------
double dTime = oletime2chttime(COleDateTime(pRs->GetCollect("RunTime")));
double dZFGPress = var2dbl(pRs->GetCollect("ZFGPress"));
double dLCGPress = var2dbl(pRs->GetCollect("LCGPress"));
double dZDGPress = var2dbl(pRs->GetCollect("ZDGPress"));
m_Chart.Series(0).AddXY(dTime,dZFGPress,NULL,RGB(255,0,0));
m_Chart.Series(1).AddXY(dTime,dLCGPress,NULL,RGB(0,255,0));
m_Chart.Series(2).AddXY(dTime,dZDGPress,NULL,RGB(0,0,255));
pRs->MoveNext();
}
// 斷開資料庫連線 ------------------------------
db.Disconnect();