Taylohtio/FusionCharts/SP2010/Backup/WebPartCode/ChartXmlWriter.cs

83 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace FusionCharts.WebParts
{
class ChartXmlWriter
{
private Plots _plots = null;
private string _chartTitle = "";
private string _xTitle = "";
private string _yTitle = "";
public int YDecimalPrecision = 0;
public bool YNumberScale = false;
public string YNumberPrefix = "";
private string _colors = "";
public ChartXmlWriter(Plots plots, string ChartTitle, string XTitle, string YTitle, string Colors)
{
_plots = plots;
_chartTitle = ChartTitle;
_xTitle = XTitle;
_yTitle = YTitle;
_colors = Colors;
}
/// <summary>
/// Generate the nice XML output for fusion charts
/// </summary>
public string WriteXml()
{
// 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");
// should we html encode these?
sb.AppendFormat(" caption='{0}'", _chartTitle);
sb.AppendFormat(" xAxisName='{0}'", _xTitle);
sb.AppendFormat(" yAxisName='{0}'", _yTitle);
sb.AppendFormat(" decimalPrecision='{0}'", this.YDecimalPrecision);
if (YNumberScale)
{
sb.Append(" formatNumberScale='1'");
}
sb.AppendFormat(" numberPrefix='{0}'", this.YNumberPrefix);
sb.AppendLine(">");
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
string x = _plots.GetX(i);
// We always have exactly one series
List<double> yvalues = _plots.GetY(i);
double y0 = yvalues[0];
if (!Double.IsNaN(y0))
{
// We add a line to the XML
sb.Append("<set name='" + x + "' value='" + y0.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();
}
}
}