![]() |
This chapter explains how Charts are printed and which properties and methods are used to control the printing process.
Note:
Advanced. Printing methods are located in the CHART.PAS unit.
Topics in this section:
Windows and printers limitations
Dotted Pen styles and Pen Width
From FAQ - Practical issues when printing TeeCharts
When printing, you can specify which margins on the paper page should be left blank. The public PrintMargins:TRect property stores the desired paper margins expressed as percents of total page dimensions.
PrintMargins:=Rect(15,15,15,15); { default 15 % printing margins }
The TChart.ChartPrintRect:TRect function returns, after applying the printing margins, the space where the Chart component will be drawn expressed as logical canvas units (pixels or dots).
With PrintMargins you can define any area of any size inside the page.
Note:
When changing paper orientation, margins are recalculated.
Charts are printed in metafile (WMF in 16bit, EMF in 32bit) format.
Metafiles are scaleable vector formats, so a “wysiwyg” effect can be achieved if sending to the printer how a Chart looks on screen. However, you might want to exploit the printer’s bigger resolution capabilities versus screen displays.
The TChart.PrintResolution integer property controls how a Chart is scaled when sending it to the printer. By default PrintResolution is zero. Setting it to a negative value in the percentage range from zero to -100 makes the Chart proportionally bigger so there’s more space for Axis Labels. Smaller fonts are used as they will be clearer on paper than on screen.
Setting it to a positive value makes font sizes bigger.
Several methods exist to print a Chart component:
Print;
PrintRect(Const R:TRect);
PrintOrientation(AOrientation:TPrinterOrientation);
PrintPortrait;
PrintLandscape;
All the above methods will do the same, print a Chart on a new page and eject (form feed) the page:
Chart1.Print ;
Will print a Chart1 in the current printer’s paper orientation.
With PrintRect you can specify a custom Chart size and location. The default location and size is determined by ChartPrintRect function, which applies PrintMargins margins.
Delphi’s Printers unit contains the TPrinter object used for all Chart print methods.
More advanced printing control can be obtained using the following methods:
Procedure PrintPartial(Const PrinterRect:TRect);
Procedure PrintPartialCanvas( PrintCanvas:TCanvas;
Const PrinterRect:TRect);
Both will print a Chart component, BUT WILL NOT create a new printer page or eject it. You can print both your own text and graphics and Chart components on the same paper page.
PrintPartial may be used to send more than one Chart to the printed page method by using BeginDoc and EndDoc to wrap the printing via several consecutive PrintPartial commmands.
Windows and printer limitations
Metafiles are very good as they are small and fast and scaleable.
Some limitations occur when using metafiles, as described in Microsoft Knowledge Base ( www.mskb.com ). TeeChart inherits those limitations:
Clipping is stored in physical co-ordinates in metafiles. This means moving or scaling a clipped metafile will not scale or move the clipping region, giving undesirable results.
TeeChart does not draw partial points, so no clipping. Drawing on zoomed Charts will probably show partial points outside the Chart axis.
As metafiles can be scaled, circles will be converted to ellipses when scaling.
Rotated fonts can not be exactly aligned on non-proportionally scaled metafiles.
Dotted Pen styles and Pen Width
Non solid pens (dots, dashes), can be drawn as solid with scaled metafiles.
From FAQ - Practical issues when printing TeeCharts
This section explains how TeeChart components are printed, and describes how to improve printing.
The TeeChart library sends Charts to the printer using a Windows graphic format known as "metafile". This format has several advantages and disadvantages over using the bitmap format:
- It's much smaller, because graphic information is stored in vector instructions, instead of pixels. This results in a faster printing speed in most cases.
- The printer doesn't need to have more memory to print 'large' images (Charts).
- Due to be a sequence of instructions instead of a fixed array of pixels, a metafile can be resized ( or "stretched" ) without losing resolution, and with greater accuracy.
- New printers support metafile format natively, via software driver or via "gdi hardware".
With Windows 32-bit (Windows 98, NT, 2000 and XP), an extended metafile format is implemented. This is known as "enhanced metafile". Metafiles have the extension: "*.wmf" and enhanced metafiles: "*.emf".
The main goal of using metafiles in TeeChart, is to provide "wysiwyg" (what you see is what you get). That is, printed Charts should look as close as possible to how they look at screen.
To do this, TeeChart creates a metafile of the Chart image, and then sends this metafile to the printer. At this point, the Windows GDI module and the Windows Printer driver will take care of "painting" the Chart image on paper. This involves "stretching" or "resizing" the metafile, from screen coordinates to printer logical pixels.
Example:
A Chart on screen is at rectangle:
Left : 100
Top : 100
Right : 300
Bottom: 400
And it gets printed at this paper rectangle:
Left : 400
Top : 1000
Right : 1200
Bottom: 1500
In this example, the Chart should be rescaled both
in width and height:
screen Width = 300 - 100 = 200
paper Width = 1200 - 400 = 800
relation screen / paper = 800 / 200 = 4 <---
screen Height = 400 - 100 = 300
paper Height = 1500 - 1000 = 500
relation screen / paper = 500 / 300 = 1.666... <---
The problem is the relation between the horizontal and vertical increments is not the same:
4 <> 1.666...
This means the Chart will be expanded more in the horizontal dimension than in the vertical dimension. In this case, Windows increases font sizes and pen widths using the new dimensions.
Some printers can show problems when printing Charts *without* using proportional printer margins.
The attached code can be used generically in your applications to calculate the optimal printer
printer margins so the relation between Chart Width and Height on screen is the same as on paper.
Another approach is to resize the Chart onscreen to match the proportion of the printer paper:
Chart1.Height:=Round(1.0*Printer.PageHeight*Chart1.Width/Printer.PageWidth);
Chart1.Print;
Increasing resolution:
The metafile format is not aware of the "resolution" concept. This is means resolution information is not stored inside the metafile image. You can modify the Chart "resolution" *before* printing,
by setting this property:
Chart1.PrintResolution := -100 ;
Negative values (like -100 above), represent the porcentual increment in resolution. Zero means "wysiwyg".
More resolution is obtained by making all Chart fonts smaller and thinner lines. The metafile size is bigger when using more resolution. Increasing resolution can improve inaccuracy in buggy printer drivers calculations.
Printing non-solid lines:
In some printer / windows combinations, non-solid pen lines such as "dot" or "dash" will be printed as solid. This happens specially in some HP Laserjet printers. It seems the only workaround is to set the Chart Pen Width properties to Zero:
Chart1.LeftAxis.Grid.Width := 0;
Increasing resolution ( see above ) can make the printer to show non-solid lines.
Colors:
Many printers accept only a subset of the available Colors. This means setting a Chart color to a non-supported palette color may result in that color not being used by the printer, thus not drawing anything. In this situations, try with "well known" solid colors like "clRed, clBlue, clYellow, clGreen".
Some printers include a "Color Mapping" configuration dialog. (At Printer Properties dialog).
Printing directly:
Drawing a Chart directly onto a Printer GDI Handle or Canvas is also possible. The following code does it:
Uses Printers;
With Printer do
begin
BeginDoc;
try
Chart1.Draw( Canvas, Rect( 0,0,PageWidth,PageHeight ) );
finally
EndDoc;
end;
end;
You will see several problems printing directly:
-- The Chart background is gray color ( instead of white ).
-- Font sizes are extremely small.
-- There are many axis grid lines.
-- Lines are very thin.
One way to solve the above problems is using the metafile printing mode, described in this document. Another way (much more complicated) is to change all Font sizes and pen Width properties.
Printer driver settings:
Try to use always the latest "good" printer driver version. Try changing the Windows Printer driver resolution settings, and the spooler method (in Windows NT) to both "EMF" and "RAW" modes. "EMF" means all output is sent to the printer in metafile format.
See the help file for extended information and examples on the following properties and methods
Printing Properties:
Properties involved in Chart printing are:
Chart1.PrintMargins
The percentual space at the four sides of the paper page.
Chart1.PrintResolution
The relation between screen dimensions and paper dimensions.
Printing methods:
The TeeChart control has several methods designed for printing:
Methods that print *and* eject the printed page:
These are the default and more used TeeChart printing methods.
Chart1.Print
Uses default paper orientation and margins.
Chart1.PrintPortrait
Sets paper in Portrait and uses default margins.
Chart1.PrintLandscape
Sets paper in Landscape and uses default margins.
Chart1.PrintRect
Uses default paper orientation and the Rect parameter to position the Chart on the page.
Methods that do not eject the printed page:
These methods allow printing more than one Chart in the same paper page, or print other things and Chart components on the same page.
With these methods you need to call Printer.BeginDoc and EndDoc yourself. See Delphi TPrinter documentation and printing demos in TeeChart “features” project.
Chart1.PrintPartial
Draws a Chart to the Printer Canvas at the passed rectangle.
Chart1.PrintPartialCanvas
Draws a Chart to the passed Canvas at the passed rectangle.
Related methods and properties:
Not directly involved with printing, but useful for advanced printing:
Chart1.Draw
Draws a Chart to the passed Canvas, in screen "mode".
"Screen mode" means with gray background and without
using the metafile format.
This method draws directly to the Canvas.
Chart1.TeeCreateMetafile
Function that returns a metafile image of the Chart,
with the passed Rect coordinates.
Chart1.Metafiling
Boolean property indicating the Chart is now drawing
onto a metafile image.
Chart1.Printing
Boolean property indicating the Chart is now drawing
onto a Printer Canvas.
Information about printing and metafiles can be found at Microsoft Windows 32-bit SDK help file:"Win32.hlp"
Use the "Help Contents" (not the index) and scroll-down up to the Metafiles chapter.
See the Delphi's TMetafile and TMetafileCanvas objects in Delphi's helpfile.