Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC

Plot Graphic Library

Rate me:
Please Sign up or sign in to vote.
4.95/5 (70 votes)
7 May 2003LGPL36 min read 1.4M   51.3K   383   301
A library to plot data (lines, maps...) in MFC projects
In this article, you will see a library called PGL that encapsulates plot capabilities in a MFC project for VC6 and VC7. It can easily plot data generated in a project without the need of any external software.

Snapshot

Description

PGL is a library that encapsulates plot capabilities in a MFC project for VC6 and VC7. It is designed to be able to easily plot data generated in a project without the need of any external software. In fact, with CView and CDialog derived classes, you can have your app display chart in 5 minutes.

The aim of PGL is not to have a user-friendly environment but rather be able to generate any plot from the source code.

PGL was originally using OpenGL to raster graphics, but now it uses GDI+ (so you need to install Microsoft SDK to compile PGL).

Licensing

The source available on CodeProject is licensed under LGPL. However, the next releases are not free anymore (Ooops, sorry). You can check the latest development at PGL Home Page. Anyway, enjoy beautiful charting.

Features

  • Line strip, fully customisable:
    • color (RGBA)
    • line dashing
    • point type (circle, box, triangle, etc.)
    • line width
    • filled
    • line shadow
    • multiple line strip
    • etc.
  • Line strip with level of detail capabilities (based on Douglas-Peukler line simplification algorithm)
  • Vector map
  • Height map
  • Text
    • variable scale
    • multiple fonts
    • orientable
  • Unlimited sub-plotting
  • Automatic axis
  • Time labelling
  • Export to EPS, SVG, JPEG, TIFF, PNG
  • CView derived class for fast integration in existing project
  • CDialog derived class, etc.

UML

A UML diagram is available in PDF here. It is not complete, but it should help you in understanding the library.

Installation

Here are the installation steps to use PGL in one of your projects:

  1. Install GDI+ (part of Microsoft SDK).
  2. Download Gdiplus.dll and make sure it is in the path.
  3. Recompile the source, it will build .lib in the lib directory and the .dll in the bin directory.
  4. Add the directory with PGL binaries to your path (by default, it is C:\Program Files\PGL\bin).
  5. Add the include directory and lib directory to Visual C++ include/lib directories.
  6. Make sure the headers are available.

That's it!

Getting Your Project Started

  1. Add the following in your StdAfx.h file:
    C++
    #include "PGL.h"
  2. Since PGL is using GDI+, you must initialize it:

    • Add the following variable to your CWinApp derived class:
      C++
      ULONG_PTR m_ulGdiplusToken;
    • Add the following to the CWinApp::OnInitInstance function to initialize GDI+:
      C++
      // initialize <code>GDI+ (gdi+ is in Gdiplus namespace)
      Gdiplus::GdiplusStartupInput gdiplusStartupInput;
      Gdiplus::GdiplusStartup(&m_ulGdiplusToken, &gdiplusStartupInput, 
                              NULL);
    • Add the following to the CWinApp::OnExitInstance function to clean up GDI+.
      C++
      // shutdown GDI+
      Gdiplus::GdiplusShutdown(m_ulGdiplusToken);

Your project should work fine now.

Examples

All these examples are accessible in the source. See the example menu of TestPGL.

Example 1: Drawing a Simple Line

This is a first explanatory example. We suppose that the points (x,y) of the line have been generated and are stored in two array pX,pY of size nPoints (note that you can also pass data as std::vector<double> to PGL).

Snapshot

Here's the code I used to generate the data: a simple sinusoid. Note that the y are in [-1.1, 1.1] but PGL will handle axe labelling the have nice units.

C++
// generate data
int nPoints = 50;
double* pX=new double[nPoints];
double* pY=new double[nPoints];
for (UINT i=0;i< nPoints;i++)
{
	pX[i]=i;
	pY[i]=sin(i/(double)nPoints*2*3.14)*1.1;
}
  1. First, create a graph object:
    C++
    CPGLGraph* pGraph = new CPGLGraph;
    Note that you can check all PGL object with ASSERT_VALID since they all inherit from CObject.
  2. Create a 2D line:

    C++
    CPGLLine2D* pLine = new CPGLLine2D;
  3. Attach the data to the line. PGL will handle the memory afterwards. That is, it will delete the pointers of data at the object destruction. This means pX,pY MUST have been allocated on the heap!

    C++
    pLine->SetDatas( nPoints /* number of points */, 
                       pX /* x(i) */, pY /* y(i) */);
  4. (Optional) Change some properties of the line: pLine->SetLineWidth(2);

  5. Add the line to the graph (note that an object can be added to only one graph):
    C++
    pGraph->AddObject(pLine);
  6. Make PGL scale the plot (automatically):

    C++
    pGraph->ZoomAll();
  7. Create a dialog box and display the plot:

    C++
    CPGLGraphBitDlg graphdlg(this, pGraph);
    graphdlg.DoModal();

You should have the same as the image above. Note that this image (PNG) has been generated by PGL.

Example 2: Adding a Line With Level of Detail Control

You may have to plot line with thousands of points. This can become very heavy and especially if you export it to EPS, the files can become very large. To overcome this problem, you can use a line with LOD included in PGL.

Snapshot

In this example, we approximate the previous line. Starting from the previous example:

  1. Change the line of code:
    C++
    CPGLLine2D* pLine = new CPGLLine2D;

    to:

    C++
    CPGLLine2DLOD* pLine = new CPGLLine2DLOD;
  2. Change tolerance of level of detail:
    C++
    pLine->SetTol(0.05);
  3. Shrink the number of points by a desired compression ratio (here to 10% with 2% threshold):
    C++
    pLine->ShrinkNorm(0.1,0.02);

In the figure above, you can see the original line and the approximated one. You can gain a serious amount of points using this technique!

Example 3: Customizing Axis, Labeling, etc.

As you can see in the previous image, all the parameters of the objects are changeable in the code. In this example, we shall

  • change the title text,
  • turn off horizontal grid,
  • show right label,
  • change number of ticks on the top axis,
  • switch to time labelling for the x-axis,
  • and more...

Snapshot

We start from the second example and add the following line of code before calling ZoomAll().

  1. Get a pointer the axis object (there's a huge mistake in English but in French it's ok :)(axe -> axis)):
    C++
    CPGLAxe2D* pAxis = pGraph-&gtGetAxe();
  2. Change the title text and color:
    C++
    pAxis->SetTitle(str);
    or:
    C++
    pAxis->GetTitle()->SetString(str);
    C++
    pAxis->GetTitle()->SetColor(0 /* red */,0.5f /* green */,
                                0 /* blue*/ /* alpha optional */);
  3. Turn off vertical grid, (vertical -> 0, horizontal -> 1):
    C++
    pAxis->SetShowGrid(1,FALSE);
  4. Show and change right label:
    C++
    pAxis->GetRightLabel()-&gtShow(TRUE);
    pAxis->GetRightLabel()-&gtSetString("This is the right label");
  5. Show right numbering:
    C++
    pAxis->GetRightNumber()->Show();
  6. Changing number of ticks on the top axis:
    C++
    pAxis->SetTopSecondTicksNb(5);
  7. Switch to time labelling the x-axis:
    C++
    // enable time labelling
    pAxis->SetTimeLabel(TRUE);
    // set origin, time step and format (see COleDateTime.Format for details)
    pAxis->SetTimeLabelFormat(COleDateTime::GetCurrentTime() 
                                             /* Time at zero. */, 
    			COleDateTimeSpan(0,0,30,0)   /* Time per unit */,
    			"%H:%M:%S"                   /* String format */);

I've also disabled the line drawing and set the tolerance to 0.025 for the LOD line. Of course, you can do much more. This is just an example.

Example 4: Sub-plotting!

What about putting multiple plots on a figure: that's possible in PGL in many ways. In fact, you can add plots to plots, and so on.

Snapshot

The class CPGLGraph is inherited from a generic plot class: CPGLRegion. You can either

  • use the function Divide(m,n) to divide the region in an array of m rows and n columns (Note that this method erase all object in the region). After that, you can access the elements with GetChilds(i) (the regions are created row by row). You can get the number of children with GetNChilds():
    C++
    // allocated somewhere
    CPGLRegion* pRegion;
    // dividing
    pRegion->Divide(m,n);
    // accessing region at row 2 and column 1 (zero based index)
    CPGLRegion* pChildRegion = pRegion->GetChild(2*n+1);
  • create an add directly a region using AddRegion. To use this method, you must SetNormBBox(...) to set the bounding box (in Normalized coordinates with respect to the parent region).
    C++
    CPGLRegion* pChildRegion = pRegion->AddRegion();
    pChildRegion->SetNormBBox(0.1 /* llx */ , 0.2 /* lly */ , 
                              0.7 /* urx */ , 0.8 /* ury */);

Of course, you can divide child regions and so on.

Example 5: Changing Properties of Objects at Runtime

You can explore the object hierarchy by right clicking the view or dialog. Unfortunately, serialization is not working yet. So it is lost work...

Snapshot

Reference

The documentation is generated with Doxygen and Doxygen studio. See Plot Graphic Library.dow file. Otherwise, it is shipped with the Microsoft Installer.

Download

You can download the Microsoft installer at the PGL Home Page.

Compiling the Sources

The sources of PGL are provided. Open the workspace:

  • "Plot Graphic Library.dsw" for VC6 users
  • "Plot Graphic LibraryNET.dsw" for VC7 users

It contains 6 projects:

  • AlgoTools. Collection of Algorithmic classes. This project contains the algorithm for line approximation.
  • IGfx. Graphic library used by PGL. Multi-layer graphic interface to export in multiple graphic format such as EPS or SVG
  • IGfxTest. Test project for IGfx
  • OGLTools. A useful library to handle OpenGL with Windows. Not used anymore but useful anyway
  • PGL. The graphic library
  • Testpgl. A demo application

Compile the sources. The .lib will be located in the lib directory, and DLLs in bin directory.

History

  • 6th November, 2002: Added VC7 build, fixed some stuff and updated DPHull
  • 15th July, 2002: Fixed resource missing, change CP_THREAD_ACP to CP_ASP, fixed, export bug and text strip bug
  • 5th June, 2002: Updated downloads
  • 29th March, 2002: Big update!
  • 8th November, 2001: Initial release

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions

 
QuestionI got error when I tried to link testpgl with PGL.dll or PGL.lib. Pin
Member 149935186-Feb-22 16:38
Member 149935186-Feb-22 16:38 
QuestionCan anyone send complete Ver. file? Pin
jin sic kim1-May-20 7:47
jin sic kim1-May-20 7:47 
QuestionUnable to built project in Visual Studio 2013 Pin
Member 1267965810-Aug-16 11:26
Member 1267965810-Aug-16 11:26 
AnswerRe: Unable to built project in Visual Studio 2013 Pin
delete[] VB15-Apr-20 17:17
delete[] VB15-Apr-20 17:17 
QuestionPlot Graphic Pin
Member 1180271430-Jun-15 1:16
Member 1180271430-Jun-15 1:16 
GeneralMy vote of 5 Pin
futurejo26-Apr-13 0:33
futurejo26-Apr-13 0:33 
QuestionHowTo? three plots in one single Graph? Pin
Alan-Lee Perkins21-Jan-13 22:58
Alan-Lee Perkins21-Jan-13 22:58 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey22-Feb-12 0:18
professionalManoj Kumar Choubey22-Feb-12 0:18 
QuestionLatest PGL version an Visual Studio versions Pin
Member 185281411-Nov-11 1:36
Member 185281411-Nov-11 1:36 
GeneralBug when compiling with VC .NET 2003 Pin
typke23-May-11 2:12
typke23-May-11 2:12 
Generalerror C2664 'hull::TDPHull<T>::Build' Pin
Burim Ramadani19-Dec-10 23:29
Burim Ramadani19-Dec-10 23:29 
GeneralRe: error C2664 'hull::TDPHull<T>::Build' Pin
wuziyuan_1114-Sep-14 17:02
wuziyuan_1114-Sep-14 17:02 
GeneralVisual C++ 2010 Pin
Burim Ramadani15-Dec-10 20:46
Burim Ramadani15-Dec-10 20:46 
GeneralRe: Visual C++ 2010 Pin
Wangrunqing17-Aug-14 16:10
Wangrunqing17-Aug-14 16:10 
GeneralRe: Visual C++ 2010 Pin
Member 1115562215-Oct-14 22:45
Member 1115562215-Oct-14 22:45 
GeneralRe: Visual C++ 2010 Pin
Member 1115562215-Oct-14 22:46
Member 1115562215-Oct-14 22:46 
GeneralRe: Visual C++ 2010 Pin
Member 1115562215-Oct-14 22:46
Member 1115562215-Oct-14 22:46 
GeneralBug scaling axis Pin
TomekG12-Apr-10 3:32
TomekG12-Apr-10 3:32 
GeneralI have compiled this project in VS 2005 PinPopular
broiler31526-Jan-10 22:24
broiler31526-Jan-10 22:24 
GeneralRe: I have compiled this project in VS 2005 Pin
david_liu11129-Jan-10 22:44
david_liu11129-Jan-10 22:44 
GeneralRe: I have compiled this project in VS 2005 Pin
typke25-Feb-11 2:10
typke25-Feb-11 2:10 
Generalopengl question Pin
noura238812-Nov-09 12:00
noura238812-Nov-09 12:00 
Generalopengl question Pin
noura238812-Nov-09 11:57
noura238812-Nov-09 11:57 
GeneralThe sample "export" functionality does not work Pin
transoft16-Apr-09 2:08
transoft16-Apr-09 2:08 
Generalprojectdistributor.net is dead.... Pin
dwawad12-Mar-09 19:29
dwawad12-Mar-09 19:29 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.