承接上一課餅圖教學,本節介紹另一種常用圖表——柱狀圖。同樣採用極簡代碼實現專業效果。
建立 Lesson07 專案,修改 WM_CREATE:
case WM_CREATE:
chartWnd.Attach(hWnd, kTypeStem);
{
double pY[5];
pY[0] = 3.0; pY[1] = 5.0; pY[2] = 4.0; pY[3] = 2.0;
chartWnd.GetChart()->AddStems(pY, 4);
chartWnd.GetChart()->SetDataTitle("代理商甲銷量", 0);
}
break;
使用 AddStems 新增數據序列,kTypeStem 為柱狀圖型別代碼。執行結果:
透過 SetStemLabel 為各柱子加上季度標籤:
chartWnd.GetChart()->SetStemLabel(_T("一季度"), 0);
chartWnd.GetChart()->SetStemLabel(_T("二季度"), 1);
chartWnd.GetChart()->SetStemLabel(_T("三季度"), 2);
chartWnd.GetChart()->SetStemLabel(_T("四季度"), 3);
執行效果:
CChart 支援多組數據並列顯示,只需重複呼叫 AddStems:
pY[0] = 4.0; pY[1] = 1.0; pY[2] = 6.0; pY[3] = 3.0;
chartWnd.GetChart()->AddStems(pY, 4);
chartWnd.GetChart()->SetDataTitle("代理商乙銷量", 1);
雙序列對比圖:
加入總標題:
chartWnd.GetChart()->SetTitle(_T("代理商銷量"));
最終成果:
柱狀圖同樣支援滑鼠互動功能,本課到此結束。