Printing chart in c#

0 votes
asked Oct 27, 2010 by mark

I am able to print a chart from my c# project using:

chart1.Printing.PrintDocument.DocumentName = "Graph of data";

But is it possible to add a title to this? I was hoping the document name would achieve this, but apparently not!

2 Answers

0 votes
answered Oct 27, 2010 by kyndigs

Here is a workaround solution to your problem, if you place the ChartingControl inside a Panel control on the Windows Form. You can then print the panel, inside the panel you can add the document heading as a label and whatever other stuff you want to add.

Firstly from the toolbox add a PrintDocument control and call it MyPrintDocument

Then add a Panel control and put your chart inside it.

Make sure you have imported the System.Drawing namespace, then you can print the panel like this.

    Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);
    panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));

    PrintDialog MyPrintDialog = new PrintDialog();

    if (MyPrintDialog.ShowDialog() == DialogResult.OK)
    {
        System.Drawing.Printing.PrinterSettings values;
        values = MyPrintDialog.PrinterSettings;
        MyPrintDialog.Document = MyPrintDocument;
        MyPrintDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
        MyPrintDocument.Print();
    }

    MyPrintDocument.Dispose();

This code converts the panel into a Bitmap and then prints that Bitmap.

You could condense this into a function like:

public void PrintPanel(Panel MyPanel)
{
   // Add code from above in here, changing panel1 to MyPanel...
}
0 votes
answered Oct 26, 2014 by jim-balkwill

You can print whatever you want directly to the page and then invoke the chart PrintPaint(). Note that if you don't switch the PageUnit to Pixels that the chart scaling gets confused.

void PrintChart(object sender, PrintPageEventArgs ev)
    {
        using (var f = new System.Drawing.Font("Arial", 10))
        {
            var size = ev.Graphics.MeasureString(Text, f);
            ev.Graphics.DrawString("Whatever text you want", f, Brushes.Black, ev.PageBounds.X + (ev.PageBounds.Width - size.Width) / 2, ev.PageBounds.Y);
        }

        //Note, the chart printing code wants to print in pixels.
        Rectangle marginBounds = ev.MarginBounds;
        if (ev.Graphics.PageUnit != GraphicsUnit.Pixel)
        {
            ev.Graphics.PageUnit = GraphicsUnit.Pixel;
            marginBounds.X = (int)(marginBounds.X * (ev.Graphics.DpiX / 100f));
            marginBounds.Y = (int)(marginBounds.Y * (ev.Graphics.DpiY / 100f));
            marginBounds.Width = (int)(marginBounds.Width * (ev.Graphics.DpiX / 100f));
            marginBounds.Height = (int)(marginBounds.Height * (ev.Graphics.DpiY / 100f));
        }

        chart1.Printing.PrintPaint(ev.Graphics, marginBounds);
    }

This menu handler opens a PrintDialog(). If you don't want a dialog you can just call pd.Print().

  private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var pd = new System.Drawing.Printing.PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintChart);

        PrintDialog pdi = new PrintDialog();
        pdi.Document = pd;
        if (pdi.ShowDialog() == DialogResult.OK)
            pdi.Document.Print();
    }
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...