TeeChart作为一款强大好用的绘图控件,通过它可以绘制出各式各样的图表,包括2D的,还有3D的,绘制的图表美观实用。
编写文章中也借用了以下几个博文,和自己研究后的分享:
Teechart动态设计方法_bjtuwayne的博客-CSDN博客
怎样在VS2013/MFC中使用TeeChart绘图控件 - Qiengo - 博客园
MFC绘制图表《TeeChart安装、注册和使用》_qq_39851127的博客-CSDN博客_mfc图表控件
本文作者原创,转载请附上文章出处与本文链接。
TeeChart 安装步骤参考:《TeeChart安装、注册和使用》_欧阳磊的博客-CSDN博客_teechart 该博客,该博客提供的TeeChart 版本为X86版本,在64位环境下会出现报错问题,需要大家找一下64位版本资源,可以先使用32位程序来测试,找到资源后在使用64位版本来发布开发版本;
MFC Teechart 绘图控件使用目录
-
-
- #pragma once
- #include "CSeries.h"
- #include "COCX.h"
- #include
-
- using namespace std;
- // CMFCApplication1Dlg 对话框
- class CMFCApplication1Dlg : public CDialogEx
- {
- // 构造
- public:
- CMFCApplication1Dlg(CWnd* pParent = nullptr); // 标准构造函数
-
- // 对话框数据
- #ifdef AFX_DESIGN_TIME
- enum { IDD = IDD_MFCAPPLICATION1_DIALOG };
- #endif
-
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
-
-
- // 实现
- protected:
- HICON m_hIcon;
-
- // 生成的消息映射函数
- virtual BOOL OnInitDialog();
- afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
- afx_msg void OnPaint();
- afx_msg HCURSOR OnQueryDragIcon();
- DECLARE_MESSAGE_MAP()
- public:
- int i_test;
- vector<double> ViewDataRecordy;
- vector
ViewDataRecordTime; - double m_dataRecordx[10];
- double m_dataRecordY[10];
-
- CSeries data1;
- CSeries data2;
- CTChart1 m_Teechart;
- CTChart1 m_Teechart2;
- afx_msg void OnBnClickedButton1();
- void SetViewClear();
- afx_msg void OnBnClickedButton2();
- };
-
- // MFCApplication1Dlg.cpp: 实现文件
- //
-
- #include "pch.h"
- #include "framework.h"
- #include "MFCApplication1.h"
- #include "MFCApplication1Dlg.h"
- #include "afxdialogex.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
-
-
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
-
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
-
- // 对话框数据
- #ifdef AFX_DESIGN_TIME
- enum { IDD = IDD_ABOUTBOX };
- #endif
-
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
-
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
- {
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
-
-
- // CMFCApplication1Dlg 对话框
-
-
-
- CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
- : CDialogEx(IDD_MFCAPPLICATION1_DIALOG, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_TCHART1, m_Teechart);
- DDX_Control(pDX, IDC_TCHART2, m_Teechart2);
-
- }
-
- BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication1Dlg::OnBnClickedButton1)
- ON_BN_CLICKED(IDC_BUTTON2, &CMFCApplication1Dlg::OnBnClickedButton2)
- END_MESSAGE_MAP()
-
-
- // CMFCApplication1Dlg 消息处理程序
-
- BOOL CMFCApplication1Dlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
-
- // 将“关于...”菜单项添加到系统菜单中。
-
- // IDM_ABOUTBOX 必须在系统命令范围内。
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != nullptr)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
-
- // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
-
- data1 = (CSeries)m_Teechart.Series(0);
- data2 = (CSeries)m_Teechart2.Series(0);
-
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
-
- void CMFCApplication1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::OnSysCommand(nID, lParam);
- }
- }
-
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
-
- void CMFCApplication1Dlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
-
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast
(dc.GetSafeHdc()), 0); -
- // 使图标在工作区矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
-
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
- {
- return static_cast
(m_hIcon); - }
-
- void CMFCApplication1Dlg::OnBnClickedButton2()
- {
- CTime tim = CTime::GetCurrentTime(0);
- CString time = tim.Format("%H:%M:%S");
-
- if (ViewDataRecordTime.size() == 12)
- {
- vector
::iterator iterTime; - iterTime = ViewDataRecordTime.begin();
- ViewDataRecordTime.erase(iterTime);
- ViewDataRecordTime.push_back(time);
- }
- else if (ViewDataRecordTime.size() < 12)
- {
- ViewDataRecordTime.push_back(time);
- }
- else
- {
- ViewDataRecordTime.clear();
- }
-
-
- if (ViewDataRecordy.size() == 12)
- {
- vector<double>::iterator iter;
- iter = ViewDataRecordy.begin();
- ViewDataRecordy.erase(iter);
-
- ViewDataRecordy.push_back(i_test);
- }
- else if (ViewDataRecordy.size() < 12)
- {
- ViewDataRecordy.push_back(i_test);
- }
- else {
- ViewDataRecordy.clear();
- }
-
-
- i_test++;
-
- }
-
- void CMFCApplication1Dlg::OnBnClickedButton1()
- {
- SetViewClear();
- int jx = 0;
- int jy = 0;
-
-
- if (ViewDataRecordy.size() != 0 && ViewDataRecordTime.size() !=0)
- {
-
- int itestx = ViewDataRecordy.size() + 1;
- for (jx = 0; jx < itestx; jx++)
- {
- if (jx == 0)
- {
- data1.AddXY(0, 0, "0:0:0", NULL);
- }
- else
- {
- data1.AddXY((double)jx, (ViewDataRecordy[jx-1] + ViewDataRecordy[jx-1] * 0.1), ViewDataRecordTime[jx-1], NULL);
- }
-
-
- }
-
- for (jy = 0; jy < 24; jy++)
- {
- data2.AddXY((double)jy, (jy + jy * 0.1), NULL, NULL);
- }
-
- }
-
- }
-
- void CMFCApplication1Dlg::SetViewClear()
- {
- int count_line = 0;
- int count_line2 = 0;
- //在画线之前把图形清除一下,否则会覆盖,清除可用CSeries的函数Clear();
- //但是当添加多个Series后要对所有Series都clear,这是很蛋疼的,
- //因为你有时都不知道有多少个Series,这样可以先获得Series的总数在clear代码如下
- count_line = m_Teechart.get_SeriesCount();
- count_line2 = m_Teechart2.get_SeriesCount();
-
- for (long i = 0; i < count_line; i++)
- {
- ((CSeries)m_Teechart.Series(0)).Clear();
- }
- for (long i = 0; i < count_line; i++)
- {
- ((CSeries)m_Teechart2.Series(0)).Clear();
- }
- }
-
-
-
-
-
出自《TeeChart安装、注册和使用》_欧阳磊的博客-CSDN博客_teechart 一文
百度网盘链接:百度网盘 请输入提取码
提取码:6k74
本文Demo下载地址: MFCTeechart绘图控件使用-C++文档类资源-CSDN下载