chart控件的简单使用

[置顶] chart控件的简单使用

标签: 曲线chart
  7579人阅读  评论(4)  收藏  举报
  分类:
 

看到很多人在论坛求助  毕业设计或者其他课题中的图表应用,最简单的就是mschart   但它有很多缺点   网上教程一大把 这里说下codeproject上的 HiSpeedChart控件  用起来也是很简单的。

源地址在这里http://www.codeproject.com/KB/miscctrl/High-speedCharting.aspx


以对话框应用为例

1添加.h  .cpp进入工程,然后在Cxxdlg.h里添加

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "ChartCtrl_source/ChartCtrl.h"  
  2. #include "ChartCtrl_source/ChartLineSerie.h"  


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public:  
  2.  CChartCtrl m_Chart;  
  3.  CChartLineSerie* pLineSerie;  

同时在resource.h里添加

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #define IDC_CHARTCTRL     1001  


 

2Cxxdlg.cpp里OnInitDialog()添加

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CRect rc;  
  2.  GetClientRect(rc);  
  3.  rc.bottom -= 50;  
  4.  m_Chart.Create(this,rc,IDC_CHARTCTRL);  
  5.  m_Chart.SetEdgeType(0);  
  6.  m_Chart.SetBackColor(RGB(239,246,248));  
  7.    
  8.  CChartStandardAxis* pBottomAxis =   
  9.   m_Chart.CreateStandardAxis(CChartCtrl::BottomAxis);  
  10.  pBottomAxis->SetMinMax(0, 10);  
  11.  CChartStandardAxis* pLeftAxis =  
  12.   m_Chart.CreateStandardAxis(CChartCtrl::LeftAxis);  
  13.  pLeftAxis->SetMinMax(0, 10);  
  14.  pLineSerie = m_Chart.CreateLineSerie();  
  15.  double XValues[10],YValues[10];  
  16.  for (int i=0;i<10;i++)  
  17.  {   
  18.   XValues[i] = YValues[i] = i;  
  19.  }  
  20.  pLineSerie = m_Chart.CreateLineSerie();  
  21.  pLineSerie->SetPoints(XValues,YValues,10);  
  22.  pLineSerie->SetColor(RGB(255,0,0));  


 

ok end

-------------------------------------2014.12.26-----------------------------

给曲线添加个label标签也比较简单,记录如下

第一种是静态标签,  在上面代码的基础上添加一句

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. pLineSerie->CreateBalloonLabel(5,_T("X = 5,Y = 5"));  
还有一种是自定义标签,需要从CChartLabelProvider类派生出新类,实现定制标签功能,有一个作者派生的CChartBalloonLabel类可以参考。效果如图

 

 =================================================================================

2014.12.27经常有需要实时显示曲线坐标点数字的功能,在这个类里实现比较方便,先看效果图

这里主要用Cursor的属性。从CChartCursor派生是最好的方法,这里这样派生

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. class CCustomCursorListener : public CChartCursorListener  
  2. {  
  3. public:  
  4.     void OnCursorMoved(CChartCursor *pCursor, double xValue, double yValue)  
  5.     {  
  6. //      TChartStringStream ssText;  
  7. //      ssText << _T("Cursor moved: xPos=") << xValue << _T(", yPos=") << yValue;  
  8. //      TChartString strText = ssText.str();  
  9.     //  MessageBox(NULL,strText.c_str(), _T("Info"), MB_OK);  
  10.         x = xValue;  
  11.         y = yValue;  
  12.   
  13.         SendMessage(m_hwnd,MESSAGE_UPDATEPOS,0,0);  
  14.         // Do something with the string...  
  15.     }  
  16.   
  17.     void GetHwnd(HWND hwnd)  
  18.     {  
  19.         m_hwnd = hwnd;  
  20.     }  
  21.     HWND m_hwnd;  
  22. };  

 为了传递出鼠标移动的消息,需要获得主窗口的句柄,添加了GetHwnd函数。

同时在工程里添加全局变量double x,y;dlg.h里添加

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CCustomCursorListener* m_pCursorListener;  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span>afx_msg LRESULT OnUpdateData(WPARAM wp,LPARAM lp);  
dlg.cpp里添加
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ON_MESSAGE(MESSAGE_UPDATEPOS,OnUpdateData)  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CChartCrossHairCursor* pCrossHair =  
  2.         m_Chart.CreateCrossHairCursor();  
  3.       
  4.     HWND hWnd = this->GetSafeHwnd();  
  5.     m_pCursorListener = new CCustomCursorListener;  
  6.     m_pCursorListener->GetHwnd(hWnd);      
  7.     pCrossHair->RegisterListener(m_pCursorListener);  
对话框添加了一个CStatic控件之后

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. LRESULT CHiChartDlg::OnUpdateData(WPARAM wp,LPARAM lp)  
  2. {  
  3.   
  4.     CString s;  
  5.     s.Format(_T("x = %.2f,y = %.2f"),x,y);  
  6.     CStatic* pStatic;  
  7.     pStatic = (CStatic*)GetDlgItem(IDC_STATIC);  
  8.     pStatic->SetWindowText(s);  
  9.     return 1;  
  10. }  

就可以实现了

0
0
C#中的CHART控件是用于绘制图表和数据可视化的工具。使用CHART控件可以创建各种类型的图表,如折线图、柱状图、饼图等。 要使用CHART控件,首先需要在项目中添加对System.Windows.Forms.DataVisualization.Charting命名空间的引用。然后在窗体中添加一个Chart控件,可以通过拖放或者在代码中动态创建。 以下是一个简单的示例,展示如何使用CHART控件创建一个折线图: 1. 在窗体中添加一个Chart控件,并设置其大小和位置。 2. 在窗体的Load事件中,编写代码以设置图表的属性和数据。 3. 使用Chart.Series属性来添加和配置图表系列。 4. 使用Chart.Series.Points属性来添加和配置系列的数据点。 下面是一个示例代码: ```csharp using System; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace ChartExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 设置图表的标题和图例 chart1.Titles.Add("Sales Report"); chart1.Legends.Add("Legend"); // 创建一个系列 Series series = new Series(); series.Name = "Sales"; series.ChartType = SeriesChartType.Line; // 添加数据点 series.Points.AddXY("Jan", 100); series.Points.AddXY("Feb", 150); series.Points.AddXY("Mar", 200); series.Points.AddXY("Apr", 120); // 将系列添加到图表 chart1.Series.Add(series); } } } ``` 在上述代码中,我们创建了一个折线图,并添加了一些数据点。可以根据需要进一步自定义图表的样式和属性。 希望这个示例可以帮助你开始使用C#的CHART控件。如果还有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值