Reputation: 127
How to print MSChart in ASP.Net. Can anybody give me some idea,I searched alot but all solutions available are for windows application not for Web.
Looking for the valuable solutions of yours,
Thanks in advance, Supriya
Upvotes: 1
Views: 1493
Reputation: 17701
you can do like this.....
Note :This is sample example how to print mschart in asp.net web application, you can change this depends upon 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 you page with this kind of object like this:
.....
ExtendedChart ReportChart = new ExtendedChart();
Series Series1 = new Series("Series1");
ChartArea ChartArea1 = new ChartArea("ChartArea1");
....... and then you can use in your menu a call to a method like this
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 can 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);
}
}
}
I hope it will helps you....
Upvotes: 1