Phresco.FusionCharts.ListToXmlConverter.GenerateXml C# (CSharp) Method

GenerateXml() public method

Generate the nice XML output for fusion charts
public GenerateXml ( string chartTitle, string xValue, string xTitle, string yValue, string yTitle, GroupAction groupAction, string colors ) : string
chartTitle string Title of the chart
xValue string Column name to use as x value
xTitle string Title to give to x
yValue string Column name to use as y value
yTitle string Title to give to y
groupAction GroupAction Action to perform (sum, count, ...)
colors string List of colors to use (separated by ;)
return string
        public string GenerateXml(string chartTitle, string xValue, string xTitle, string yValue, string yTitle, GroupAction groupAction, string colors)
        {
            // We first convert color (string) into a nice array
            string[] separator = {";"};
            string[] colorsArray = colors.Split(separator, StringSplitOptions.RemoveEmptyEntries);

            StringBuilder sb = new StringBuilder();
            // We open the graph tag with a few parameters
            sb.AppendLine("<graph caption='" + chartTitle + "' xAxisName='" + xTitle + "' yAxisName='" + yTitle + "'>");

            // We prepare the data in a nice hashtable
            Plots plots = PrepareData(xValue, yValue, groupAction);
            for (int i = 0; i < plots.Count(); i++)
            {
                // We try to compute the color
                string color = "";
                try
                {
                    color = colorsArray[i % (colorsArray.Length)]; // computes to color to use
                }
                catch { } // if we fail we skip the error, default color will be used

                // We add a line to the XML
                sb.Append("<set name='" + plots.GetX(i) + "' value='" + plots.GetY(i).ToString() + "'");
                if (color != "") sb.Append(" color='" + color + "'");
                sb.AppendLine(" />");
            }

            // We close the graph tag opened previously
            sb.AppendLine("</graph>");

            // We finally return the xml data
            return sb.ToString();
        }

Usage Example

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();

                    // Your code here...

                    // We first check all settings
                    CheckSettings();

                    // We transform the list into XML Input
                    ListToXmlConverter conv            = new ListToXmlConverter(_listName, _viewName);
                    string             chartXmlContent = conv.GenerateXml(ChartTitle, XValue, XTitle, YValue, YTitle, Action, Colors);

                    // We generate the graph
                    string         chartHtml = Utils.RenderChartHTML(ChartType, chartXmlContent, ChartID.ToString(), ChartWidth.ToString(), ChartHeight.ToString(), false);
                    LiteralControl lc        = new LiteralControl(chartHtml);
                    this.Controls.Add(lc);

                    // We add a cariage return to display correctly export buttons
                    if (this._excelExport || this._xmlExport)
                    {
                        this.Controls.Add(new LiteralControl("<br />"));
                    }

                    // display (or not, depending of the user choice option) the button "export to excel"
                    if (this._excelExport)
                    {
                        Button btnExcel = new Button();
                        btnExcel.Text   = "Export chart to Excel";
                        btnExcel.Click += new System.EventHandler(this.ExportExcelButtonClick);
                        btnExcel.Style.Add("font-size", "10");
                        btnExcel.Style.Add("margin", "10");
                        this.Controls.Add(btnExcel);
                    }

                    // 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);
                }
            }
        }
All Usage Examples Of Phresco.FusionCharts.ListToXmlConverter::GenerateXml