原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control
本文翻译在CodeProject上的介绍(主要仍是谷歌翻译,看不太明白的地方,请对比原文,敬请原谅),方便本身和后面人的学习(花费了两天时间,但愿是值得的)。推荐一个前辈写的东西:TeeChart替代品,MFC下好用的高速绘图控件-(Hight-Speed Charting),本身也转载了这篇文章,在转载的文章中根据本身的实验修改了一些东西,修改了什么如今也不记得了,文章地址:TeeChart替代品,MFC下好用的高速绘图控件-(Hight-Speed Charting) .
下面是我能找到的资料前三个是原文提供的(要想找到最新的,到原文中找),后三个是本身在其余的博客中找到的。
ChartCtrl_demo.zip
ChartCtrl_doxygen.zip
ChartCtrl_source.zip
ChartCtrl.pdf
MFC动态绘制曲线图-HightSpeedChart实现.rar–>上传还没成功
TeeChart和HightSpeedChart动态绘图.rar–>上传还没成功html
这个控件是用来展现2D数据,以下面的图片。
git
对于我以前的一个项目,我须要在图表控件上显示连续的数据流。 我决定开发本身的控件,由于我找不到任何能够提供所需灵活性的自由软件控件。 其中一个主要的限制是,控件必须绘制大量的数据,并可以迅速显示它(在Pocket PC上)。 控件可以经过仅绘制新的数据点而不是完整的数据序列来作到这一点而且图表还可以显示静态数据。
这种控件是我长时间工做的结果,并且费尽周折地为了提供足够的灵活性来供须要它的人使用。 对于使用者反馈我表示由衷的感谢:一个邮件,留言板中的一一句话或只是对本文评级。 当我不知道是否还有人使用它时,我就没有必要维护这个控件了。redis
这个控件是我花费很长时间的开发的结果,所以我对代码的使用放置一些小条件:安全
该代码能够以编译的形式用于任何非商业和商业目的。代码能够被从新开发,只要它提供做者名字和完整的免责声明。 更改源代码须要获得做者的赞成。 此代码不提供任何安全保证。 我不会对使用此代码形成的损失负责。 使用它须要本身承担风险。
This code may be used for any non-commercial and commercial purposes in a compiled form. The code may be redistributed as long as it remains unmodified and providing that the author name and the disclaimer remain intact. The sources can be modified with the author consent only. This code is provided without any guarantees. I cannot be held responsible for the damage or the loss of time it causes. Use it at your own risks.
鉴于开发这个控件所付出的努力,下面的要求并不过度: 若是你在在商业应用程序中使用这个控件,那么请给我发邮件让我知道。app
控件的主要特色是:less
高速绘图(轴固定时),容许快速绘制数据 无限数量的数据序列(内存是限制) 每一个数据序列的数据量不受限制 支持线图,点图,平面图,柱状图,K线图 和甘特图系列 最多四个轴(左,下,右和上轴) 标准轴,对数轴或日期/时间轴 自动伸缩的坐标轴, 翻转的坐标轴(相互独立) 轴标签 点标签 平滑的曲线 网格 图例和标题 交互性(在控件中发生特定事件时的通知) 支持手动缩放和鼠标平移 支持鼠标指针 支持轴上的滚动条 高度可定制(颜色,标题,标签,边缘,字体等) 支持UNICODE 支持打印和保存到图像文件编辑器
本文经过一系列简短的教程来涵盖控件的大部分功能。 阅读本文后,您将可以快速地在本身的应用程序中使用本控件。ide
我决定从文章中删除全部的类和函数的文档,由于它不是很是友好而且我很难维护。 此外,随着代码的增加,要记录的类和函数的列表变得过于普遍以致于不能将全部内容放在文章中。 做为替代,我提供了一个doxygen文档,您能够从本文中(文章的开头)下载:只需下载“Doxygen文档”zip文件,解压全部文件,双击“Index.html”文件,进行查看。函数
此图表控件容许您在屏幕上绘制一系列数据。 此控件能够添加几个不一样类型数据序列而且最多可使用四个轴。 添加到图表的数据序列与一个水平轴(底部或顶部)和一个垂直轴(右侧或左侧)相关联。 这两个轴控制数据序列在图表上的显示方式。
为了可以在应用程序中使用次图表控件,您首先须要在本身的工程里添加源代码zip中包含的文件。性能
注意:控件在内部使用动态转型,所以必须启用RTTI(RunTime Type Information 运行时自动类型识别的机制),不然可能会发生崩溃。 默认状况下,VC6没有启用RTTI,所以要启用它打开项目设置 - >“C / C ++”选项卡 - >“C ++语言”类别,并确保“Enable Run-Time Type Information (RTTI) “选项已选中。
在应用程序中使用图表控件有两种方法:手动插入,或经过资源编辑器插入。
1.#include "ChartCtrl"
添加在对话框(Dialog)类的头文件中
2.在对话框类中添加变量CChartCtrl
:
//{{AFX_DATA(CChartDemoDlg) //}}AFX_DATA CChartCtrl m_ChartCtrl;
3.在对话框类的OnInitDialog
方法中添加这个控件的Create
方法。
1.向对话框资源添加自定义控件,打开控件的属性,并为Class属
性指定ChartCtrl
。 为了不滚动条上的闪烁,必须设置WS_CLIPCHILDREN
样式(0x02000000L)
,如图所示。
2.#include "ChartCtrl.h"
添加在对话框(Dialog)类的头文件中
3.在对话框类中添加变量CChartCtrl
:
//{{AFX_DATA(CChartDemoDlg) //}}AFX_DATA CChartCtrl m_ChartCtrl;
4.在DoDataExchange
函数中添加DDX_Control
(不要忘了更改ID号和控件名字):
Add a variable of type CChartCtrl in your dialog class: Hide Copy Code //{{AFX_DATA(CChartDemoDlg) //}}AFX_DATA
几种类型的数据序列能够添加到控制:点序列,线序列,曲面序列,柱状图序列,K线图序列或甘特图序列。 点的数据格式可能因序列而异(例如,K线图和甘特图系列使用不一样的点格式)。
Series type Description Create function Point type Point series Each data point is represented by a single point on the screen. The appearance of the point can be customized. CreatePointsSerie SChartXYPoint Line series The data points are connected through a line. The appearance of this line can be customized and it can also be smoothed. CreateLineSerie SChartXYPoint Surface series The data points are connected through a line and the area under this line is filled with a specific brush. The series can also be displayed vertically. CreateSurfaceSerie SChartXYPoint Bar series Each data point is plotted as a vertical bar of a certain width. Multiple bar series can be stacked next to each other without overlapping. The bars can also be plotted horizontally. CreateBarSerie SChartXYPoint Candlestick series Each data point is made of five attributes: the low value, the high value, the open value, the close value and the X value (time). Each point is drawn as a candlestick. This series is used for plotting financial data. CreateCandlestickSerie SChartCandlestickPoint Gantt series Each data point is made of three attributes: the start and end time and a Y value. Each point is drawn as a horizontal bar starting at the start time and finishing at the end time. The bar is positioned along the Y axis at its Y value. CreateGanttSerie SChartGanttPoint
一旦你选择了一种系列,你能够经过调用上表中列出的CChartCtrl
类的辅助函数之一将其添加到图表中。 这些函数接受两个可选参数:两个布尔值来肯定描述该系列是链接到副水平轴(顶轴)或者是链接大副垂直轴(右轴)。 若是未指定参数,则数据系列将附加到主水平轴(底部轴)和主垂直轴(左轴)。
警告: 在将任何系列添加到图表以前,您须要建立该系列所链接的两个轴。 若是不这样作,将致使控件失效(assert)。 有关详细信息,请参见“操纵轴”一节。
一旦将系列添加到图表后,咱们就可使用数据填充该图表。 有两种方法:将数据放到一个单元中一块儿添加,或者逐点添加。 后者用于有动态数据时:每次调用函数时都会更新图表。 虽然这个调用是快速的(在某些特定条件下),可是最好尽量地将数据放到一个单元中。 下面是一个简单代码示例,它在图表中建立两个系列,并用数据填充它们:一个系列在初始化时彻底填充,另外一个系列在调用OnDataReceived
函数(仅存在于此示例的目的)时填充。 m_pLineSeries
,m_pPointsSeries
和m_ChartCtrl
是CMyClass
类的成员变量。
void CMyClass::Init() { .... // SNIP: Creation of the axes in the chart. This MUST be done before. m_pLineSeries = m_ChartCtrl.CreateLineSerie(); m_pPointsSeries = m_ChartCtrl.CreatePointsSerie(); double YValues[10]; for (inti=0;i<10;i++) XValues[i] = YValues[i] = i; m_pLineSerie->SetPoints(XValues,YValues,10); } void CMyClass::OnDataReceived(double X, double Y) { m_pPointsSeries->AddPoint(X, Y); }
全部系列类继承自同一抽象基类:CChartSerie
。该类处理全部系列通用的功能,但对具体的数据点没有任何处理功能。点的概念在子类CChartSerieBase
中引入,它是一个模板类,模板参数是要操做为点的数据类型。这很重要,由于序列可能必须处理不一样的数据类型:例如点序列操做具备X和Y值的点,可是K线图系列操纵具备5个值(打开,关闭,高,低和时间值)的点。其余系列继承自CChartSerieBase
并提供他们操做的数据类型。 CChartSerieBase
类已经处理了大多数数据管理,并经过纯虚函数将渲染委托给子类。每一个系列在建立时也会分配一个Id。此标识可经过CChartSerie :: GetSerieId()
检索,并可用于从图表中删除该系列。
该系列的一个重要特征是控制点的顺序:该系列中的全部点将根据它们的值从新排序。 默认状况下,点是基于它们的X值排序的,但您能够经过对它们的Y值排序或不对它们进行排序来改变这种行为(在这种状况下,系列保持将点添加到系列中的顺序 )。 对点进行排序会对性能产生影响:若是点是有序的,则控件可以从完整系列中检索第一个和最后一个可见点,而且仅绘制两个点之间的点。 另外一方面,你将不能绘制像椭圆形的曲线。 您能够经过调用CChartSerieBase :: SetSeriesOrdering
来更改点的顺序。
控件中的不一样系列的功能一般是不言自明的。 然而,柱状图系列须要一些解释。
这个系列有点特别,若是其中几个在同一个控件上绘制在一块儿,他们将互相影响。 目的是可以绘制多个条形图系列,而不会重叠:它们是彼此相邻绘制的。 为此,您须要指定每一个所属的组(一个简单的整数标识符)。 同一组的系列彼此相邻地绘制(或者对于水平条在彼此的顶部):参见两个图形的示例。 设置组ID是经过SetGroupId
函数完成的。
Bar series with the same group Id Bar series with different group Id
您还能够经过调用SetInterSpace
静态函数来控制全部柱形图之间剩余的空间的宽度。 这将为全部系列设置以像素为单位的空间(所以,若是显示多于两个系列,则在任何位置使用相同的空间)。 注意,您能够经过调用SetBarWidth
单独设置柱状图系列的宽度。
一旦使用数据填充您的系列,您还能够在系列的特定点上添加标签:这个标签始终附加到特定点。 如今,只提供一种类型的标签,气泡标签:包含文本的圆角矩形并用线链接到特定点上。 固然,若是须要,您也能够提供本身的自定义标签(参见“扩展功能”一节)。
有两种方式建立文本标签:静态建立标签时,或动态注册一个对象,当标签请求时,它将提供文本。 第一种方法是最简单的,但也不太灵活。 下面是一个代码片断,显示如何作(假设m_pSeries
已经建立并填充足够的数据):
void CMyClass::Init() { // SNIP... m_pSeries->CreateBalloonLabel(5,_T("This is a simple label")); }
此调用将建立一个带有“This is a simple label”文本的标签,并将其附加到带索引为5的点。该函数返回一个指向新建立的标签的指针,以便您能够修改其某些属性或存储以供之后使用。
第二种方法有点复杂,但提供了更多的灵活性:例如,您能够以更方便的方式在标签中显示点属性(例如X值,Y值,…)。 为此,您必须建立一个继承自CChartLabelProvider <PointType>
的类,并在建立标签时提供此类的实例。 此类是模板类,模板参数是标签附加到的系列的点类型。 这个类是一个简单的接口,你必须覆盖TChartString GetText(CChartSerieBase <PointType> * pSerie,unsigned uPtIndex)
方法。 此函数应返回必须在标签中显示的文本。 它接收指向标签所附加的系列和点索引的指针。 这里有一个这样的标签提供程序类的例子:
class CCustomLabelProvider : public CChartLabelProvider<SChartXYPoint> { public: TChartString GetText(CChartSerieBase<SChartXYPoint>* pSeries, unsigned uPtIndex) { TChartStringStream ssText; SChartXYPoint Point = pSeries->GetPoint(uPtIndex); ssText << _T("X value=") << Point.X; return ssText.str(); } };
此代码段显示如何将其与标签一块儿使用。 注意m_pSeries应该是一个操做SChartXYPoint点(点,线,面或者柱系列)的系列。 若是不是这样,你的代码将给出一个编译错误。
void CMyClass::Init() { // SNIP... m_pLabelProvider = new CCustomLabelProvider(); m_pSeries->CreateBalloonLabel(5, m_pLabelProvider); }
控件不获取指针的全部权,所以,当你再也不须要时,你有责任删除它。 在上面的例子中,它一般会在CMyClass
析构函数中被删除。 在上面的示例中,您能够为全部要添加的标签地方重复使用相同的标签类, 这也带来另外一个优势:若是你想在运行时改变标签的格式,你只须要在CustomLabelProvider
中添加代码。 不须要遍历全部现有标签并更改其文本。 固然,在这种状况下,须要刷新控件,由于必须从新绘制标签。 还要注意TChartStringStream
类的用法,TChartStringStream
类是由控件提供的别名(相似于TChartString
)。 当UNICODE被定义时,它解析为std :: wstringstream
,当未定义UNICODE时,解析为std :: stringstream
。
轴是图表的一个重要特征,由于它们控制不一样系列在控制中的显示方式。 控件中最多可以使用四个轴:底部,顶部,左侧和右侧。 控件的每一个系列必须和一个水平轴和一个垂直轴相链接。 在图表中添加系列时指定这些轴。 底部和左侧轴是主轴,顶部和右侧轴是辅助轴(您将在控件的某些功能中遇到此问题)。 如今有三种类型的轴供选择:标准轴,对数轴和日期/时间轴。 您能够在不一样位置选用不一样类型的轴。
一旦您选择了在不一样位置使用哪些轴,您须要先建立它们,而后才能向控件添加任何数据。 为此,经过指定轴附加在哪一个位置,简单地调用CreateStandardAxis
,CreateLogarithmicAxis
或CreateDateTimeAxis
。 若是已经在该位置建立了轴,则控件将销毁它而且用新的轴替换它。 这里有一个简单的代码片断,显示如何在底部建立日期/时间,在左侧建立一个标准轴:
void CMyClass::Init() { CChartStandardAxis* pBottomAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::BottomAxis); CChartLogarithmicAxis* pLeftAxis = m_ChartCtrl.CreateLogarithmicAxis(CChartCtrl::LeftAxis); }
一旦建立了这些轴,就能够对它们设置一些属性。 大多数属性在全部轴类型之间共享(例如自动模式,最小值和最大值,轴标签,…)。 轴能够设置为三种“自动”模式:全自动,屏幕自动和手动模式。
全自动模式基于附加到该轴的全部系列计算轴最小值和最大值(全部系列的全部点的最小值用做轴的最小值,并使用全部系列的全部点的最大值 做为轴的最大值)。 屏幕自动模式基于与该轴相关的全部系列的全部可见点计算轴最小值和最大值。 例如,若是图表仅显示链接到手动底部轴和屏幕自动左侧轴的一个系列,则左侧轴将自适应于当前可见的点,而且不考虑这些点有可能超过底轴的范围(在全自动模式下,底轴外部的点将被考虑)。 警告:若是系列的两个轴都处于屏幕自动模式,则结果未定义。 在手动模式下,轴最小和最大值由用户设置,不禁控件计算。
在使用自动轴模式下,若是将数据动态添加到控件,若是新的数据点位于轴的范围以外,那么控件将自动刷新。 这里是一个代码片断(继续前一个代码段),显示一个全自动轴(底部轴)和一个手动轴(左轴,它是一个对数轴):
void CMyClass::Init() { // SNIP ... pBottomAxis->SetAutomaticMode(CChartAxis::FullAutomatic); // The call to SetAutomaticMode(CChartAxis::NotAutomatic) is not // really needed because this is the default. pLeftAxis->SetAutomaticMode(CChartAxis::NotAutomatic); pLeftAxis->SetMinMax(0.01,1000); }
轴有一个模式是离散模式(默认禁用)。此模式指定轴不显示连续值,而只显示离散值,这些值是轴上刻度指定的值,而轴将不显示其余的值。尝试绘制不一样于显示的节拍值的值是不可能的。让咱们举一个例子:假设你有一个底部标准轴,间隔为1.0(因此,显示的蜱是1,2,3等等)。尝试绘制X值为0.5的点将在相同位置显示该点,就好像它的值为1.0。事实上,你能够认为两个刻度之间的区域是一个常量值。这就是为何刻度标签显示在两个刻度的中间,而不是刻度自己。
这里有一个小代码片断,显示离散轴对系列显示方式的影响。代码片断下的两个图像显示启用离散模式(第一个图像)或禁用(第二个图像)的结果。
void CMyClass::Init() { CChartStandardAxis* pBottomAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::BottomAxis); pBottomAxis->SetMinMax(0, 10); CChartStandardAxis* pLeftAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::LeftAxis); pLeftAxis->SetMinMax(0, 10); pBottomAxis->SetTickIncrement(false, 1.0); pBottomAxis->SetDiscrete(true); CChartLineSerie* pSeries = m_ChartCtrl.CreateLineSerie(); double XVal[20]; double YVal[20]; for (int i=0; i<20; i++) { XVal[i] = YVal[i] = i/2.0; } pSeries->SetPoints(XVal,YVal,20); }
Discrete mode enabled Discrete mode disabled
使用日期/时间轴有点特别,下面是如何利用这个功能的解释。要了解日期/时间轴的重要一点是它们在COleDateTime
对象内部工做。缘由很简单:COleDateTime
中有DATE
类型的类,DATE
类型是一个双精度型。因为图表中的点表示为双精度值,所以它很是适合:使用标准点(非日期/时间)和日期/时间点之间没有差别,这使得后者的使用不太复杂。全部点仍然存储为双精度型,不管是不是日期/时间。
建立日期/时间轴后,能够在控件中填充数据。为此目的,没有改变:你必须从CChartSerie类调用void AddPoint(double X,double Y)或void SetPoints(double * X,double * Y,int Count)。 CChartCtrl类提供了两个静态函数,让你从COleDateTime转换为双精度,反之亦然:
double DateToValue(const COleDateTime& Date) COleDateTime ValueToDate(double Value)
若是您有另外一种格式的日期(例如time_t
或SYSTEMTIME
),这不是一个问题,由于COleDateTime
对象能够从不一样的时间格式构造(检查COleDateTime
类的MSDN文档,以了解从哪一种格式能够构造它)。
填充数据后,能够配置轴以显示所需的内容。 与日期/时间轴相关的几个功能可用:
void SetDateTimeIncrement(TimeInterval Interval, int Multiplier) void SetDateTimeFormat(bool bAutomatic, const TChartString& strFormat) void SetReferenceTick(COleDateTime referenceTick)
第一个容许您指定轴上显示的两个节拍之间的间隔。两个节拍之间的间隔将遵照正确的时间,这意味着若是指定1个月的节拍增量(Interval=CChartAxis::tiMonth and Multiplier=1)
,则两个节拍之间的间隔将是不规则的(28,30或31天)。第二个函数容许您指定刻度标签的格式。控件根据刻度间隔自动格式化刻度标签,但您能够经过调用此函数覆盖它。检查MSDN上的COleDateTime :: Format
函数的文档以获取更多信息。最后,SetReferenceTick(COleDateTime referenceTick)
函数容许您为轴指定一个参考标记。参考标记是用做绘制标记的参考的日期:在该日期老是存在标记。当您在SetDateTimeIncrement
函数中指定的multiplier
不是1时,这颇有用。例如,假设您指定了3个月的单位增量,而且您但愿在2月(所以,5月,8月,…)有一个单位,那么您能够调用此函数将2月1日设置为参考单位。默认设置为2000年1月1日。
下面是一个简单的代码片断,它建立一个日期/时间轴,并显示不一样函数的用法:
void CMyClass::Init() { // Sets the axis min value to January 1st 2006 and the axis // max value to December 31st 2007. COleDateTime minValue(2006,1,1,0,0,0); COleDateTime maxValue(2007,12,31,0,0,0); pBottomAxis->SetMinMax(CChartCtrl::DateToValue(minValue), CChartCtrl::DateToValue(maxValue)); // Sets the tick increment to 4 months (disable automatic tick increment) pBottomAxis->SetTickIncrement(false, CChartDateTimeAxis::tiMonth, 4); // Sets the tick label format for instance "Jan 2006" pBottomAxis->SetTickLabelFormat(false, _T("%b %Y")); }
控件的外观方面能够根据不一样的应用场景作出更改,好比控件的不一样部分(图例,标题,背景,…)均可以修改。 全部与这些对象的交互是经过CChartCtrl
类来实现:一些将根据须要建立(例如axes或series),一些在建立控件时建立(legend,titles,…)。 通常来讲,你永远不会本身建立这些对象,而是将该任务委派给CChartCtrl类。 惟一的例外是当您要使用自定义轴或自定义系列(请参阅“扩展功能”部分)。 例如,下面是一个代码段,设置渐变背景,并将图例放在控件的底部:
void CMyClass::Init() { // SNIP // Disable the refresh of the control m_ChartCtrl.EnableRefresh(false); // Set the gradient for the background m_ChartCtrl.SetBackGradient(RGB(255,255,255),RGB(125,125,255),gtVertical); // Dock the legend at the bottom m_ChartCtrl.GetLegend()->DockLegend(CChartLegend::dsDockBottom); // Specifies that the legend entries are horizontally stacked m_ChartCtrl.GetLegend()->SetHorizontalMode(true); // Re-enable the refresh of the control m_ChartCtrl.EnableRefresh(true); }
重要:从版本1.4的控件,每次调用控件上的一个属性将致使控件的彻底刷新(即便像改变一些文本的字体或对象的颜色)。 为了不在没有必要时刷新控件(例如,当您同时更改多个属性时),应首先禁用刷新,更改属性,而后从新启用刷新,如上面的代码段所示 。
自从1.5版的控件开始支持UNICODE。 全部出现的std :: string
对象已被TChartString
对象替换,这只是一个typedef
,若是未启用UNICODE,则解析为std :: string
,并在启用UNICODE时解析为std :: wstring
。
有时,应用程序须要响应用户鼠标操做。 例如,若是用户点击点,则程序能够显示关于被点击的点的信息,这一节将解释如何作到。
虽然原理是有点不一样,可是不管你想听在图表上的通常鼠标事件自己(点击轴,图例,…)或你是否对特定系列的鼠标事件感兴趣。 这两种状况都很容易实现。
你必须实现CChartMouseListener
接口,覆盖你感兴趣的方法,并经过调用CChartCtrl :: RegisterMouseListener(CChartMouseListener * pMouseListener)
将该类的实例注册到图表控件。 根据鼠标事件发生在控件的哪一个部分:标题,图例,轴或绘图区,调用该接口上的不一样函数。 对于全部这些函数,老是传递两个参数:MouseEvent
,它是列出鼠标事件类型(鼠标移动,左键单击,…)的枚举,以及一个CPoint
对象,它包含的发生事件的点的屏幕坐标。 对于某些函数,须要时传递一些其余参数。 例如,当单击一个轴时,指向该轴的指针被传递给该函数。
下面是CChartMouseListener
的实现,它对轴的点击做出反应,并显示一个消息框:
class CCustomMouseListener : public CChartMouseListener { public: void OnMouseEventAxis(MouseEvent mouseEvent, CPoint point, CChartAxis* pAxisClicked) { if (mouseEvent == CChartMouseListener::LButtonDoubleClick) { MessageBox(_T("Axis clicked"), _T("Info"), MB_OK); } } };
而后你必须建立一个这个类的实例并注册它:
m_pMouseListener = new CCustomMouseListener(); m_ChartCtrl.RegisterMouseListener(m_pMouseListener);
这里也须要本身删除指针。
响应系列上的事件与响应通常事件很是类似,只是监听器是CChartSeriesMouseListener
的一个实例,它是一个模板类,模板参数是系列的点类型。 这是须要的,以免当您要检索点的特定值时没必要要的转型。 另外一个区别是,您必须在系列自己上注册监听器,而不是在图表控件上注册。
下面是CChartSeriesMouseListener
的实现,它对系列的点击作出反应,若是点击发生在点上,它将显示一个带有点的Y值的消息框:
class CCustomMouseListener : public CChartSeriesMouseListener<SChartXYPoint> { public: void OnMouseEventSeries(MouseEvent mouseEvent, CPoint point, CChartSerieBase<SChartXYPoint>* pSerie, unsigned uPointIndex) { if (mouseEvent == CChartMouseListener::LButtonDoubleClick && uPointIndex != INVALID_POINT) { TChartStringStream ssText; SChartXYPoint Point = pSeries->GetPoint(uPointIndex); ssText << _T("Y value=") << Point.Y; TChartString strText = ssText.str(); MessageBox(NULL,strText.c_str(), _T("Info"), MB_OK); } } };
注意:当用户不点击一个点时,OnMouseEventSeries
函数也能够被调用。 例如当用户在两个点之间但仍然在该系列上点击时状况。 在这种状况下,为uPointIndex
参数传递INVALID_POINT
。
而后,您必须建立此类的实例并将其注册到系列中:
m_pMouseListener = new CCustomMouseListener(); m_pSeries.RegisterMouseListener(m_pMouseListener);
注意:只有当系列操做SChartXYPoint
类型的点(点,线,面或者柱状图系列)时,这才会起做用。 若是不是这样,您的代码将生成编译错误。
出于性能缘由,禁止检测系列上的鼠标移动事件。 要启用它,请参阅doxygen文档中的CChartSerie :: EnableMouseNotifications函数。
您还能够向控件添加光标。 支持两种类型的光标:“十字线”光标和“拉线”光标。 第一个是在鼠标移动的绘图区域上显示的简单十字,第二个是与特定轴关联的水平或垂直线,您能够经过单击它并使用鼠标移动来拖动。 对于每一个光标,您能够注册一个侦听器,以便在移动光标时通知它。 这里是一段代码,用于建立与底部和左侧轴相关联的“十字准线”光标以及与底部轴相关联的“拉线”光标:
// Creates a cross-hair cursor associated with the two primary axes. CChartCrossHairCursor* pCrossHair = m_ChartCtrl.CreateCrossHairCursor(); // Creates a dragline cursor associated with the bottom axis. CChartDragLineCursor* pDragLine = m_ChartCtrl.CreateDragLineCursor(CChartCtrl::BottomAxis); // Hides the mouse when it is over the plotting area. m_ChartCtrl.ShowMouseCursor(false);
注意到对CChartCtrl :: ShowMouseCursor
的调用结束。 默认状况下,鼠标老是可见的,可是当您使用十字光标时,当它在绘图区域时隐藏有时是须要的。
若是但愿在光标位置更改时收到通知,则必须实现CChartCursorListener
接口,建立其实例并使用光标注册它:
class CCustomCursorListener : public CChartCursorListener { public: void OnCursorMoved(CChartCursor *pCursor, double xValue, double yValue) { TChartStringStream ssText; ssText << _T("Cursor moved: xPos=") << xValue << _T(", yPos=") << yValue; // Do something with the string... } };
CCustomCursorListener* pCursorListener = new CCustomCursorListener; pDragLine->RegisterListener(pCursorListener);
OnCursorMoved
函数接收一个X和Y值,但对于拖动光标,只使用这些值中的一个:若是光标与水平轴相关联,则使用X值,不然使用Y值。
在版本1.1的控件中,缩放和平移功能已被添加到控件。 使用鼠标左键控制缩放,用鼠标右键控制平移。 要缩放图表的特定部分,只需左键单击图表(这将是缩放矩形的左上角),而后拖动到右下角。 将出现一个矩形。 一旦松开鼠标按钮,四个轴将自动调整到您选择的区域。 默认状况下启用缩放,但您能够经过调用CChartCtrl :: SetZoomEnabled(bool bEnabled)
来禁用缩放。 您还能够经过调用CChartAxis :: SetZoomLimit(double dLimit)
为每一个轴指定缩放限制。 它指定缩放时轴的最小范围。 默认值为0.001。
要平移控件,右键单击控件上的某处并移动鼠标。 鼠标下的点将“跟随”鼠标的移动(实际上,轴的最小和最大值将改变)。 默认状况下启用平移,但您能够经过调用CChartCtrl :: SetPanEnabled(bool bEnabled)
来禁用它。
若是您左键单击图表(例如开始缩放),但若是您移动到左上角,全部使用缩放和平移功能所作的修改将被取消(控制将处于它的状态 在使用平移和缩放操做以前)。 最后,还有一种方法经过调用CChartAxis :: SetPanZoomEnabled(bool bEnabled)
禁用特定轴的平移和缩放功能。
线和点系列容许以高速率绘制数据。 这一般在要绘制来自外部设备(例如,传感器)的数据时完成。 这是可能的,由于当您向此类系列添加点时,控件不会彻底刷新,只会绘制最后一个点(或最后一个线段),这是很是有效的。 可是,若是但愿控件可以足够快地绘制数据,则必须考虑几点。
一个重要的事情是,使用自动轴可能会下降不少性能。这是由于若是一个点绘制在轴范围以外,则轴范围将被自动调整,这意味着控制将被彻底刷新。所以,若是您使用自动底部轴线并具备“滚动”轨迹,则每一个新点都将位于轴的当前范围以外,而且将对每一个点执行控制刷新。处理的更好的方法是使用固定轴而且每秒手动地增长轴的范围(或以合理的速率)。
另外一个重要的点是,你不该该在向一个系列添加一个新点以后调用RefreshCtrl
。这固然会彻底地刷新控件,可是应该避免这样作。最后,若是您须要同时应用几个修改或添加几个点到控件,您应该在EnableRefresh(false)
和EnableRefresh(true)
之间封装这些调用(请参阅“自定义外观”部分)。
在某些特定状况下,您须要使用新功能扩展控件,例如新的系列类型。目前,您能够自定义四个组件:序列,轴,点标签和光标。
要提供新轴,新标签或新光标,您只需继承基类(CChartAxis
,CChartLabel
或CChartCursor
)并实现所需的虚拟函数。一旦完成,您能够经过调用不一样函数的自定义版本(CChartCtrl :: AttachCustomAxis
,CChartCtrl :: AttachCustomLabel
或CChartCtrl :: AttachCustomCursor
)附加您的新对象。 CChartLabel
类是一个模板类。这个主题有点普遍,进入了不少细节,但最简单的方法是看看不一样的现有类。
若是你想提供新的系列,这有点不一样:你首先要考虑你想要在你的系列中操纵的点的类型。若是你只须要使用X和Y值来操做点,那么你能够继承CChartXYSerie
,它提供了不少功能来操做这些点。而后,您必须实现所需的虚拟函数。看看下面的系列:CChartLineSerie
,CChartPointSerie
,CChartSurfaceSerie
和CChartBarSerie
具体示例。
若是你的系列操纵其余类型的点,那么你首先必须为点包含如下方法建立一个结构:double GetX()
,double GetXMin()
,double GetXMax()
,double GetY()
,double GetYMin()
和double GetYMax()
。一旦完成,您必须继承CChartSerieBase
并将此点做为模板参数。而后,您必须提供所需的虚拟功能。看看下面的系列具体例子:CChartCandlestickSerie
和CChartGanttSerie
。
在版本2.0中,对控件进行重构,致使API的更改。 主要的可见变化是每一个轴类型如今有其单独的类(CChartStandardAxis,CChartDateTimeAxis和CChartLogarithmicAxis)。 这也意味着默认状况下没有建立轴,而且您必须在向图表添加系列以前本身建立轴(不然代码将断言)。 这包括在“操纵轴”部分。
另外一个变化是添加系列到图表的方式:AddSerie已经在CChartCtrl类中删除,并已被帮助函数替代,以建立特定的系列类型(CreateLineSerie,CreatePointsSerie,…)。 这些函数返回确切的系列类型,所以再也不须要铸造。 这在“操纵系列”一节中有详细描述。
版本3.0.0的主要变化是,系列基类如今已经做为模板类,模板参数是系列操做的点类型。若是您没有经过提供新的系列类型扩展控件,这将不会在您的代码中有所不一样。若是你提供了一个新的系列类型,你的类必须继承CCharSerieBase并提供它操做的点的类型。若是你的系列使用只有X和Y值的点,你能够简单地继承CChartXYSerie。看看现有的系列更多的例子。
另外一个小的修改是标签提供程序如今也是模板类(出于一样的缘由)。而且监听系列中的鼠标事件如今从图表上的鼠标事件中分离出来。这两点在“在点上添加标签”部分和“鼠标事件通知”部分中有很好的解释。
最后,CChartAxis :: SetAutomatic方法已被标记为已弃用,您应该使用CChartAxis :: SetAutomaticMode(已经引入了一个额外的自动模式)。
本节只是两个代码片断,显示了如何使用控件。 第一个片断再现了示波器示例的图像(参见本文顶部),第二个示例再现了“2008年收入”图像。 代码是文档化的,因此它不该该太难理解。
Oscilloscope example:
// Disable the refresh of the control (avoid multiple refresh). m_ChartCtrl.EnableRefresh(false); // Create a bottom and left axes CChartStandardAxis* pBottomAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::BottomAxis); CChartStandardAxis* pLeftAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::LeftAxis); // Sets the min and max values of the bottom and left axis to -15 -> 15 pBottomAxis->SetMinMax(-15,15); pLeftAxis->SetMinMax(-15,15); // Add a new series of type line to the control and add data to it CChartLineSerie* pLineSeries = m_ChartCtrl.CreateLineSerie(); // Specifies that the points in the series are not ordered (needed to be able // to draw an ellipse). pLineSeries->SetSeriesOrdering(poNoOrdering); for (int i=0;i<361;i++) { double X = 10 * sin(i/360.0 * 2 * 3.141592); double Y = 10 * cos( (i-60)/360.0 * 2 * 3.141592); pLineSeries->AddPoint(X,Y); } // Defines the different colors (back color, axes color, ...) COLORREF BackColor = RGB(0,50,0); COLORREF GridColor = RGB(0,180,0); COLORREF TextColor = RGB(0,180,0); COLORREF SerieColor = RGB(0,255,0); // Specifies a sunken border for the control m_ChartCtrl.SetEdgeType(EDGE_SUNKEN); // Sets the color of the border and the back color m_ChartCtrl.SetBorderColor(TextColor); m_ChartCtrl.SetBackColor(BackColor); //Sets the color of the different elements of the bottom axis m_ChartCtrl.GetBottomAxis()->SetAxisColor(TextColor); m_ChartCtrl.GetBottomAxis()->SetTextColor(TextColor); m_ChartCtrl.GetBottomAxis()->GetGrid()->SetColor(GridColor); // Sets the color of the different elements of the left axis m_ChartCtrl.GetLeftAxis()->SetAxisColor(TextColor); m_ChartCtrl.GetLeftAxis()->SetTextColor(TextColor); m_ChartCtrl.GetLeftAxis()->GetGrid()->SetColor(GridColor); // Sets the color of the title, change the font to Times New Roman // and add a string m_ChartCtrl.GetTitle()->SetColor(TextColor); m_ChartCtrl.GetTitle()->SetFont(140,_T("Times New Roman")); m_ChartCtrl.GetTitle()->AddString(_T("An example of oscilloscope")); // Change the color of the line series pLineSeries->SetColor(SerieColor); // Finally re-enable the refresh of the control. This will refresh the // control if any refresh was still 'pending'. m_ChartCtrl.EnableRefresh(true);
“Income over 2008” example:
srand((unsigned int)time(NULL)); // Disable the refresh m_ChartCtrl.EnableRefresh(false); COleDateTime Min(2008,1,1,0,0,0); COleDateTime Max(2008,10,1,0,0,0); // Create the bottom axis and configure it properly CChartDateTimeAxis* pBottomAxis = m_ChartCtrl.CreateDateTimeAxis(CChartCtrl::BottomAxis); pBottomAxis->SetMinMax(Min,Max); pBottomAxis->SetDiscrete(true); pBottomAxis->SetTickIncrement(false,CChartDateTimeAxis::tiMonth,1); pBottomAxis->SetTickLabelFormat(false,_T("%b")); // Create the left axis and configure it properly CChartStandardAxis* pLeftAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::LeftAxis); pLeftAxis->SetMinMax(0,100); pLeftAxis->GetLabel()->SetText(_T("Units sold")); // Create the right axis and configure it properly CChartStandardAxis* pRightAxis = m_ChartCtrl.CreateStandardAxis(CChartCtrl::RightAxis); pRightAxis->SetVisible(true); pRightAxis->GetLabel()->SetText(_T("Income (kEuros)")); pRightAxis->SetMinMax(0,200); // Configure the legend m_ChartCtrl.GetLegend()->SetVisible(true); m_ChartCtrl.GetLegend()->SetHorizontalMode(true); m_ChartCtrl.GetLegend()->UndockLegend(80,50); // Add text to the title and set the font & color m_ChartCtrl.GetTitle()->AddString(_T("Income over 2008")); CChartFont titleFont; titleFont.SetFont(_T("Arial Black"),120,true,false,true); m_ChartCtrl.GetTitle()->SetFont(titleFont); m_ChartCtrl.GetTitle()->SetColor(RGB(0,0,128)); // Sets a gradient background m_ChartCtrl.SetBackGradient(RGB(255,255,255),RGB(150,150,255),gtVertical); // Create two bar series and a line series and populate them with data CChartBarSerie* pBarSeries1 = m_ChartCtrl.CreateBarSerie(); CChartBarSerie* pBarSeries2 = m_ChartCtrl.CreateBarSerie(); CChartLineSerie* pLineSeries = m_ChartCtrl.CreateLineSerie(false,true); int lowIndex = -1; int lowVal = 999; for (int i=0;i<9;i++) { COleDateTime TimeVal(2008,i+1,1,0,0,0); int DesktopVal = 20 + rand()%(100-30); pBarSeries1->AddPoint(TimeVal,DesktopVal); int LaptopVal = 10 + rand()%(80-20); pBarSeries2->AddPoint(TimeVal,LaptopVal); int Income = DesktopVal + LaptopVal*1.5; if (Income < lowVal) { lowVal = Income; lowIndex = i; } pLineSeries->AddPoint(TimeVal,Income); } // Configure the series properly pBarSeries1->SetColor(RGB(255,0,0)); pBarSeries1->SetName(_T("Desktops")); pBarSeries2->SetColor(RGB(68,68,255)); pBarSeries2->SetGradient(RGB(200,200,255),gtVerticalDouble); pBarSeries2->SetName(_T("Laptops")); pBarSeries2->SetBorderColor(RGB(0,0,255)); pBarSeries2->SetBorderWidth(3); pLineSeries->SetColor(RGB(0,180,0)); pLineSeries->SetName(_T("Total income")); pLineSeries->SetWidth(2); pLineSeries->EnableShadow(true); // Add a label on the line series. TChartStringStream labelStream; labelStream << _T("Min income: ") << lowVal; CChartBalloonLabel<SChartXYPoint>* pLabel = pLineSeries->CreateBalloonLabel(lowIndex, labelStream.str() + _T(" kEuros")); CChartFont labelFont; labelFont.SetFont(_T("Microsoft Sans Serif"),100,false,true,false); pLabel->SetFont(labelFont); // Re enable the refresh m_ChartCtrl.EnableRefresh(true);
Quite a lot of work is involved in the development of this control and, as any other software project, it might still contain bugs or errors in the documentation. If you encounter such a problem, please let me know (even if you fixed it yourself) so that I can fix the issue as soon as possible. Other users of the control will thank you for that. The same if you encounter errors in the documentation or typos in the article.
I’m also more or less constantly working on this control to add new features. If you have some requirement for a nice feature that could be useful for others, please let me know and I’ll add it to my wishlist. However, as I’m working on this control in my spare time, my time is rather limited.
Finally, if you liked this control, do not hesitate to drop me a word in the discussion forum or to rate the article, this is much appreciated. Thank you.
08/05/2006: Release of version 1.0 19/08/2006: Release of version 1.1
Bug fix in ScreenToValue function (CChartAxis) Bug fix in RemoveAllSeries function (CChartCtrl) Added support for manual zoom Added support for mouse panning Ability to specify a tick increment on the axis Added support for resizing the control 09/04/2007: Release of version 1.2
GDI leak corrected Invisible series are not taken in account for auto axis and legend (thanks to jerminator-jp) Ability to change the text color of the axis Ability to change the color of the border of the drawing area Surface series added 16/02/2008: Release of version 1.3
Added date/time axis Bug fix in how the logarithmic labels are displayed (trailing 0) Ability to change the color of the zoom rectangle Removed compiler warnings for VC2005 Bug fix in the zoom 14/04/2008: Release of version 1.4
Added support for scrollbars Bar series added Legend can be docked on any side or floating Support for legend in horizontal mode Support for transparent background on the legend Support for shadow for several objects RemovePointsFromBegin, RemovePointsFromEnd and AddPoints in the CChartSeries class Support for gradient background EnableRefresh and UndoPanZoom functions added in CChartCtrl Possibility to enable/disable the zoom for a specific axis and to set its limit Speed improvement on the series (min and max cached, ordering of the series) Series can be removed using their pointers Bug fix for invisible series in the legend Bug fix for logarithmic axis (1 digit was not displayed) Bug fix when removing series from the control Bug fix if the pen width is bigger than 1 for line series Bug fix for automatic axis 20/08/2008: Release of version 1.5 Added support for UNICODE Added support for printing Auto-hide scrollbars Baseline selection for bar series Performance patch Scrollbar flickering removed (see here) Bug fix: scrollbar is now updated when axis is panned Bug fix: calling AddPoint was not drawing the new point Bug fix: tick labels for log axis were not always correct (rounding error) Bug fix: last point of ChartPointSerie was not displayed Bug fix: moving the mouse outside the control doesn’t stop the zoom or pan operation (the button can be released outside the control) 13/04/2009: Release of version 2.0
The different axis types are now separated into different classes Modified the way to add series to the control for improved flexibility Added cursors Ability to display discrete axes Ability to be notified about mouse events occurring on the control Added labels on points Ability to display a smooth curve Added ChartFont: allows for italic, bold or underlined fonts Added the SetReferenceTick function for date/time axis Ability to store user data for each point Series now have an Id Removed the CChartObject class Points are now stored in a standard array instead of a std::vector for efficiency Binary search implemented for finding the first and last visible points (for efficiency) The line series now uses PolyLine instead of MoveTo/LineTo (efficiency) Bug fix when using date/time axis with a tick interval in years Bug fix: bar series were drawn from the wrong axis 11/06/2009: Release of version 2.0.1
Optimization: the pan feature has been smoothed Optimization: points with the same X and Y values are not plotted anymore for the line series. Bug fix: in some situations, the code was crashing when accessing points outside the valid range Bug fix: when series were removed, the legend was accessing removed series (which crashed) Bug fix: when a series was cleared, new points were not drawn properly Bug fix: inserting a point for which the X value already existed in the series did not add the point properly Bug fix with the CChartFont class 07/08/2009: Release of version 2.0.2
Bug fix: the control was crashing when a series with no points and no ordering was added Bug fix: the shadow of the line was not drawn correctly Bug fix: when an automatic date/time axis was used without any data, the code crashed 28/12/2009: Release of version 3.0.0
Series are now template classes with the template parameter being the point type. This allows the control to manipulate any type of points Added candlestick and Gantt series Added support to save the chart to an image file Bar series can be stacked Added a new automatic mode for axes: the screen automatic mode Listening for mouse events on a series has been moved to a CChartSeriesMouseListener class Bug fix: when a point X or Y value is modified, the series is reordered Bug fix: setting a tick increment on a standard axis did not show the digits properly 17/01/2010: Release of version 3.0.1
Bug fix: when using labels with the points series, the border of the points was changing color. Fixed by providing a way to specify the border color. Bug fix: the code was crashing when clicking on a series without having registered a mouse listener on the series. Bug fix: detection of mouse events on certain series type was crashing Bug fix: CChartTitle::SetVisible was not implemented 13/07/2010: Release of version 3.0.2
Bug fix: the high-speed functionality has been removed by mistake Bug fix: the draw function of the line series was not drawing points Bug fix: replaced Clear() by clear() in the ClearSerie function. Bug fix: Added implementation of ctor/dtor for the CChartCursorListener class Bug fix: memory leak when the series was cleared (labels were not deleted)
I would like to thank all the people from this community, they were a great help when I started programming. Thanks also to all the people who contributed to this control with their various help or feedback: toxcct, Chris Maunder, Kevin Hoffman, jerminator-jp, Laurie Gellatly, Eugene Pustovoyt, Andrej Ritter, Nick Holgate, Nick Schultz, Johann Obermayr, Pierre Schramm and Kevin Winter. A special thanks to Bruno Lavier for the time spent working on the control. I hope I didn’t forget anybody.
Chris Maunder’s Colour Picker Control (used in the demo application) Drawing smooth curves with Bezier Primitives
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
$(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('').text(i)); }; $numbering.fadeIn(1700); }); });