Profile Picture

Printing multiple charts on a page

Posted By Lee Harris 12 Years Ago
Author
Message
Lee Harris
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 7, Visits: 12
Thanks - actually, our structure for the charts is just what you've shown. The part I was missing was (Duh!) that I just need to call printDocument.Print() to kick off the actual printing... thx again.

Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009

Hi Lee,

Generally when you use custom printing you should not rely on the chart print manager - instead you need to create your own print document and print the charts there one by one. The following code snippet shows how to achieve this:

 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   // init the first chart
   {
    NCartesianChart chart = (NCartesianChart)nChartControl1.Charts[0];

    chart.BoundsMode = BoundsMode.Stretch;

    NBarSeries bar = new NBarSeries();
    bar.Values.Add(10);
    bar.Values.Add(20);
    bar.Values.Add(30);

    chart.Series.Add(bar);
   }

   // init the second chart
   {
    NCartesianChart chart = (NCartesianChart)nChartControl2.Charts[0];

    chart.BoundsMode = BoundsMode.Stretch;

    NBarSeries bar = new NBarSeries();
    bar.Values.Add(10);
    bar.Values.Add(20);
    bar.Values.Add(30);

    chart.Series.Add(bar);
   }
  }

  private PrintDocument CreatePrintDocument()
  {
   PrintDocument printDocument = new PrintDocument();
   printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);

   return printDocument;
  }

  private void OnPrintPage(object sender, PrintPageEventArgs ev)
  {
   RectangleF marginBounds = new RectangleF(ev.MarginBounds.Left, ev.MarginBounds.Top, ev.MarginBounds.Width, ev.MarginBounds.Height);

   // create header and footer
   string header = "Custom Header";
   string footer = "Custom Footer";

   Font font = new Font("Arial", 15);
   Brush brush = new SolidBrush(Color.Black);

   // measure them
            SizeF headerSize = ev.Graphics.MeasureString(header, font);
   SizeF footerSize = ev.Graphics.MeasureString(footer, font);

   // draw header
   RectangleF headerBounds = new RectangleF(marginBounds.Left, marginBounds.Top, marginBounds.Width, headerSize.Height);
   ev.Graphics.DrawString(header, font, brush, headerBounds);

   float chartSize = (marginBounds.Height - headerSize.Height - footerSize.Height) / 2;

   // draw chart
   NRectangleF firstChartBounds = new NRectangleF(marginBounds.Left, headerBounds.Bottom, marginBounds.Width, chartSize);
   using (NChartPrintView printView = new NChartPrintView(nChartControl1.PrintManager, nChartControl1.Document, ev.Graphics))
   {
    printView.Print(firstChartBounds);
   }

   NRectangleF secondChartBounds = new NRectangleF(marginBounds.Left, headerBounds.Bottom + chartSize, marginBounds.Width, chartSize);
   using (NChartPrintView printView = new NChartPrintView(nChartControl2.PrintManager, nChartControl1.Document, ev.Graphics))
   {
    printView.Print(secondChartBounds);
   }

   // draw footer
   RectangleF footerBounds = new RectangleF(marginBounds.Left, marginBounds.Bottom - footerSize.Height, marginBounds.Width, footerSize.Height);
   ev.Graphics.DrawString(footer, font, brush, footerBounds);

   // dispose objects
   font.Dispose();
   brush.Dispose();
  }

  private void ShowPrintPreviewButton_Click(object sender, EventArgs e)
  {
   try
   {
    PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
    printPreviewDialog.Document = CreatePrintDocument();
    printPreviewDialog.ShowDialog();
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
  }
 }
This will create e print page with two charts, header and footer. Let us know if you meet any problems or have any questions...

 



Best Regards,
Nevron Support Team



Lee Harris
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 7, Visits: 12
Okay - I got it to print a basic chart, using the following, where 'db.mCharts(0).mChartView' is a reference to a chart on the page:

printManager = New NPrintManager(db.mCharts(0).mChartView.Document)
printManager.PageSettings.Landscape = True
printManager.Print()

Now I'm trying to use the example in the PRM 'Chart for .NET > User's Guide > Printing > Custom Printing' to do custom printing, because I think I can then use that to put my multiple charts on the page.

However, the example isn't complete enough to be useable. In particular, the two lines:

PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);

seem to be floating in the air - how do I tie these into my stuff, so that the page event handler gets called so I can do my custom drawing? I assume that 'PrintDocument' is from System.Drawing.Printing and I Imports that so that it's defined, but I'm clearly missing something to tie it into the OnPrintPage sub.

Thanks,
Lee

Lee Harris
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 7, Visits: 12
Sorry to be a pain, but I can't find the custom printing example - would appreciate a link or more explicit directions. I see the online live demo for thin-client printing, but that's not what we're doing. We have a standalone Windows app from which we need to print the Nevron charts.

Thanks in advance...
Lee

Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)Supreme Being (4,435 reputation)

Group: Forum Members
Last Active: Last Week
Posts: 3,054, Visits: 4,009

Hi Lee,

The approach is Ok - you can take a look at the Printing\Custom Printing example. It shows how to create a print document and then tell the chart to print in this document - that way you can combine multiple charts inside a single page / print document. Let us know if you meet any problems...



Best Regards,
Nevron Support Team



Lee Harris
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: Forum Members
Last Active: 10 Years Ago
Posts: 7, Visits: 12
We have a VB.Net program that puts multiple NChartControls on a panel in a Windows Form, to show the user different graphic views of his data. We create a single chart for each of those NChartControls, and the user can move and resize each of those charts on the panel in the Form. Is there any sample code to show how to print all those controls on a single output page? Or are we using the wrong approach for having multiple different charts on a single page if we need to be able to print it out?

thanks!



Similar Topics


Reading This Topic