5,548,129 members and growing! (18,259 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Charting Controls     Intermediate

NSChart Control

By Ivan Cachicatari

This control creates a chart in a dialog with Pie and Bar styles.
VC6, VC7, C++Windows, Win2K, WinXP, MFC, VS6, Visual Studio, Dev

Posted: 24 Jul 2004
Updated: 7 Aug 2004
Views: 63,510
Bookmarked: 43 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
26 votes for this Article.
Popularity: 6.41 Rating: 4.53 out of 5
1 vote, 3.8%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
5 votes, 19.2%
4
20 votes, 76.9%
5

Sample Image - nschart.gif

Sample Image - nschart.gif

Introduction

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():
    m_chart.SubclassDlgItem(IDC_CUSTOM1,this);
  4. Initialize the control colors, and add the elements in the InitDialog() function or any,
    m_chart.PrepareColors(CNSChartCtrl::GrayScale); //Colors
    
    m_chart.AddValue(10,"One");
    m_chart.AddValue(20,"Two");
    m_chart.AddValue(90,"Three");
    m_chart.AddValue(30,"Four");
    m_chart.AddValue(40,"Five");
    m_chart.AddValue(20,"Six")
  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.ResetColors();
m_chart.AddSolidBrush(0x000000FF);
m_chart.AddSolidBrush(RGB(255,0,255));
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(CYourDialog)

      ...
      afx_msg void OnChartSelectedItem(NMHDR* pNMHDR, LRESULT* pResult);
      ...
      //}}AFX_MSG

   ...
};
void CYourDialog::OnChartSelectedItem(NMHDR* pNMHDR, LRESULT* pResult)
{
   LPNMCHARTCTRL nmchart = (LPNMCHARTCTRL)pNMHDR;
   if(nmchart->iItem >= 0 )
   {
      //... have selected item

   }
   *pResult = FALSE;
}

Declare the notify message in MESSAGE_MAP block:

BEGIN_MESSAGE_MAP(CYourDialog, CDialog )
   //{{AFX_MSG_MAP(CYourDialog)

   ...
   ON_NOTIFY(NSCS_SELECTEDITEM, IDC_CUSTOM_CTRL, OnChartSelectedItem)
   ...
END_MESSAGE_MAP()

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

History

  • 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.

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

Ivan Cachicatari


Ivan Cachicatari is an student of Computer Sciense in Peru.

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

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



Occupation: Web Developer
Location: Peru Peru

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
Subject  Author Date 
Generalcompile errorsmemberEric-staal3:42 27 Feb '07  
GeneralHow to save the chart to an image file?memberLuo Pei'en20:57 24 Aug '06  
Generalwowmemberpeysy7:41 23 Aug '06  
GeneralHOw can i add scrolling functionality?memberAmit j Patil2:57 18 Jul '06  
GeneralGood but still a bug foundmemberxwq197522:06 20 Mar '06  
Generallicencemembercmaryus3:21 31 Oct '05  
GeneralScrolling support addedmemberaib163846:25 5 Sep '05  
GeneralRe: Scrolling support addedmembermyhawk6:01 22 Sep '07  
Generalanother chartsussAnonymous8:39 29 Aug '05  
Generalnice controlmemberyccheok22:06 2 Aug '05  
Generalhttp://wieler-forum.nl/car-donations--car-donation-virginia/susshard . local as Using click File Bar17:11 24 Jul '05  
GeneralSaving the graphsussAnonymous12:12 7 Jun '05  
GeneralResize fixsussPatrik Strömstedt22:34 28 Mar '05  
Generalvery good.memberMax Santos12:52 25 Mar '05  
GeneralRe: very good.membernavi20000:32 30 May '05  
GeneralNegative Valuesmembermattfaramir335:22 12 Mar '05  
GeneralCScrollViewsussFayezElFar5:59 1 Mar '05  
GeneralRe: CScrollViewmemberFayezElFar6:15 1 Mar '05  
Generalbug on this Ns chart !!memberchunghansu22:32 2 Nov '04  
GeneralHow do I create Bar Chart using MFC...?memberpubududilena18:04 1 Nov '04  
GeneralSmall modificationsmembernavi20002:48 1 Oct '04  
GeneralRe: Small modificationsmembermarkusha22:00 27 May '05  
GeneralBad scale on vertical axis !memberKochise22:57 25 Jul '04  
GeneralRe: Bad scale on vertical axis !memberIvan Cachicatari4:51 26 Jul '04  
GeneralRe: Bad scale on vertical axis !memberdropperkk15:56 1 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Aug 2004
Editor: Smitha Vijayan
Copyright 2004 by Ivan Cachicatari
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project