using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Xml; using System.Web.UI.WebControls; namespace FusionCharts.WebParts { /// /// Displays a Fusion Chart Chart with data coming from /// an XML file (or content) /// [Guid("e884ae9e-3b17-435e-b002-854e90ac94b5")] public class ChartFromXml : WebPart { #region PrivateMembers private bool _error = false; private string _chartId = null; // null by default (important) private ChartType _chartType; private string _chartXmlUrl = ""; // empty by default private string _chartXmlContent = ""; // empty by default private int _chartHeight = Utils.DefaultHeight; // default height private int _chartWidth = Utils.DefaultWidth; // default width private bool _xmlExport = false; // display/hide the custom button to export chart to xml file private string _myBuildVersion = "ChartFromXml: 2013-02-13-1200"; private string _myTrace = ""; #endregion #region CustomProperties /// /// The type of chart to display /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart Type")] [WebDescription("The type of chart that will be displayed")] public ChartType ChartType { get { return _chartType; } set { _chartType = value; } } /// /// The height of the chart that will be displayed /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart Height")] [WebDescription("The height of the chart to display")] public int ChartHeight { get { return _chartHeight; } set { _chartHeight = value; } } /// /// The width of the chart that will be displayed /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart Width")] [WebDescription("The width of the chart to display")] public int ChartWidth { get { return _chartWidth; } set { _chartWidth = value; } } /// /// The XML Url that contains the data to generate the chart /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart Xml Url")] [WebDescription("The Url of the Xml file that contains the data to generate the chart")] public string ChartXmlUrl { get { return _chartXmlUrl; } set { _chartXmlUrl = value; } } /// /// The XML Content (inline) that contains data to generate the chart /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart Xml Content")] [WebDescription("The Xml content that contains the data to generate the chart")] public string ChartXmlContent { get { return _chartXmlContent; } set { _chartXmlContent = value; } } /// /// The HTML Id of the chart /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Chart HTML ID")] [WebDescription("The Html ID that will identify the chart")] public string ChartID { get { if (_chartId == null) { // We generate a ID for the chart _chartId = "chart" + Utils.GenerateRandomId().ToString(); } return _chartId; } set { _chartId = value; } } /// /// does we have to display custom button to export to xml ? /// [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDisplayName("Enable XML export")] [WebDescription("Display a custom button to export the chart to a xml file")] public bool XMLExport { get { return _xmlExport; } set { _xmlExport = value; } } public enum DiagnosticSetting { None, IncludeVersion, Trace }; private DiagnosticSetting _myDiagnostics = DiagnosticSetting.None; [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Fusion Charts")] [WebDescription("Level of diagnostic reporting (into comment in script in page source)")] [Microsoft.SharePoint.WebPartPages.WebPartStorage(Microsoft.SharePoint.WebPartPages.Storage.Shared)] public DiagnosticSetting Diagnostics { get { return _myDiagnostics; } set { _myDiagnostics = value; } } #endregion #region Constructors /// /// Constructor /// public ChartFromXml() { this.ExportMode = WebPartExportMode.All; } #endregion #region Methods /// /// Create all your controls here for rendering. /// Try to avoid using the RenderWebPart() method. /// protected override void CreateChildControls() { if (!_error) { try { // wire up PreRender for diagnostics this.PreRender += new EventHandler(OnPreRender); base.CreateChildControls(); // We create literal control that will display the HTML needed to chart string chartHtml = Utils.RenderChartHTML(ChartType, ChartXmlUrl, ChartXmlContent, ChartID.ToString(), ChartWidth.ToString(), ChartHeight.ToString(), false); LiteralControl lc = new LiteralControl(chartHtml); this.Controls.Add(lc); // display (or not, depending of the user choice option) the button "export to xml" if (this._xmlExport) { Button btnXML = new Button(); btnXML.Text = "Export chart to XML file"; btnXML.Click += new System.EventHandler(this.ExportXmlButtonClick); btnXML.Style.Add("font-size", "10"); btnXML.Style.Add("margin", "10"); this.Controls.Add(btnXML); } } catch (Exception ex) { HandleException(ex); } } } /// /// Clear all child controls and add an error message for display. /// /// private void HandleException(Exception ex) { this._error = true; this.Controls.Clear(); this.Controls.Add(new LiteralControl("ERROR: " + ex.Message)); } /// /// After Load and PostBack events, PreRender occurs /// /// /// private void OnPreRender(object obj, EventArgs args) { // Add trace info after all controls are loaded // and all postback events have fired, so _myTrace // has all trace info OutputTrace(); } /// /// When the user click on the export to XML button /// so we get to xml, and make it download /// protected void ExportXmlButtonClick(object sender, EventArgs e) { Context.Response.Clear(); Context.Response.ContentType = "application/octet-stream"; Context.Response.AppendHeader("Content-Disposition", "attachment; filename=XMLExport.xml"); Context.Response.AddHeader("Content-Length", ChartXmlContent.Length.ToString()); Context.Response.Write(ChartXmlContent); Context.Response.End(); } #endregion #region Developer Diagnostics /// /// Write version info (and accumulated trace info if any) into HTML comment in page /// private void OutputTrace() { if (_myDiagnostics != DiagnosticSetting.None) { string tracemsg = ""; if (_myDiagnostics == DiagnosticSetting.Trace) { tracemsg = "\nTrace:\n"; if (_myTrace.Length == 0) { tracemsg += "No Recorded Trace Info"; } else { tracemsg += System.Web.HttpUtility.HtmlEncode(_myTrace); } tracemsg += "\n"; } System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); string message = "\n"; this.Controls.Add(new LiteralControl(message)); } } #endregion } }