Skip to main content
Email Password   helpLost your password?

Sample Image - printchart.jpg

Introduction

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 myself, right?

Not really. Surprisingly, it only took 5 hours of my Saturday afternoon.

Getting Started

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 view base.

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 to 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 CFormView.

In the header of your view class (i.e. PrintMyChart.h) add this private member:

    protected:
        HBITMAP m_hbitmap;

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 shown below.

BOOL CPrintMyChartView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// make preparation to print


	// order chart to copy itself onto clipboard

	m_chart.EditCopy();

	// make sure the item in clipboard is a bitmap

	if(IsClipboardFormatAvailable(CF_BITMAP))
	{
		if(OpenClipboard())
		{
			m_hbitmap = (HBITMAP)::GetClipboardData(CF_BITMAP);
			CloseClipboard();
		}
	}

	return DoPreparePrinting(pInfo);
}

void CPrintMyChartView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
	// Create a text for the title

	CString sTitleHeader=_T("My First Chart");

	// Get the page size and boundaries

	CRect rectPage = pInfo->m_rectDraw;

	TEXTMETRIC tm;
	CFont font;
	CSize textSize;
	int cyChar;

	// Create the font we will be using

	font.CreatePointFont(240, "Arial", pDC);
	CFont *pOldFont=pDC->SelectObject(&font);

	//Set Margin

	rectPage.top+=rectPage.bottom/48;
	rectPage.bottom-=rectPage.bottom/48;
	rectPage.left+=200;
	rectPage.right-=200;

	// Get Text size in order to center

	pDC->GetTextMetrics(&tm);
	textSize=pDC->GetTextExtent(sTitleHeader);
	cyChar = tm.tmHeight;


	// Draw Text (centered)

	pDC->TextOut(((rectPage.right+rectPage.left)/2)-(textSize.cx/2),
	                rectPage.top, sTitleHeader);
	rectPage.top += cyChar + cyChar / 4;

	// Draw header line divider

	pDC->MoveTo(rectPage.left, rectPage.top);
	pDC->LineTo(rectPage.right, rectPage.top);

	// Go to next line

	rectPage.top += cyChar / 4;

	if(m_hbitmap)
	{
		BITMAP bm;
		::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
		CSize chartSize(bm.bmWidth, bm.bmHeight);

		CDC dcMemory,dcScreen;
		dcScreen.Attach(::GetDC(NULL));

		// create "from" device context and select the

		// loaded bitmap into it

		dcMemory.CreateCompatibleDC(&dcScreen);
		dcMemory.SelectObject(m_hbitmap);

		// Print at 85% size within left/right margin 

		CSize printSize;
		printSize.cx=(int)(rectPage.right*.85);
		printSize.cy=printSize.cx/chartSize.cx*chartSize.cy;

		// Print chart centered

		pDC->StretchBlt( ((rectPage.right+rectPage.left)/2)-
		         (printSize.cx/2), 
				  rectPage.top,
				  printSize.cx,
				  printSize.cy,
				  &dcMemory, 
				  0, 0, chartSize.cx, chartSize.cy, SRCCOPY);
		dcMemory.DeleteDC();
	}

	// Revert and Destroy

	pDC->SelectObject(pOldFont);
	font.DeleteObject();

}

Alrighty then. You're done! Now go and design that killer application report you've been dying to do! :)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalconvert any file into tiff formatt Pin
mohan.iton
20:43 2 Nov '09  
QuestionIs it possible to copy Chart Pin
chris175
9:22 27 Jun '06  
AnswerRe: Is it possible to copy Chart Pin
Ananth.tm
10:54 13 Oct '06  
QuestionGood Works But ,a Bug Exitting in it.... Pin
mrxwh
20:24 2 Mar '06  
AnswerRe: Good Works But ,a Bug Exitting in it.... Pin
mrxwh
21:46 7 Mar '06  
GeneralMuch better way to print MSChart Control Pin
Victor Ricklefs
4:35 9 Jan '06  
QuestionRe: Much better way to print MSChart Control Pin
chris175
9:05 27 Jun '06  
Generalmschart label Pin
edge2
3:57 2 Jan '06  
AnswerRe: mschart label Pin
chris175
9:08 27 Jun '06  
Generaltext over or above the columns Pin
marcdev
5:55 2 Aug '05  
GeneralRe: text over or above the columns Pin
wengjay
23:19 27 Oct '05  
GeneralOne Other Thing Pin
jlatham
11:15 10 Apr '05  
GeneralClipboard Use? Pin
jlatham
11:10 10 Apr '05  
GeneralHELP, IT WORKS, BUT CANT PRINT Pin
Julihc
19:57 5 Apr '04  
GeneralRe: HELP, IT WORKS, BUT CANT PRINT Pin
mingjwan
17:45 29 Apr '04  
GeneralRe: HELP, IT WORKS, BUT CANT PRINT Pin
Qadddd
7:31 6 May '04  
Generalhow to work with legend in mschar Pin
Anonymous
5:37 2 Mar '04  
GeneralRe: how to work with legend in mschar Pin
Ananth.tm
10:56 13 Oct '06  
GeneralIs there any such graph which can be printed and drawn to Scale Pin
Kapoor Vikrant
9:13 3 Oct '03  
GeneralDIB Pin
smirre
4:00 24 Apr '03  
GeneralRe: DIB Pin
tree_house
6:37 1 May '03  
GeneralRe: DIB Pin
smirre
22:08 5 May '03  
Generalhow to do the print when chart is in a dialog? Pin
Qadddd
14:21 4 Dec '02  
GeneralRe: how to do the print when chart is in a dialog? Pin
Victor Porter
16:08 4 Dec '02  
GeneralRe: how to do the print when chart is in a dialog? Pin
Qadddd
0:10 5 Dec '02  


Last Updated 4 Aug 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009