The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
<!-- Article image -->
<!-- Add the rest of your HTML here -->
Just recently, I was given a task: "Create a report with a chart." Sounds
simple, right? That was what I though too, at first. My first reaction was to
use a 3rd party plug-in of some sort, such as Crystal Report. "Sorry, we don't
have budget to use a third party item which requires royalty or additional fee."
All righty then, how about exporting the data to an excel template? "Sorry,
exporting a 10x30000 spread sheet on a Pentium 2-300 would take about an hour
before printing." How about I code the report onto the printer's device context?
"Great! We'll need that in 3 days. And make sure it has some flexibility for us
do some custom configuration and filtering"
*Gasp* Okay, this doesn't leave me with too many choices, so I looked into
the ActiveX component, MsChart. I've seen postings of people talking about
MsChart in VC++ forums, but they were mostly questions without answers. This
lead me to believe that it might not be a very good component. Furthermore, I've
never been a very big far for ActiveX. Frankly, I find ActiveX pretty ugly; Just
another nightmare for C++ coders like other COM objects. Usually, I am pretty
good at avoiding ActiveX when I can, but this time I'm cornered. Searching my
favorite VC++ forums came up either empty or unanswered questions when I entered
"print + MsChart". Oh boy, this is going to take me forever to figure out
Not really. Surprisingly, it only took 5 hours of my Saturday afternoon.
I have to admit, MsChart isn't as difficult to use as I though. Actually, it
is fairly easy to incorporate it into your project or report.
First thing you must do is add the MsChart Component into your project. For
this, you can refer to JL Colson's article Passing an Array of
Values to the Visual C++ MSChart OCX. It's a really nice article to get you
started with adding the chart control. Either that, or you can can try to
compile the sample source code. However, you might have to run the .reg file
enclosed in the zip file so that everything that must be in the registry is
there. The only thing you must make sure is that MsChart must reside in a form,
so it's easier to create a SDI project with a
CFormView as your
How to Print MsChart
Alright. Now let's get down to the code. First thing you would need to do is
go into your resource's toolbar and change the printer icon from
ID_FILE_PRINT_PREVIEW. This makes it
easier for us to access the print preview without always having to go to the
File Menu. Next, use the ClassWizard and add
OnPrint to your
In the header of your view class (i.e.
PrintMyChart.h) add this
Also be sure to use the ClassWizard to add a member variable
m_chart to your MsChart component.
The actual logic to print the chart is actually quite simple. First, you tell
the chart component to copy itself into the clipboard using
m_chart.EditCopy(). Once it is in memory, create a DC and past the
clipboard bitmap onto it and then transfer it to the printer's DC
pDC). Now, go to your view class and edit the two functions as
BOOL CPrintMyChartView::OnPreparePrinting(CPrintInfo* pInfo)
m_hbitmap = (HBITMAP)::GetClipboardData(CF_BITMAP);
void CPrintMyChartView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
CString sTitleHeader=_T("My First Chart");
CRect rectPage = pInfo->m_rectDraw;
font.CreatePointFont(240, "Arial", pDC);
cyChar = tm.tmHeight;
rectPage.top += cyChar + cyChar / 4;
rectPage.top += cyChar / 4;
::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
CSize chartSize(bm.bmWidth, bm.bmHeight);
0, 0, chartSize.cx, chartSize.cy, SRCCOPY);
Alrighty then. You're done! Now go and design that killer application report
you've been dying to do!