Click here to Skip to main content
12,701,854 members (34,907 online)
Click here to Skip to main content
Add your own
alternative version


83 bookmarked

NSChart Control

, 7 Aug 2004
Rate this:
Please Sign up or sign in to vote.
This control creates a chart in a dialog with Pie and Bar styles.

Sample Image - nschart.gif

Sample Image - nschart.gif


The CNSChartCtrl control class is derived from CWnd. The purpose is to create pie and bar charts in a dialog (also in a view) with editable fill colors and values, in runtime. Originally, the source code and project was written and compiled with Visual C++ 6.0 SP5 under Windows 98; it also was run in later versions.

It is an alternative to many tools available that perform similar tasks.

How to use

  1. To create a new NSChart control, use the MS Visual C++ dialog editor, insert a custom control on the dialog, and set "MFC_NSChartCtrl" as the Class name. The caption field is used to set the title of the chart.
  2. Create an instance of CNSChartCtrl in the dialog class:
    #include "NSChartCtrl.h"
    CNSChartCtrl m_chart
  3. Subclass the class member with the control in InitDialog():
  4. Initialize the control colors, and add the elements in the InitDialog() function or any,
    m_chart.PrepareColors(CNSChartCtrl::GrayScale); //Colors
  5. Run!

Customizing colors and patterns

The NSChart control uses an array of patterns to fill the sectors and bars. To add a new pattern or color, use the AddSolidBrush(COLORREF) or AddBrush(COLORREF).

m_chart.AddBrush(m_pBrush); // pre created brush

Using the Notify Code for Selection Items

The NSChartCtrl notifies you when you have clicked in one sector or bar (according to the case) with information about the selected value. The notify code is NSCS_SELECTEDITEM. For use, it needs to add the necessary code.

Declare the function that will be called from ON_NOTIFY in the AFX_MSG block.

class CYourDialog : public CDialog
      afx_msg void OnChartSelectedItem(NMHDR* pNMHDR, LRESULT* pResult);
void CYourDialog::OnChartSelectedItem(NMHDR* pNMHDR, LRESULT* pResult)
   if(nmchart->iItem >= 0 )
      //... have selected item
   *pResult = FALSE;

Declare the notify message in MESSAGE_MAP block:

BEGIN_MESSAGE_MAP(CYourDialog, CDialog )

If the control is created in a CView, change IDC_CUSTOM_CTRL with 0.


  • 25 Jun 2004 - 1.0.1
    • Initial release!
  • 02 Aug 2004 - 1.1.0
    • Fixed some drawing problems.
    • Added methods to modify and delete items.
    • Added Notify Messages with HitTest method.


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

Ivan Cachicatari
Web Developer
Peru Peru
Ivan Cachicatari is an student of Computer Sciense in Peru.

I'm webmaster of the latin developers comunity (in my free time)

Recently I have a personal blog (spanish), sonn in english. click on: Ivan Cachicatari's Blog

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Oscar Huanca4-Oct-12 6:27
memberOscar Huanca4-Oct-12 6:27 
Generalcompile errors Pin
Eric-staal27-Feb-07 3:42
memberEric-staal27-Feb-07 3:42 
QuestionHow to save the chart to an image file? Pin
Luo Pei'en24-Aug-06 20:57
memberLuo Pei'en24-Aug-06 20:57 
Generalwow Pin
peysy23-Aug-06 7:41
memberpeysy23-Aug-06 7:41 
QuestionHOw can i add scrolling functionality? Pin
Amit j Patil18-Jul-06 2:57
memberAmit j Patil18-Jul-06 2:57 
GeneralGood but still a bug found Pin
xwq197520-Mar-06 22:06
memberxwq197520-Mar-06 22:06 
Generallicence Pin
cmaryus31-Oct-05 3:21
membercmaryus31-Oct-05 3:21 
GeneralScrolling support added Pin
aib163845-Sep-05 6:25
memberaib163845-Sep-05 6:25 
GeneralRe: Scrolling support added Pin
myhawk22-Sep-07 6:01
membermyhawk22-Sep-07 6:01 
Generalanother chart Pin
Anonymous29-Aug-05 8:39
sussAnonymous29-Aug-05 8:39 
Generalnice control Pin
yccheok2-Aug-05 22:06
memberyccheok2-Aug-05 22:06 
General Pin
hard . local as Using click File Bar24-Jul-05 17:11
susshard . local as Using click File Bar24-Jul-05 17:11 
GeneralSaving the graph Pin
Anonymous7-Jun-05 12:12
sussAnonymous7-Jun-05 12:12 
GeneralResize fix Pin
Patrik Strömstedt28-Mar-05 22:34
sussPatrik Strömstedt28-Mar-05 22:34 
Generalvery good. Pin
Max Santos25-Mar-05 12:52
memberMax Santos25-Mar-05 12:52 
GeneralRe: very good. Pin
navi200030-May-05 0:32
membernavi200030-May-05 0:32 
GeneralNegative Values Pin
mattfaramir3312-Mar-05 5:22
membermattfaramir3312-Mar-05 5:22 
GeneralCScrollView Pin
FayezElFar1-Mar-05 5:59
sussFayezElFar1-Mar-05 5:59 
GeneralRe: CScrollView Pin
FayezElFar1-Mar-05 6:15
memberFayezElFar1-Mar-05 6:15 
Generalbug on this Ns chart !! Pin
chunghansu2-Nov-04 22:32
memberchunghansu2-Nov-04 22:32 
QuestionHow do I create Bar Chart using MFC...? Pin
pubududilena1-Nov-04 18:04
memberpubududilena1-Nov-04 18:04 
GeneralSmall modifications Pin
navi20001-Oct-04 2:48
membernavi20001-Oct-04 2:48 
GeneralRe: Small modifications Pin
markusha27-May-05 22:00
membermarkusha27-May-05 22:00 
GeneralBad scale on vertical axis ! Pin
Kochise25-Jul-04 22:57
memberKochise25-Jul-04 22:57 
GeneralRe: Bad scale on vertical axis ! Pin
Ivan Cachicatari26-Jul-04 4:51
memberIvan Cachicatari26-Jul-04 4:51 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 8 Aug 2004
Article Copyright 2004 by Ivan Cachicatari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid