/*
*
* ChartCtrl.cpp
*
* 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.
*
*
* History:
* - 18/05/2006: Added support for panning
* - 28/05/2006: Bug corrected in RemoveAllSeries
* - 28/05/2006: Added support for resizing
* - 12/06/2006: Added support for manual zoom
* - 10/08/2006: Added SetZoomMinMax and UndoZoom
* - 24/03/2007: GDI leak corrected
* - 24/03/2007: Invisible series are not taken in account for auto axis
* and legend (thanks to jerminator-jp).
* - 24/03/2007: possibility to specify a margin for the axis. Needs to be improved
* - 05/04/2007: ability to change the text color of the axis.
* - 05/04/2007: ability to change the color of the border of the drawing area.
* - 05/04/2007: Surface series added.
* - 26/08/2007: The clipping area of the series is a bit larger (they will be
* drawn over the bottom and right axes).
* - 12/01/2007: Ability to change the color of the zoom rectangle.
* - 08/02/2008: Added convenience functions to convert from date to value and
* opposite.
* - 21/02/2008: The zoom doesn't do anything if the user only clicks on the control
* (thanks to Eugene Pustovoyt).
* - 29/02/2008: The auto axis are now refreshed when a series is removed (thanks to
* Bruno Lavier).
* - 08/03/2008: EnableRefresh function added (thanks to Bruno Lavier).
* - 21/03/2008: Added support for scrolling.
* - 25/03/2008: UndoZoom function added.
* - 25/03/2008: A series can now be removed using its pointer.
* - 12/08/2008: Performance patch (thanks to Nick Holgate).
* - 18/08/2008: Added support for printing.
* - 31/10/2008: Fixed a bug for unicode.
*
*/
#include "stdafx.h"
#include "ChartCtrl.h"
#include "ChartSerie.h"
#include "ChartGradient.h"
#include "ChartStandardAxis.h"
#include "ChartDateTimeAxis.h"
#include "ChartLogarithmicAxis.h"
#include "ChartCrossHairCursor.h"
#include "ChartDragLineCursor.h"
#include "ChartPointsSerie.h"
#include "ChartLineSerie.h"
#include "ChartSurfaceSerie.h"
#include "ChartBarSerie.h"
#include "ChartCandlestickSerie.h"
#include "ChartGanttSerie.h"
#if _MFC_VER > 0x0600
#include "atlImage.h"
#endif
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CHARTCTRL_CLASSNAME _T("ChartCtrl") // Window class name
COLORREF pSeriesColorTable[] = { RGB(255,0,0), RGB(0,150,0), RGB(0,0,255), RGB(255,255,0), RGB(0,255,255),
RGB(255,128,0), RGB(128,0,128), RGB(128,128,0), RGB(255,0,255), RGB(64,128,128)};
/////////////////////////////////////////////////////////////////////////////
// CChartCtrl
CChartCtrl::CChartCtrl()
{
RegisterWindowClass();
m_iEnableRefresh = 1;
m_bPendingRefresh = false;
m_BorderColor = RGB(0,0,0);
m_BackColor = GetSysColor(COLOR_BTNFACE);
EdgeType = EDGE_RAISED;
m_BackGradientType = gtVertical;
m_bBackGradient = false;
m_BackGradientCol1 = m_BackGradientCol2 = m_BackColor;
for (int i=0;i<4;i++)
m_pAxes[i] = NULL;
m_pLegend = new CChartLegend(this);
m_pTitles = new CChartTitle(this);
m_bMemDCCreated = false;
m_bPanEnabled = true;
m_bRMouseDown = false;
m_bZoomEnabled = true;
m_bLMouseDown = false;
m_ZoomRectColor = RGB(255,255,255);
m_bToolBarCreated = false;
m_pMouseListener = NULL;
m_bMouseVisible = true;
}
CChartCtrl::~CChartCtrl()
{
TSeriesMap::iterator seriesIter = m_mapSeries.begin();
for (seriesIter; seriesIter!=m_mapSeries.end(); seriesIter++)
{
delete (seriesIter->second);
}
TCursorMap::iterator cursorIter = m_mapCursors.begin();
for (cursorIter; cursorIter!=m_mapCursors.end(); cursorIter++)
{
delete (cursorIter->second);
}
for (int i=0; i<4 ;i++)
{
if (m_pAxes[i])
delete m_pAxes[i];
}
if (m_pLegend)
{
delete m_pLegend;
m_pLegend = NULL;
}
if (m_pTitles)
{
delete m_pTitles;
m_pTitles = NULL;
}
}
BEGIN_MESSAGE_MAP(CChartCtrl, CWnd)
//{{AFX_MSG_MAP(CChartCtrl)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDBLCLK()
ON_WM_SIZE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChartCtrl message handlers
void CChartCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (!m_bMemDCCreated)
{
RefreshCtrl();
m_bMemDCCreated = true;
}
// Get Size of Display area
CRect rect;
GetClientRect(&rect);
dc.BitBlt(0, 0, rect.Width(), rect.Height(),
&m_BackgroundDC, 0, 0, SRCCOPY) ;
// Draw the zoom rectangle
if (m_bZoomEnabled && m_bLMouseDown)
{
CPen NewPen(PS_SOLID,1,m_ZoomRectColor);
CPen* pOldPen = dc.SelectObject(&NewPen);
dc.MoveTo(m_rectZoomArea.TopLeft());
dc.LineTo(m_rectZoomArea.right,m_rectZoomArea.top);
dc.LineTo(m_rectZoomArea.BottomRight());
dc.LineTo(m_rectZoomArea.left,m_rectZoomArea.bottom);
dc.LineTo(m_rectZoomArea.TopLeft());
dc.SelectObject(pOldPen);
DeleteObject(NewPen);
}
// Draw the cursors.
TCursorMap::iterator iter = m_mapCursors.begin();
for (iter; iter!=m_mapCursors.end(); iter++)
iter->second->Draw(&dc);
}
BOOL CChartCtrl::OnEraseBkgnd(CDC* )
{
// To avoid flickering
// return CWnd::OnEraseBkgnd(pDC);
return FALSE;
}
CChartCrossHairCursor* CChartCtrl::CreateCrossHairCursor(bool bSecondaryHorizAxis,
bool bSecondaryVertAxis)
{
CChartAxis* pHorizAxis = NULL;
CChartAxis* pVertAxis = NULL;
if (bSecondaryHorizAxis)
pHorizAxis = m_pAxes[TopAxis];
else
pHorizAxis = m_pAxes[BottomAxis];
if (bSecondaryVertAxis)
pVertAxis = m_pAxes[RightAxis];
else
pVertAxis = m_pAxes[LeftAxis];
ASSERT(pHorizAxis != NULL);
ASSERT(pVertAxis != NULL);
CChartCrossHairCursor* pNewCursor = new CChartCrossHairCursor(this, pHorizAxis, pVertAxis);
m_mapCursors[pNewCursor->GetCursorId()] = pNewCursor;
return pNewCursor;
}
CChartDragLineCursor* CChartCtrl::CreateDragLineCursor(EAxisPos relatedAxis)
{
ASSERT(m_pAxes[relatedAxis] != NULL);
CChartDragLineCursor* pNewCursor = new CChartDragLineCursor(this, m_pAxes[relatedAxis]);
m_mapCursors[pNewCursor->GetCursorId()] = pNewCursor;
return pNewCursor;
}
void CChartCtrl::AttachCustomCursor(CChartCursor* pCursor)
{
m_mapCursors[pCursor->GetCursorId()] = pCursor;
}
void CChartCtrl::RemoveCursor(unsigned cursorId)
{
TCursorMap::iterator iter = m_mapCursors.find(cursorId);
if (iter != m_mapCursors.end())
{
delete iter->second;
m_mapCursors.erase(iter);
}
}
void CChartCtrl::ShowMouseCursor(bool bShow)
{
m_bMouseVisible = bShow;
if (!bShow)
SetCursor(NULL);
}
CChartStandardAxis* CChartCtrl::CreateStandardAxis(EAxisPos axisPos)
{
CChartStandardAxis* pAxis = new CChartStandardAxis();
AttachCustomAxis(pAxis, axisPos);
return pAxis;
}
CChartLogarithmicAxis* CChartCtrl::CreateLogarithmicAxis(EAxisPos axisPos)
{
CChartLogarithmicAxis* pAxis = new CChartLogarithmicAxis();
AttachCustomAxis(pAxis, axisPos);
return pAxis;
}
CChartDateTimeAxis* CChartCtrl::CreateDateTimeAxis(EAxisPos axisPos)
{
CChartDateTimeAxis* pAxis = new CChartDateTimeAxis();
评论2