Click here to Skip to main content
Click here to Skip to main content

Scientific charting control

By , 17 Jan 2005
 

Introduction

Displaying large amounts of technical data in a chart can be a frustrating task. You can find tons of charting controls with fancy effects and useless features, but when it comes to displaying many curves at once, independently scaled on different axes, most of them fail. Maybe CXGraph is the solution:

CXGraph can display unlimited series of data on multiple axes.

The documentation is sparse, because I do not have the time to write documentation. However, the included demo is hopefully a help in understanding how to use the control.

Features

  • unlimited data series
  • unlimited axes (your monitor is the limit)
  • logarithmic scales
  • autoscaling
  • automatic/manual axis placement
  • nearly every visual aspect can be customized
  • legends
  • bitmaps
  • cursor (add data notation marks by clicking)
  • zoom
  • pan
  • color ranges
  • object selection with the mouse
  • linear trend
  • cubic trend
  • nth order polynomial trend
  • data notation marks
  • moving average (simple, linear, sine-weighted ...)
  • property persistence (save and load properties, data and axes)
  • Support for Windows CE
  • axis markers
  • UNICODE support
  • polygon filling between curves
  • cursor synchronization
  • data editing
  • measuring mode

Classes

CXGraph

CXGraphDataSerie& SetData(TDataPoint* pValues, long nCount, int nCurve, int nXAxis, int nYAxis, bool bAutoDelete) Adds/sets a data series.
CXGraphAxis& GetXAxis(int nAxis) Returns reference to specified X-axis.
CXGraphAxis& GetYAxis(int nAxis) Returns reference to specified y-axis.
CXGraphDataSerie& GetCurve(int nCurve) Returns reference to specified curve.
bool SelectCurve(int nCurve) Selects the specified curve.
bool SelectXAxis(int nAxis) Selects the specified x-axis.
bool SelectYAxis(int nAxis) Selects the specified y-axis.
int GetXAxisCount() Returns x-axis count.
int GetYAxisCount() Returns y-axis count.
int GetCurveCount() Returns curve count.
void Zoom() Zoom-mode. User can zoom into the graph with the mouse.
void ResetZoom() Resets all axes to optimal values.
void Pan() Pan-mode. User can pan the graph with the mouse.
void NoOp() Select-mode. User can select objects with the mouse.
void Cursor() Cursor-mode. A cursor is being displayed showing the values at the current mouse position.
void Measure() Measuring mode. User can measure the distance between two data points.
void Edit() Editing mode. User can edit the curve data.
CXGraphLabel& InsertLabel(CString cText) Inserts a text-label. Returns reference to a CXGraphLabel object.
void PrintGraph(CDC *pDC) Prints the graph.
void SetBackColor(COLORREF color) Sets the chart's backcolor.
void SetGraphColor(COLORREF color) Sets the chart's exterior color.
void SetInnerColor(COLORREF color) Sets the chart's interior color.
void SetDoubleBuffer(bool bValue) Sets double buffering on/off.
void SetShowLegend(bool bValue) Sets legend display on/off.
void SetLegendAlignment(EAlignment align) Sets the legend alignment (left, right).
void SetSnapCursor(bool bValue) Sets snap cursor mode (on/off).
void SetInteraction(bool bValue) Enable/disable interaction.
bool GetInteraction()
void SetSnapRange(int nValue) Sets snap cursor range.
bool Save(const CString cFile, UINT nFlags)

Stores chart properties, data and axes. Use one or more of the following values to specify what to be stored:

  • PERSIST_PROPERTIES: store chart properties.
  • PERSIST_DATA: store chart data and axes.
  • PERSIST_OBJECTS: store chart objects (labels, ...).
bool Load(const CString cFile) Loads chart properties, data and axes.
bool SaveBitmap(const CString cFile) Saves the chart as bitmap.
bool DeleteCurve(int nCurve) Deletes the specified curve.
bool DeleteYAxis(int nAxis) Removes the specified y-axis.
bool DeleteXAxis(int nAxis) Removes the specified x-axis.
int GetZoomDepth() Determines the zoom depth (history).
void RestoreLastZoom() Restores the last zoom stage.
void SetBackgroundBitmap(CBitmap* pBitmap, UINT nFlags) Sets a background image.
void InsertBitmap(CRect rect, CBitmapEx* pBitmap) Inserts a bitmap-object.

CXGraphLabel

SetFont(LOGFONT* pLogFont) Sets the label's font.
SetText(CString cText) Sets the label's text.
SetColor(COLORREF color) Sets the label's background color.
SetTextColor(COLORREF color) Sets the label's text color.
SetBorder(bool bValue) Sets the label's border.
SetTransparent(bool bValue) Sets the label's transparency.
SetAlignment(UINT nValue) Sets the label's text alignment.
SetCurve(int nCurve) Links the label to the specified curve.
CString GetText() Returns the label's text.
COLORREF GetColor() Returns the label's background color.
COLORREF GetTextColor() Returns the label's text color.
bool GetBorder() Returns true if label has a border, otherwise false.
bool GetTransparent() Returns true if label is transparent. otherwise false.
UINT GetAlignment() Returns the label's text alignment.
int GetCurve() Returns the label's associated curve (or -1 if not associated).

CXGraphAxis

See source code

CXGraphDataSerie

See source code

Notifications

  • XG_YAXISCLICK
  • XG_XAXISCLICK
  • XG_CURVECLICK
  • XG_YAXISDBLCLICK
  • XG_XAXISDBLCLICK
  • XG_CURVEDBLCLICK
  • XG_GRAPHDBLCLICK
  • XG_RBUTTONUP
  • XG_CURSORMOVED

Usage

To use this control in your own application, add XGraph10.lib to your project and make sure XGraph10.dll is accessible. Create an instance of CXGraph and call Create on it. Take a look at the sample application for further details.

Credits

  • James White - Color button.
  • Chris Maunder, Alexander Bischofberger - Color popup.
  • Dennis Crain (MSDN) - Routines for line-hit testing.
  • Keith Rule - CMemDC.
  • Ishay Sivan (bug fixing).
  • Pavel Klocek (bug fixing).
  • J.G. Hattingh (parts of Device Context Utilities).
  • Jörg Blattner (bug fixing, improvements).

History & Bug fixes

Date Version Comment
21 Mar 2002 1.00
  • First release.
25 Mar 2002 1.01
  • Fixed DateTime-bug in cursor mode.
  • Fixed crash when adding trend or moving average.
17 Apr 2002 1.02
  • Panning in cursor mode added.
  • Added support for very large and very small numbers.
  • Values greater/less than 10E6/10E-6 are now automatically shown in exponential format.
  • Completely redesigned autoscaling (only for normal axes, DateTime types follow).
  • Added data notation marks.
  • Added Get/Set member functions.
  • Added nth-order-polynomial trend.
20 Sep 2002 1.03
  • Now compiles under VC7.
  • Load/save for properties/data/object persistence.
  • Lots of bug fixes.
20 Nov 2003 1.04
  • Added Unicode support.
  • Fixed crash when resizing chart to small dimensions.
  • Corrected tab-order in property pages.
  • Added background bitmaps.
  • Added gradient color ranges.
  • Added axis markers.
  • Added bitmap objects.
13 Jan 2005 1.05
  • Added support for Windows CE (needs improvement).
  • Logarithmic axes (thanks to ...).
  • Added polygon filling between two curves.
  • Several bugs fixed by Joerg Blattner.
  • Added Cursor-Synchronization.
  • Curve editing mode.
  • Measuring mode.

Known issues

  • Clipping in print preview still doesn't work.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gunnar Bolle
Web Developer
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAccuracy of Graphsmembersseredn25 Feb '13 - 18:59 
Hi !
I have amazed by the high speed of graph drawing by you control. But exploring its sources, searching for the secret of this high perfomance, i have found in method
void CXGraphDataSerie::Draw(CDCEx *pDC)

 
folowing string:
 
if (point.x != oldPoint.x )
    {
        if ((m_pGraph->m_clInnerRect.PtInRect (point) || m_pGraph->m_clInnerRect.PtInRect (oldPoint)))
            CreateCurveGripRgn(oldPoint, point);
 
        if (m_gtType == gtLine)
            pDC->LineTo(point);
 

this means, that line is drawing, only if x coordinate of next point will change. But what if data x coordinate of next point will not be changed noticeably, but y coord. will change greatly ? We will lose this point, and will not see it on the graph ??
QuestionCompiling with Visual Studio 2010memberMember 786633813 Dec '12 - 22:44 
I downloaded the Demo project, and compiled it in Visual Studio 2010.
 
First I got some compiler errors, which I could fix by defining i differently in CXGraphTestView::AddDataSerie and adding void as type specifier where it was missing.
 
It then compiled without errors, but the linker gave me two error, it couldn't find CXGraph::InsertLabel(class ATL::CStringT... ) and CXGraphAxis::SetColorRange(..).
 
This is because Visual Studio 2010 uses ATL for CString which makes CString to be CStringT, but the lib exports the "old" CString.
 
Any workaround for this available?
QuestionHow can I put text labels on an axis?memberTom Paronis31 Oct '12 - 12:50 
I've been using this nice component for a while in an option risk package but the person who wrote that part of the system is gone and I have to make some modifications. I'm unable to compile it in Visual Studio 2010. I'd like to be able to do that but, more importantly, I'd like to be able to label the horizontal axis with text and can't figure out how to do that. Could someone please tell me how to link a text array to an axis? Also, looking ahead, I'd like to be allow users to enter data points. More generally, I'd like to get up to speed with this ASAP. Any help would be appreciated. Thank you.
QuestionLicensememberYusein Ali28 Feb '12 - 22:57 
do you have a license for this nice tool? If we have a modified version of the tool can we release it under GPLv3 as derivative of your software?
AnswerRe: Licensememberxawari29 Oct '12 - 4:56 
I think that is possible. Cannot contact the original author though.. Frown | :(
At least you can share your part with us here.
in another thousand years we'll be machines or gods█

GeneralRe: LicensememberTom Paronis1 Nov '12 - 5:56 
Xawari; I'm hoping you can give me some guidance. I URGENTLY need to do some work with this charting component and I'm sort of stumbling in the dark. I can figure some things out by looking at the code but my progress would be much faster if I could direct questions to a qualified authority. It's all basic stuff; labeling, editing, etc. Thanks in advance.
 
Tom Paronis
GeneralRe: Licensememberxawari18 Nov '12 - 3:46 
I'm very sorry to disappoint you, but last time I touched MFC and XGraph in particular, was 5 years ago and I can only say that I do not remember a single thing there. I can look for example left from the time I used XGraph (AFAIR I wrote a program for simple point-by-point waveform editing). Don't expect that soon, because in best rare cases I have internet access twice a week. 8(
 
PS: can't understand why comment about license was downvoted.. wtf? o_O
 
in another thousand years we'll be machines or gods█
GeneralRe: LicensememberTom Paronis18 Nov '12 - 5:59 
I appreciate your response. I've been plugging away and have been able to figure out most of what I need to know. It would just be easier if I could communicate with somebody who knows the code better than me. I'm sure you know how difficult it can be to wade through other people's code.
 
Tom
Suggestionlook here for UPDATED sources!memberxawari20 Feb '12 - 6:54 
Download here, no registration (at least for now Smile | :) ):
NetHeaven -> Downloads -> Projects -> XGraph
Just a new URL, I did not update the package itself. If you happen to modify it, PLEASE POST IT HERE as reply to keep the development! Thumbs Up | :thumbsup:
in another thousand years we'll be machines or gods█

Question64 bit compatibility ?memberbeauwlf12 Jul '11 - 23:02 
Seems like a cool Graph Tool. But i would like to know if the DLL can work in 64 bit or the author provide a 64 bit version of the dll ?
regards,

GeneralMy vote of 5memberbigbigmonkey17 May '11 - 22:37 
Thanks for share.
GeneralExcellentmembersisixiao12 Mar '11 - 15:35 
Thanks for sharing
Generalquestion on your trending algorithmsmemberMichael Eber24 Jun '10 - 5:28 
for your linear trending, what was the formula that you used?
QuestionHow To Port The Control To WINDOWS CEmemberten319830 May '10 - 16:21 
How To Port The Control To WINDOWS CE?
GeneralXGraph von Codeproject.com lässt sich nicht übersetzenmemberHSCH_20106 Mar '10 - 0:47 
Hallo ich habe mir von der Internetseite codeproject.com das Demo von
Xgraph heruntergeladen. Mit Microsoft Visual Studio C++ 2008 (Prof.)
habe ich dieses Projekt geöffnet. Beim Übersetzen erscheinen zahlreiche
Fehlermeldungen. Wahrscheinlich liegt es daran, dass dieses Projekt ein
MFC Projekt ist. Müsste ich in Microsoft Visual Studio C++ 2008 (Prof.)
irgendwelche Einstellungen vornehmen?
 
Fehlermeldung:
Fehler 1 error C4430: Fehlender Typspezifizierer - int wird
angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
c:\dokumente und
einstellungen\user\desktop\xgraph_demo\include\gfxutils.h 70
XGraphTest
GeneralVS2008 problem.....help me, please.memberSeungkwon Kim21 Dec '09 - 17:52 
Hello, I want to use this control on the VS2008 project.
 
I have used it on VC6, it was really nice control.
However, I cannot use it on the VS2008.....
 
Please, somebody help me..
GeneralRe: VS2008 problem.....help me, please.memberJack Lu18 May '10 - 22:29 
I am using in VS2008 now...
QuestionIs This support Bar Graph ? or any plan for support Bar Graph ?membergsheladia16 Dec '09 - 6:16 
Hi , XGraph10 can support Bar chart ? or any plan for supporting bar chart ?
GeneralCompile under vs2005membermaruigxxg10 Dec '09 - 20:18 
Compile under vs2005, may help someone.
1. open the project under the vs2005;
2. build it, there are some errors, may “fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]”, open the stdafx.h, and add the code “#define _AFXDLL”;
3. build it again, may some errors like this “error C2065: 'i' : undeclared identifier”, please define it , for example, modify the code “for (i = 0; i < (m_nCount + nPoints); i ++)” to “for (int i = 0; i < (m_nCount + nPoints); i ++)”. All the other errors like this, may change it.
4. then build the project, “error C2668: 'pow' : ambiguous call to overloaded function” may be found, and make the code “double(i+1)” replace the code “i+1” in the line “(pow (i+1, 6));”;
5. now, there are less errors, may some errors about vector, for example, let the code line “m_ColorRanges.erase (&m_ColorRanges[nRange]);” replaced by the line “m_ColorRanges.erase (m_ColorRanges.begin()+nRange);”; all the other same errors may modified like this.
6. and then open the menu project->properties->configuration properties->c/c++->command line, add the code “/wd4430” to the box.
7. all of the errors have modified, and have a good fun.
GeneralRe: Compile under vs2005memberxicexice12 Apr '11 - 17:13 
Good Work for vs2005, it also works with vs2008/vs2010 using this method.
Smile | :)
maruigxxg wrote:
Compile under vs2005, may help someone.
1. open the project under the
vs2005;
2. build it, there are some errors, may “fatal error C1189: #error :
Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll
version. Please #define _AFXDLL or do not use /MD[d]”, open the stdafx.h, and
add the code “#define _AFXDLL”;
3. build it again, may some errors like this
“error C2065: 'i' : undeclared identifier”, please define it , for example,
modify the code “for (i = 0; i < (m_nCount + nPoints); i ++)” to “for (int i
= 0; i < (m_nCount + nPoints); i ++)”. All the other errors like this, may
change it.
4. then build the project, “error C2668: 'pow' : ambiguous call to
overloaded function” may be found, and make the code “double(i+1)” replace the
code “i+1” in the line “(pow (i+1, 6));”;
5. now, there are less errors, may
some errors about vector, for example, let the code line “m_ColorRanges.erase
(&m_ColorRanges[nRange]);” replaced by the line “m_ColorRanges.erase
(m_ColorRanges.begin()+nRange);”; all the other same errors may modified like
this.
6. and then open the menu project->properties->configuration
properties->c/c++->command line, add the code “/wd4430” to the box.
7.
all of the errors have modified, and have a good fun.

GeneralRe: Compile under vs2005memberbobhappy826 Jul '11 - 16:19 
thanks!!! ^^;;
GeneralRe: Compile under vs2005membermisasagi_inori12 Dec '11 - 14:26 
I still have a problem,since i have modified all the problems,it occurs that:fatal error C1083: 无法打开预编译头文件:“.\Debug\XGRAPH.pch”: No such file or directory
GeneralRe: Compile under vs2005membernoending18 Nov '12 - 19:27 
My Advice is to Rebuild the project.
GeneralA probable bug concerning autoscalingmemberdidov22 Jul '09 - 6:27 
The (probable) incorrect behavior was noticed on the following setup:
 
- Display some linear data serie on the graph (e.g 1-10000 on the X-axis and some random data between 1500 and 2500 on the Y-axis)
- Autoscale the graph using CXGraph::Autoscale(). Y-grid step becomes 20
- Bring some random window (e.g Notepad) on top of the graph. This causes an internal CXGraphAxis::Redraw() and CXGraphAxis::Autoscale() (presuming that both axis are left autoscalable)
- I notice that the Y-grid step unexpectedly changes to 50, Graph line also changes it's placement in the control.
 
After tweaking on the XGraph code I noticed the following -> At the end of the function CXGraphAxis::Autoscale() the axis-start is tweaked to be a divisor of the tick step (see CXGraphAxis::AdaptAxis())
 
The problem is that AdaptAxis() changes the member m_fCurMin/m_fCurMax instead of the data serie fixed min/max. This results in sudden change of axis' scale after continuous redraw of the graph.
 
Best regards,
Dido.
GeneralPrinting!!memberdd-pp-qq6 May '09 - 16:05 
Great work Gunnar;
I want to know how to print serval charts in one page,how to modify the function OnFilePrint()...
 

Best Regards~

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 17 Jan 2005
Article Copyright 2002 by Gunnar Bolle
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid