Printing MS Charts in ASP.net Application

How to print MSChart in ASP.Net. Can someone give me some ideas, I was looking for alot, but all of the available solutions are for Windows applications, not Web.

Looking for valuable solutions

Thanks in advance, Supriya

0
source share
1 answer

you can do like this .....

Note. This is an example of an example of how to print mschart in an asp.net web application, you can change this depending on your requirements .......

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.UI.DataVisualization.Charting;
using System.IO;
using System.Web.UI.WebControls;

namespace Avatar_Reports
{
    public class ExtendedChart : Chart
    {
        /// <summary>
        /// This property was added to keep a serialized version of the Chart at a given fixed time.
        /// </summary>
        public string SerializedString { get; set; }

        /// <summary>
        /// Updates the SerializedString property with current state of the object.
        /// </summary>
        public void UpdateSerializedString()
        {
            // Serialize the Chart into serialized Chart variable.
            using (MemoryStream writer = new MemoryStream())
            {
                this.Serializer.Content = SerializationContents.All;
                this.Serializer.Save(writer);
                this.SerializedString = GetStringFromStream(writer);
            }
        }

        /// <summary>
        /// Obtain an ExtendedChart with state and data as saved in the SerializedString property.
        /// </summary>
        /// <returns></returns>
        public ExtendedChart GetChartFromSerialized()
        {
            // Deserialize the Chart into a Chart variable.
            ExtendedChart clonedChart = new ExtendedChart();
            byte[] chartAsArray = System.Text.Encoding.Default.GetBytes(this.SerializedString);
            using (MemoryStream reader = new MemoryStream(chartAsArray))
            {
                clonedChart.Serializer.Content = SerializationContents.All;
                clonedChart.Serializer.Load(reader);
            }

            return clonedChart;
        }

        private string GetStringFromStream(Stream stream)
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

then you create a page with a kind of object like this:

.....

ExtendedChart ReportChart = new ExtendedChart();
Series Series1 = new Series("Series1");
ChartArea ChartArea1 = new ChartArea("ChartArea1");

....... and then you can use a method call like this in your menu

SerializeChartControls(outputTable);

Session["CtrlToPrint"] = outputTable;
                    Page.ClientScript.RegisterStartupScript(this.GetType(),
                        "Print", "<script language='javascript'>window.open('Print.aspx','PrintMe','height=1000px,width=900px,scrollbars=1');</script>");

......

private void SerializeChartControls(Control root)
{
    foreach (Control c in root.Controls)
    {
        if (c is Chart)
        {
            ExtendedChart theChart = c as ExtendedChart;
            theChart.UpdateSerializedString();
        }
        else if ((c != null) && (c.HasControls() == true))
        {
            SerializeChartControls(c);
        }
    }
}      

And you PRINT.ASPX could be like this:

.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.DataVisualization.Charting;

namespace Avatar_Reports
{
    public partial class Print : System.Web.UI.Page
    {
        private WebControl ctrlToPrint;
        private string footerText = String.Empty;

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ctrlToPrint = (WebControl)Session["CtrlToPrint"];
                DeserializeChartControls(ctrlToPrint);
                ExpandAllGridViews(ctrlToPrint);
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel titlePanel = new Panel();
                Label reportTitle = new Label();

                // reportTitle.Text = Session["ReportTitle"].ToString();
                titlePanel.CssClass = "ReportTitlePrinted";

                // titlePanel.Controls.Add(reportTitle);
                // Page.Form.Controls.Add(titlePanel);

                ctrlToPrint.Style.Add("text-align", "center");
                Page.Form.Controls.Add(ctrlToPrint);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.RegisterStartupScript("printPopup", "<script>window.print();</script>");
        }


        private void DeserializeChartControls(Control root)
        {
            foreach (Control c in root.Controls)
            {
                if (c is ExtendedChart)
                {
                    ExtendedChart theChart = c as ExtendedChart;
                    ExtendedChart newChart = theChart.GetChartFromSerialized();

                    // Replace the serialized version by the actual Chart.
                    WebControl chartParent = theChart.Parent as WebControl;
                    int chartIndex = chartParent.Controls.IndexOf(theChart);
                    chartParent.Controls.AddAt(chartIndex, newChart);
                    chartParent.Controls.Remove(theChart);
                }
                else if ((c != null) && (c.HasControls() == true))
                {
                    DeserializeChartControls(c);
                }
            }
        }

Hope this helps you ....

+1

All Articles