Achieve WYSIWYG environment for the chart workspace

Applies to: Nevron Chart for .NET

How to achieve WYSIWYG environment for the chart workspace?

In order to achieve WYSIWYG you need to match the chart print margins to the chart dimensions on the screen.

When you print charts the chart area on the page is calculated as follows:

Left = margins.Left
Top = margins.Top
Right = pageSize.Width - margins.Left – margins.Right
Bottom = pageSize.Height  - margins.Top – margins.Bottom

Therefore if you want the chart to appear exactly as it appears on the screen you need to first take the control dimensions and calculate them against the printer resolution – the formula is:

[C#]
chartWidthInPixelsOnPage = chartControl.Width * printerResolution.DpiX / 96;
chartHeightInPixelsOnPage = chartControl.Height * printerResolution.DpiY / 96;

[VB.NET]
chartWidthInPixelsOnPage = chartControl.Width * printerResolution.DpiX / 96
chartHeightInPixelsOnPage = chartControl.Height * printerResolution.DpiY / 96

then you need to convert the pixels to a hundreds of inch which is the measurement unit accepted by .net for print margins. This is done with code similar to:

[C#]
chartWidhtInHundredsOfInch = chartWidthInPixelsOnPage / printerResolution.DpiX * 100;
chartHeightInHundredsOfInch = chartHeightInPixelsOnPage / printerResolution.DpiX * 100;

[VB.NET]
chartWidhtInHundredsOfInch = chartWidthInPixelsOnPage / printerResolution.DpiX * 100
chartHeightInHundredsOfInch = chartHeightInPixelsOnPage / printerResolution.DpiX * 100

Finally you need to obtain the currently selected page size and adjust the print margins in such a way that the area occupied by the chart matches the chart width and height expressed in hundreds of inch.

The above is pseudo code, the real code should look like:

[C#]
NPrintManager printManager = new NPrintManager(m_ChartControl.Document);
PageSettings pageSettings = printManager.PageSettings;
Margins margins = pageSettings.Margins;
PaperSize paperSize = pageSettings.PaperSize;
PrinterResolution printerResolution = pageSettings.PrinterResolution;
  
float chartWidthInPixelsOnPage = m_ChartControl.Width * printerResolution.X / 96;
float chartHeightInPixelsOnPage = m_ChartControl.Height * printerResolution.Y / 96;
  
int chartWidhtInHundredsOfInch = (int)(chartWidthInPixelsOnPage / printerResolution.X * 100 + 0.5f);
int chartHeightInHundredsOfInch = (int)(chartHeightInPixelsOnPage / printerResolution.Y * 100 + 0.5f);
  
margins.Right = paperSize.Width - chartWidhtInHundredsOfInch - margins.Left;
margins.Bottom = paperSize.Height - chartHeightInHundredsOfInch - margins.Top;
printManager.ShowPrintPreview();

[VB.NET]
Dim printManager As New NPrintManager(m_ChartControl.Document)
Dim pageSettings As PageSettings = printManager.PageSettings
Dim margins As Margins = pageSettings.Margins
Dim paperSize As PaperSize = pageSettings.PaperSize
Dim printerResolution As PrinterResolution = pageSettings.PrinterResolution
  
Dim chartWidthInPixelsOnPage As Single = m_ChartControl.Width * printerResolution.X / 96
Dim chartHeightInPixelsOnPage As Single = m_ChartControl.Height * printerResolution.Y / 96
  
Dim chartWidhtInHundredsOfInch As Integer = CInt(chartWidthInPixelsOnPage / printerResolution.X * 100 + 0.5F)
Dim chartHeightInHundredsOfInch As Integer = CInt(chartHeightInPixelsOnPage / printerResolution.Y * 100 + 0.5F)
  
margins.Right = paperSize.Width - chartWidhtInHundredsOfInch - margins.Left
margins.Bottom = paperSize.Height - chartHeightInHundredsOfInch - margins.Top
printManager.ShowPrintPreview()

Article ID: 61, Created On: 10/6/2010, Modified: 11/15/2010