Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

High-speed Charting Control

, 13 Jul 2010 CPOL
A flexible charting control to display 2D data
chartcrtl_src.zip
ChartCtrl_demo.zip
ChartDemo
ChartCtrl
.svn
all-wcprops
entries
format
prop-base
props
text-base
ChartAxis.cpp.svn-base
ChartAxis.h.svn-base
ChartAxisLabel.cpp.svn-base
ChartAxisLabel.h.svn-base
ChartBalloonLabel.h.svn-base
ChartBalloonLabel.inl.svn-base
ChartBarSerie.cpp.svn-base
ChartBarSerie.h.svn-base
ChartCandlestickSerie.cpp.svn-base
ChartCandlestickSerie.h.svn-base
ChartCrossHairCursor.cpp.svn-base
ChartCrossHairCursor.h.svn-base
ChartCtrl.cpp.svn-base
ChartCtrl.h.svn-base
ChartCursor.cpp.svn-base
ChartCursor.h.svn-base
ChartDateTimeAxis.cpp.svn-base
ChartDateTimeAxis.h.svn-base
ChartDragLineCursor.cpp.svn-base
ChartDragLineCursor.h.svn-base
ChartFont.cpp.svn-base
ChartFont.h.svn-base
ChartGanttSerie.cpp.svn-base
ChartGanttSerie.h.svn-base
ChartGradient.cpp.svn-base
ChartGradient.h.svn-base
ChartGrid.cpp.svn-base
ChartGrid.h.svn-base
ChartLabel.h.svn-base
ChartLabel.inl.svn-base
ChartLegend.cpp.svn-base
ChartLegend.h.svn-base
ChartLineSerie.cpp.svn-base
ChartLineSerie.h.svn-base
ChartLogarithmicAxis.cpp.svn-base
ChartLogarithmicAxis.h.svn-base
ChartMouseListener.h.svn-base
ChartPointsArray.h.svn-base
ChartPointsArray.inl.svn-base
ChartPointsSerie.cpp.svn-base
ChartPointsSerie.h.svn-base
ChartScrollBar.cpp.svn-base
ChartScrollBar.h.svn-base
ChartSerie.cpp.svn-base
ChartSerie.h.svn-base
ChartSerieBase.h.svn-base
ChartSerieBase.inl.svn-base
ChartSeriesMouseListener.h.svn-base
ChartStandardAxis.cpp.svn-base
ChartStandardAxis.h.svn-base
ChartString.h.svn-base
ChartSurfaceSerie.cpp.svn-base
ChartSurfaceSerie.h.svn-base
ChartTitle.cpp.svn-base
ChartTitle.h.svn-base
ChartXYSerie.cpp.svn-base
ChartXYSerie.h.svn-base
PointsOrdering.h.svn-base
tmp
prop-base
props
text-base
ChartBalloonLabel.inl
ChartCtrl_source.zip
ChartLabel.inl
ChartPointsArray.inl
ChartSerieBase.inl
ChartDemo.dsp
ChartDemo.dsw
ColourPicker
.svn
entries
format
prop-base
props
text-base
ColourPicker.cpp.svn-base
ColourPicker.h.svn-base
ColourPopup.cpp.svn-base
ColourPopup.h.svn-base
tmp
prop-base
props
text-base
Doc
html
class_c_chart_axis.png
class_c_chart_balloon_label.png
class_c_chart_bar_serie.png
class_c_chart_candlestick_serie.png
class_c_chart_cross_hair_cursor.png
class_c_chart_cursor.png
class_c_chart_date_time_axis.png
class_c_chart_drag_line_cursor.png
class_c_chart_gantt_serie.png
class_c_chart_label.png
class_c_chart_line_serie.png
class_c_chart_logarithmic_axis.png
class_c_chart_points_serie.png
class_c_chart_serie.png
class_c_chart_serie_base.png
class_c_chart_standard_axis.png
class_c_chart_surface_serie.png
class_c_chart_x_y_serie.png
doxygen.png
tab_b.gif
tab_l.gif
tab_r.gif
res
ChartDemo.ico
ChartCtrl_doxygen.zip
html
class_c_chart_axis.png
class_c_chart_balloon_label.png
class_c_chart_bar_serie.png
class_c_chart_candlestick_serie.png
class_c_chart_cross_hair_cursor.png
class_c_chart_cursor.png
class_c_chart_date_time_axis.png
class_c_chart_drag_line_cursor.png
class_c_chart_gantt_serie.png
class_c_chart_label.png
class_c_chart_line_serie.png
class_c_chart_logarithmic_axis.png
class_c_chart_points_serie.png
class_c_chart_serie.png
class_c_chart_serie_base.png
class_c_chart_standard_axis.png
class_c_chart_surface_serie.png
class_c_chart_x_y_serie.png
doxygen.png
tab_b.gif
tab_l.gif
tab_r.gif
ChartCtrl_source.zip
ChartLabel.inl
ChartPointsArray.inl
ChartSerieBase.inl
ChartBalloonLabel.inl
chartctrl_src.zip
chartctr_demo.zip
ChartDemo.clw
ChartDemo.dsp
ChartDemo.dsw
res
ChartDemo.ico
/*
 *
 *	ChartSerieBase.h
 *
 *	Written by C�dric Moonen (cedric_moonen@hotmail.com)
 *
 *
 *
 *	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 this disclaimer remain intact. The sources can be modified WITH the author 
 *	consent only.
 *	
 *	This code is provided without any garanties. I cannot be held responsible for the damage or
 *	the loss of time it causes. Use it at your own risks
 *
 *	An e-mail to notify me that you are using this code is appreciated also.
 *
 *
 */

#pragma once

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "ChartSerie.h"
#include "ChartAxis.h"
#include "ChartPointsArray.h"
#include "ChartLabel.h"
#include "ChartBalloonLabel.h"
#include "ChartSeriesMouseListener.h"

#include <map>
#include "ChartString.h"
#include "PointsOrdering.h"

class CChartLegend;


//! Base class for all series of the control
/**
	This class inherits from CChartSeries and introduces the concept of 
	points through the template parameter. 

	This class is much more than a simple base class. It already store
	all the data points and provide utility functions to manipulate them.
**/
template <class T>
class CChartSerieBase : public CChartSerie
{
	friend CChartCtrl;
	friend CChartLegend;

public:
	//! Adds a single data point to the series.
	void AddPoint(const T& newPoint);
	//! Adds an array of points to the series.
	/**
		Points which were already present in the series are kept.
		@param pPoints
			Array of the new points
		@param Count
			Size of the array
	**/
	void AddPoints(T* pPoints, unsigned Count);
	//! Retrieves the point at the specified index.
	/**
		@param index
			The index of the point to retrieve
	**/
	const T& GetPoint(unsigned index) const;
	//! Sets an array of points to the series.
	/**
		Points which were already present in the series are discarded.
		@param pPoints
			Array of the new points
		@param Count
			Size of the array
	**/
	void SetPoints(T* pPoints, unsigned Count);
	//! Removes a certain amount of points from the begining of the series.
	void RemovePointsFromBegin(unsigned Count);
	//! Removes a certain amount of points from the end of the series.
	void RemovePointsFromEnd(unsigned Count);
	//! Removes all points from the series.
	void ClearSerie();

	//! Returns the number of points in the series.
	unsigned GetPointsCount() const  { return m_vPoints.GetPointsCount(); }

	//! Retrieves the minimum and maxium Y values of the series.
	/**
		@param Min
			Minimum value will be stored in this parameter
		@param Max
			Maximum value will be stored in this parameter
		@return
			false if the series doesn't contain data or is invisible
	**/
	bool GetSerieYMinMax(double& Min, double& Max)  const;
	//! Retrieves the minimum and maxium X values of the series.
	/**
		@param Min
			Minimum value will be stored in this parameter
		@param Max
			Maximum value will be stored in this parameter
		@return
			false if the series doesn't contain data or is invisible
	**/
	bool GetSerieXMinMax(double& Min, double& Max)  const;
	//! Retrieves the minimum and maxium screen X values of the series.
	/**
		@param Min
			Minimum value will be stored in this parameter
		@param Max
			Maximum value will be stored in this parameter
		@return
			false if the series doesn't contain data or is invisible
	**/
	bool GetSerieXScreenMinMax(double& Min, double& Max)  const;
	//! Retrieves the minimum and maxium screen Y values of the series.
	/**
		@param Min
			Minimum value will be stored in this parameter
		@param Max
			Maximum value will be stored in this parameter
		@return
			false if the series doesn't contain data or is invisible
	**/
	bool GetSerieYScreenMinMax(double& Min, double& Max)  const;

	//! Creates and attaches a balloon label on a point of the series.
	/**
		This functions specifies a static text to display in the label.
		@param uPointIndex
			The index of the point on which the label should be attached
		@param strLabelText
			The text of the label
		@return the created CChartBalloonLabel
	**/
	CChartBalloonLabel<T>* CreateBalloonLabel(unsigned uPointIndex, const TChartString& strLabelText);
	//! Creates and attaches a balloon label on a point of the series.
	/**
		This functions specifies a CChartLabelProvider object which is used to
		supply the text of the label. This is useful if you want more flexibility
		for the text of the label (display information about the point value for
		instance).
		@param uPointIndex
			The index of the point on which the label should be attached
		@param pLabelProvider
			Object providing the text displayed on the label
		@return the created CChartBalloonLabel
	**/
	CChartBalloonLabel<T>* CreateBalloonLabel(unsigned uPointIndex, CChartLabelProvider<T>* pLabelProvider);
	//! Attaches a custom label on a point of the series.
	/**
		@param uPointIndex
			The index of the point on which the label should be attached
		@param pLabel
			The label to attach to the point
	**/
	void AttachCustomLabel(unsigned uPointIndex, CChartLabel<T>* pLabel);

	//! Constructor
	CChartSerieBase(CChartCtrl* pParent);
	//! Destructor
	virtual ~CChartSerieBase();

	//! Specifies how the points should be ordered in the series.
	/**
		This specifies if the points should be ordered on their X values,
		on their Y values or not ordered (kept in order they are added to 
		the control). Ordering can improve performances a lot but makes it
		impossible to draw some specific curves (for instance, drawing an 
		ellipse is only possible if no ordering is set).
	**/
	virtual void SetSeriesOrdering(PointsOrdering newOrdering);

	//! Retrieves the screen point of a specific data point
	/**
		@param uPointIndex
			The index of the point for which to retrieve the screen point
		@return the screen point
	**/
	CPoint GetPointScreenCoord(unsigned uPointIndex);

	//! Register a series mouse listener on this series
	/**
		@param pListener
			The listener to register
	**/
	void RegisterMouseListener(CChartSeriesMouseListener<T>* pListener);
	//! Unregister the series mouse listener for this series
	void UnregisterMouseListener();

protected:
	//! Returns the first and last visible points of the series.
	/**
		This function only works if ordering is enabled.
		@param uFirst
			The index of the first visible point is stored in this argument
		@param uLast
			The index of the last visible point is stored in this argument
		@return false if the series has no ordering or no data points.
	**/
	bool GetVisiblePoints(unsigned& uFirst, unsigned& uLast) const;

	//! Called by the control to check if an event occured on the series.
	bool OnMouseEvent(CChartMouseListener::MouseEvent mouseEvent, 
					const CPoint& screenPoint);

	//! The helper class containing all the data points.
	CChartPointsArray<T> m_vPoints;
	//! Index of the last point drawn
	unsigned m_uLastDrawnPoint;		

private:
	//! Draws the labels of the series.
	void DrawLabels(CDC* pDC);

	typedef std::map<unsigned, CChartLabel<T>*> TLabelMap;
	//! Map containing the labels of the series.
	TLabelMap m_mapLabels;

	CChartSeriesMouseListener<T>* m_pMouseListener;
};

#include "ChartSerieBase.inl"

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Cedric Moonen
Engineer
Belgium Belgium
I am a 29 years old guy and I live with my girlfriend in Hoegaarden, little city from Belgium well known for its white beer Smile | :) .
I studied as an industrial engineer in electronics but I oriented myself more towards software development when I started to work.
Currently I am working in a research centre in mechatronica. I mainly develop in C++ but I also do a bit of Java.
When I have so spare time, I like to read (mainly fantasy) and play electric guitar.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 13 Jul 2010
Article Copyright 2006 by Cedric Moonen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid