Printing Example for WinForms

Nevron NOV Chart for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms Chart examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Printing Example for WinForms Description

This example demonstrates how to configure the chart print layout and how to print the chart.


Printing Example

C# Printing Example Code for Nevron Chart for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Chart.Export;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NPrintingExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NChartViewWithCommandBars chartViewWithCommandBars = new NChartViewWithCommandBars();
			m_ChartView = chartViewWithCommandBars.View;
			m_ChartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

			// configure title
			m_ChartView.Surface.Titles[0].Text = "Printing Example";

			// configure chart
			NCartesianChart chart = (NCartesianChart)m_ChartView.Surface.Charts[0];

			chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear);

			// setup X axis
			NLinearScale xScale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryX].Scale;

			xScale.MajorGridLines.Visible = true;

			// setup Y axis
			NLinearScale yScale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryY].Scale;
			yScale.MajorGridLines.Visible = true;
			
			// add interlaced stripe
			NScaleStrip strip = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
			strip.Interlaced = true;
			yScale.Strips.Add(strip);

			// setup shape series
			NRangeSeries range = new NRangeSeries();
			chart.Series.Add(range);

			range.DataLabelStyle = new NDataLabelStyle(false);
			range.UseXValues = true;
			range.Fill = new NColorFill(NColor.DarkOrange);
			range.Stroke = new NStroke(NColor.DarkRed);

			// fill data
			double[] intervals = new double[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60 };
			double[] values = new double[] { 4180, 13687, 18618, 19634, 17981, 7190, 16369, 3212, 4122, 9200, 6461, 3435 };

			int count = Math.Min(intervals.Length, values.Length);
			double x = 0;

			for (int i = 0; i < count; i++)
			{
				double interval = intervals[i];
				double value = values[i];

				double x1 = x;
				double y1 = 0;

				x += interval;
				double x2 = x;
				double y2 = value / interval;

				range.DataPoints.Add(new NRangeDataPoint(x1, y1, x2, y2));
			}

			m_ChartView.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();

			// Create the radio button group
			{
				NStackPanel radioStack = new NStackPanel();
				NRadioButton metricUnitsRadioButton = new NRadioButton("Metric units");
				radioStack.Add(metricUnitsRadioButton);

				NRadioButton imperialUnitsRadioButton = new NRadioButton("Imperial units");
				radioStack.Add(imperialUnitsRadioButton);

				NRadioButtonGroup unitsRadioGroup = new NRadioButtonGroup(radioStack);
				unitsRadioGroup.SelectedIndex = 0;
				unitsRadioGroup.SelectedIndexChanged += OnUnitsRadioGroupSelectedIndexChanged;

				stack.Add(new NGroupBox("Print layout units:", unitsRadioGroup));
			}

			NButton printButton = new NButton("Print...");
			printButton.Click += OnPrintButtonClick;
			stack.Add(printButton);

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to configure the chart print layout and how to print the chart.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Printing a Chart", "https://www.nevron.com/docs/openvision/chart-printing.html"), ("Chart Overview", "https://www.nevron.com/docs/openvision/chart-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnUnitsRadioGroupSelectedIndexChanged(NValueChangeEventArgs arg) { bool metricUnits = (int)arg.NewValue == 0; NChartPrintLayout printLayout = m_ChartView.Surface.PrintLayout; if (metricUnits) { // Restore the default chart print layout property values printLayout.ClearLocalValue(NPrintLayout.PageWidthProperty); printLayout.ClearLocalValue(NPrintLayout.PageHeightProperty); printLayout.ClearLocalValue(NPrintLayout.LeftPageMarginsProperty); printLayout.ClearLocalValue(NPrintLayout.TopPageMarginsProperty); printLayout.ClearLocalValue(NPrintLayout.RightPageMarginsProperty); printLayout.ClearLocalValue(NPrintLayout.BottomPageMarginsProperty); } else { // Change the chart print layout to imperial units printLayout.PageSize = new NPaperSize(ENPaperKind.Letter); printLayout.LeftPageMargins = new NLength(0.4, NUnit.Inch); printLayout.TopPageMargins = new NLength(0.4, NUnit.Inch); printLayout.RightPageMargins = new NLength(0.4, NUnit.Inch); printLayout.BottomPageMargins = new NLength(0.4, NUnit.Inch); } } private void OnPrintButtonClick(NEventArgs arg) { NChartPrintExporter printExporter = new NChartPrintExporter(m_ChartView.Content); printExporter.ShowDialogAsync(m_ChartView.DisplayWindow, true); } #endregion #region Fields private NChartView m_ChartView; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Printing Example for WinForms Solution

VB.NET Printing Example Code for Nevron Chart for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Chart.Export
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms
Imports System

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NPrintingExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim chartViewWithCommandBars As NChartViewWithCommandBars = New NChartViewWithCommandBars()
            m_ChartView = chartViewWithCommandBars.View
            m_ChartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian)

            ' configure title
            m_ChartView.Surface.Titles(0).Text = "Printing Example"

            ' configure chart
            Dim chart = CType(m_ChartView.Surface.Charts(0), NCartesianChart)

            chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYLinear)

            ' setup X axis
            Dim xScale As NLinearScale = chart.Axes(ENCartesianAxis.PrimaryX).Scale

            xScale.MajorGridLines.Visible = True

            ' setup Y axis
            Dim yScale As NLinearScale = chart.Axes(ENCartesianAxis.PrimaryY).Scale
            yScale.MajorGridLines.Visible = True

            ' add interlaced stripe
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(NColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            yScale.Strips.Add(strip)

            ' setup shape series
            Dim range As NRangeSeries = New NRangeSeries()
            chart.Series.Add(range)

            range.DataLabelStyle = New NDataLabelStyle(False)
            range.UseXValues = True
            range.Fill = New NColorFill(NColor.DarkOrange)
            range.Stroke = New NStroke(NColor.DarkRed)

            ' fill data
            Dim intervals = New Double() {5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60}
            Dim values = New Double() {4180, 13687, 18618, 19634, 17981, 7190, 16369, 3212, 4122, 9200, 6461, 3435}

            Dim count = Math.Min(intervals.Length, values.Length)
            Dim x As Double = 0

            For i = 0 To count - 1
                Dim interval = intervals(i)
                Dim value = values(i)

                Dim x1 = x
                Dim y1 As Double = 0

                x += interval
                Dim x2 = x
                Dim y2 = value / interval

                range.DataPoints.Add(New NRangeDataPoint(x1, y1, x2, y2))
            Next

            m_ChartView.Document.StyleSheets.ApplyTheme(New NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series))

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()

            ' Create the radio button group
            If True Then
                Dim radioStack As NStackPanel = New NStackPanel()
                Dim metricUnitsRadioButton As NRadioButton = New NRadioButton("Metric units")
                radioStack.Add(metricUnitsRadioButton)

                Dim imperialUnitsRadioButton As NRadioButton = New NRadioButton("Imperial units")
                radioStack.Add(imperialUnitsRadioButton)

                Dim unitsRadioGroup As NRadioButtonGroup = New NRadioButtonGroup(radioStack)
                unitsRadioGroup.SelectedIndex = 0
                unitsRadioGroup.SelectedIndexChanged += AddressOf OnUnitsRadioGroupSelectedIndexChanged

                stack.Add(New NGroupBox("Print layout units:", unitsRadioGroup))
            End If

            Dim printButton As NButton = New NButton("Print...")
            printButton.Click += AddressOf OnPrintButtonClick
            stack.Add(printButton)

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to configure the chart print layout and how to print the chart.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Printing a Chart", "https://www.nevron.com/docs/openvision/chart-printing.html"), ("Chart Overview", "https://www.nevron.com/docs/openvision/chart-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnUnitsRadioGroupSelectedIndexChanged(arg As NValueChangeEventArgs) Dim metricUnits = CInt(arg.NewValue) = 0 Dim printLayout = m_ChartView.Surface.PrintLayout If metricUnits Then ' Restore the default chart print layout property values printLayout.ClearLocalValue(NPrintLayout.PageWidthProperty) printLayout.ClearLocalValue(NPrintLayout.PageHeightProperty) printLayout.ClearLocalValue(NPrintLayout.LeftPageMarginsProperty) printLayout.ClearLocalValue(NPrintLayout.TopPageMarginsProperty) printLayout.ClearLocalValue(NPrintLayout.RightPageMarginsProperty) printLayout.ClearLocalValue(NPrintLayout.BottomPageMarginsProperty) Else ' Change the chart print layout to imperial units printLayout.PageSize = New NPaperSize(ENPaperKind.Letter) printLayout.LeftPageMargins = New NLength(0.4, NUnit.Inch) printLayout.TopPageMargins = New NLength(0.4, NUnit.Inch) printLayout.RightPageMargins = New NLength(0.4, NUnit.Inch) printLayout.BottomPageMargins = New NLength(0.4, NUnit.Inch) End If End Sub Private Sub OnPrintButtonClick(arg As NEventArgs) Dim printExporter As NChartPrintExporter = New NChartPrintExporter(m_ChartView.Content) printExporter.ShowDialogAsync(m_ChartView.DisplayWindow, True) End Sub #End Region #Region "Fields" Private m_ChartView As NChartView #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Printing Example for WinForms Solution

Related Documentation Links