在ASP.net应用程序中打印MS图表 [英] Printing of MS Chart in ASP.net Application

查看:71
本文介绍了在ASP.net应用程序中打印MS图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.Net中打印MSChart. 有人可以给我一些想法吗,我搜寻了很多,但是所有可用的解决方案都是针对Windows应用程序而不是针对Web.

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,

预先感谢, Supriya

Thanks in advance, Supriya

推荐答案

您可以这样做.....

you can do like this.....

注意:这是如何在asp.net Web应用程序中打印mschart的示例示例,您可以根据自己的要求进行更改.......

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);
        }
    }
}      

您的PRINT.ASPX可能是这样的:

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....

这篇关于在ASP.net应用程序中打印MS图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆