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
A flexible charting control to display 2D data
Prize winner in Competition "MFC/C++ Apr 2006"
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
chartctrl_src.zip
chartctr_demo.zip
res
ChartDemo.ico
/*
 *
 *	ChartAxis.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.
 *
 */

#include "stdafx.h"
#include "ChartStandardAxis.h"
#include "ChartCtrl.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

CChartStandardAxis::CChartStandardAxis()
 : CChartAxis(), m_dFirstTickValue(0), 
   m_dTickIncrement(1.0), m_uDecCount(0)	
{
}

CChartStandardAxis::~CChartStandardAxis()
{
}


void CChartStandardAxis::SetTickIncrement(bool bAuto, double newIncrement)		
{ 
	m_bAutoTicks = bAuto;
	if (!m_bAutoTicks)
	{
		m_dTickIncrement = newIncrement; 
	
		int Zeros = (int)floor(log10(m_dTickIncrement));

		int Digits = 0;
		if (Zeros<0)		
		{
			//We must set decimal places. In the other cases, Digits will be 0.
			Digits = (int)fabs(Zeros*1.0);
		}
		SetDecimals(Digits);
	}
}

double CChartStandardAxis::GetFirstTickValue() const
{ 
	double dRetVal = m_dFirstTickValue;
	if (m_bDiscrete)
	{
		dRetVal = m_dFirstTickValue - m_dTickIncrement;
	}
	return dRetVal; 
}

bool CChartStandardAxis::GetNextTickValue(double dCurrentTick, double& dNextTick) const
{
	if (m_dTickIncrement==0)
		return false;

	dNextTick = dCurrentTick + m_dTickIncrement;
	if (dNextTick <= m_MaxValue)
		return true;
	else
		return false;
}

long CChartStandardAxis::ValueToScreenDiscrete(double dValue) const
{
	// In discrete mode, all values between two ticks are "directed"
	// to the middle of the interval.
	double precision = 0.0000000001;
	if (dValue < 0)
		precision = -0.0000000001;
	int tickNr = (int)((dValue+precision)/m_dTickIncrement);
	dValue = tickNr * m_dTickIncrement;
	
	dValue += m_dTickIncrement/2.0;
	return ValueToScreenStandard(dValue);
}

long CChartStandardAxis::GetTickPos(double TickVal) const
{
	// The tick is always at the same position,
	// even if the axis is discrete.
	return ValueToScreenStandard(TickVal);
}

TChartString CChartStandardAxis::GetTickLabel(double TickValue) const
{
	TChartStringStream ssLabel;
	ssLabel << fixed << setprecision(m_uDecCount) << TickValue;
	return ssLabel.str();
}

void CChartStandardAxis::RefreshTickIncrement()
{
	if (!m_bAutoTicks)
		return;

	if (m_MaxValue == m_MinValue)
	{
		m_dTickIncrement = 0;
		return;
	}

	int PixelSpace;
	if (m_bIsHorizontal)
		PixelSpace = 30;
	else
		PixelSpace = 20;

	int MaxTickNumber = (int)fabs((m_EndPos-m_StartPos)/PixelSpace * 1.0);

	//Calculate the appropriate TickSpace (1 tick every 30 pixel +/-)
	//Temporary tick increment
	double TempTickIncrement = (m_MaxValue-m_MinValue)/MaxTickNumber;

	// Calculate appropriate tickSpace (not rounded on 'strange values' but 
	// on something like 1, 2 or 5*10^X  where X is optimalized for showing the most
	// significant digits)
	int Zeros = (int)floor(log10(TempTickIncrement));
	double MinTickIncrement = pow(10.0,Zeros);

	int Digits = 0;
	if (Zeros<0)		
	{
		//We must set decimal places. In the other cases, Digits will be 0.
		Digits = (int)fabs(Zeros*1.0);
	}

	if (MinTickIncrement>=TempTickIncrement)
	{
		m_dTickIncrement = MinTickIncrement;
		SetDecimals(Digits);
	}
	else if (MinTickIncrement*2>=TempTickIncrement)
	{
		m_dTickIncrement = MinTickIncrement*2;
		SetDecimals(Digits);
	}
	else if (MinTickIncrement*5>=TempTickIncrement)
	{
		m_dTickIncrement = MinTickIncrement*5;
		SetDecimals(Digits);
	}
	else if (MinTickIncrement*10>=TempTickIncrement)
	{
		m_dTickIncrement = MinTickIncrement*10;
		if (Digits)
			SetDecimals(Digits-1);
		else
			SetDecimals(Digits);
	}
}

void CChartStandardAxis::RefreshFirstTick()
{
	if (m_dTickIncrement!=0)
	{
		if (m_MinValue == 0)
			m_dFirstTickValue = 0;
		else if (m_MinValue>0)
		{
			m_dFirstTickValue = (int)(m_MinValue/m_dTickIncrement) * m_dTickIncrement;
			while (m_dFirstTickValue<m_MinValue)
				m_dFirstTickValue += m_dTickIncrement;
		}
		else
		{
			m_dFirstTickValue = (int)(m_MinValue/m_dTickIncrement) * m_dTickIncrement;
			while (m_dFirstTickValue>m_MinValue)
				m_dFirstTickValue -= m_dTickIncrement;
			if (!(m_dFirstTickValue == m_MinValue))
				m_dFirstTickValue += m_dTickIncrement;
		}
	}
	else	// m_TickIncrement!=0
	{
		m_dFirstTickValue = m_MinValue;
	}
}

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)

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 Jul 2010
Article Copyright 2006 by Cedric Moonen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid