using System; using System.Collections.Generic; using System.Text; using System.Web; using Microsoft.SharePoint; namespace FusionCharts.WebParts { /// /// Contains several very useful and required methods used to generate charts. /// Most the methods are statics. /// class Utils { public static string ChartsPath = "/_layouts/15/FusionChartsFree/"; private static string[] DefaultColors = { "AFD8F8", "F6BD0F", "8BBA00", "FF8E46", "008E8E", "D64646", "8E468E", "588526", "B3AA00", "008ED6", "9D080D", "A186BE" }; public static int DefaultHeight = 400; public static int DefaultWidth = 600; /// /// Determines if the current connection is SSL based or not /// /// True if SSL, False if not SSL public static bool IsSSL() { return HttpContext.Current.Request.IsSecureConnection; } /// /// Generate a random number that will become the HTML ID of the chart /// /// public static int GenerateRandomId() { Random random = new Random(); return random.Next(0, 10000); } /// /// Converts a boolean into a numeric value /// /// the boolean value to convert /// 0 or 1 (integer) private static int boolToNum(bool value) { return (value ? 1 : 0); } /// /// Generates the Fusion Chart based on script /// /// /// /// /// /// /// /// /// /// public static string RenderChart(string chartSWF, string strURL, string strXML, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS) { StringBuilder builder = new StringBuilder(); builder.AppendFormat("" + Environment.NewLine, chartId); builder.AppendFormat("
" + Environment.NewLine, chartId); builder.Append("Chart." + Environment.NewLine); builder.Append("
" + Environment.NewLine); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartId); return builder.ToString(); } /// /// Generate the fusion chart based on HTML /// /// /// /// /// /// /// /// /// public static string RenderChartHTML(ChartType chartType, string strURL, string strXML, string chartId, string chartWidth, string chartHeight, bool debugMode) { // This might be more simply done by using FusionCharts.js // which is included in the layouts directory of this solution, but not currently included or used // Example code from Fusion Charts web site: /* * * */ StringBuilder builder = new StringBuilder(); string str = string.Empty; // We convert the chart type to path string chartSWF = Utils.ConvertChartTypeToPath(chartType); // We get sample data in case no XML is specified if (strURL.Length == 0 && strXML.Length == 0) { strURL = Utils.GetXmlDataSample(chartType); } str = string.Format("&chartWidth={0}&chartHeight={1}&debugMode={2}" , chartWidth , chartHeight , boolToNum(debugMode) ); // Switch to use inline XML or external XML if (strXML.Length == 0) { // Possibly strURL should be html encoded? str += "&dataURL=" + strURL; } else { str += "&dataXML=" + strXML; } // We determine if we should is http or https prefix // (just for links to adobe's sites // (This is to avoid browser warnings from having insecure links on a secure page) string protocol = IsSSL() ? "https://" : "http://"; // We build the HTML and returns it builder.AppendFormat("" + Environment.NewLine, chartId); builder.AppendFormat("" + Environment.NewLine, chartWidth, chartHeight, chartId); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartSWF); builder.AppendFormat("" + Environment.NewLine, str); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, new object[] { chartSWF, str, chartWidth, chartHeight, chartId }); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartId); return builder.ToString(); } /// /// Generate the fusion chart based on HTML /// /// /// /// /// /// /// /// /// public static string RenderChartHTML(ChartTypeRestricted chartType, string strXML, string chartId, string chartWidth, string chartHeight, bool debugMode) { StringBuilder builder = new StringBuilder(); string str = string.Empty; // We convert the chart type to path string chartSWF = Utils.ConvertChartTypeToPath(chartType); // We use the XML given as param str = string.Format("&chartWidth={0}&chartHeight={1}&debugMode={2}&dataXML={3}", new object[] { chartWidth, chartHeight, boolToNum(debugMode), strXML }); // We determine if we should is http or https prefix string protocol = IsSSL() ? "https://" : "http://"; // We build the HTML and returns it builder.AppendFormat("" + Environment.NewLine, chartId); builder.AppendFormat("" + Environment.NewLine, chartWidth, chartHeight, chartId); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartSWF); builder.AppendFormat("" + Environment.NewLine, str); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, new object[] { chartSWF, str, chartWidth, chartHeight, chartId }); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartId); return builder.ToString(); } /// /// Converts a ChartType into a path that can be used with RenderChartHTML function /// /// The type of the chart /// public static string ConvertChartTypeToPath(ChartType chartType) { string path = SPContext.Current.Site.Url + ChartsPath + ConvertChartTypeToFilename(chartType); return path; } /// /// Converts a ChartType into a chart filename /// /// The type of the chart /// public static string ConvertChartTypeToFilename(ChartType chartType) { if (ChartType.FCF_Area2D == chartType) return "FCF_Area2D.swf"; if (ChartType.FCF_Bar2D == chartType) return "FCF_Bar2D.swf"; if (ChartType.FCF_Candlestick == chartType) return "FCF_Candlestick.swf"; if (ChartType.FCF_Column2D == chartType) return "FCF_Column2D.swf"; if (ChartType.FCF_Column3D == chartType) return "FCF_Column3D.swf"; if (ChartType.FCF_Doughnut2D == chartType) return "FCF_Doughnut2D.swf"; if (ChartType.FCF_Funnel == chartType) return "FCF_Funnel.swf"; if (ChartType.FCF_Gantt == chartType) return "FCF_Gantt.swf"; if (ChartType.FCF_Line == chartType) return "FCF_Line.swf"; if (ChartType.FCF_MSArea2D == chartType) return "FCF_MSArea2D.swf"; if (ChartType.FCF_MSBar2D == chartType) return "FCF_MSBar2D.swf"; if (ChartType.FCF_MSColumn2D == chartType) return "FCF_MSColumn2D.swf"; if (ChartType.FCF_MSColumn2DLineDY == chartType) return "FCF_MSColumn2DLineDY.swf"; if (ChartType.FCF_MSColumn3D == chartType) return "FCF_MSColumn3D.swf"; if (ChartType.FCF_MSColumn3DLineDY == chartType) return "FCF_MSColumn3DLineDY.swf"; if (ChartType.FCF_MSLine == chartType) return "FCF_MSLine.swf"; if (ChartType.FCF_Pie2D == chartType) return "FCF_Pie2D.swf"; if (ChartType.FCF_Pie3D == chartType) return "FCF_Pie3D.swf"; if (ChartType.FCF_StackedArea2D == chartType) return "FCF_StackedArea2D.swf"; if (ChartType.FCF_StackedBar2D == chartType) return "FCF_StackedBar2D.swf"; if (ChartType.FCF_StackedColumn2D == chartType) return "FCF_StackedColumn2D.swf"; if (ChartType.FCF_StackedColumn3D == chartType) return "FCF_StackedColumn3D.swf"; // Default charts in case there was no match return "FCF_Area2D.swf"; } /// /// Converts a ChartType into a path that can be used with RenderChartHTML function /// /// The type of the chart /// public static string ConvertChartTypeToPath(ChartTypeRestricted chartType) { string path = SPContext.Current.Site.Url + ChartsPath + ConvertChartTypeToFilename(chartType); return path; } /// /// Converts a ChartType into a chart filename /// /// The type of the chart /// public static string ConvertChartTypeToFilename(ChartTypeRestricted chartType) { if (ChartTypeRestricted.FCF_Area2D == chartType) return "FCF_Area2D.swf"; if (ChartTypeRestricted.FCF_Bar2D == chartType) return "FCF_Bar2D.swf"; if (ChartTypeRestricted.FCF_Column2D == chartType) return "FCF_Column2D.swf"; if (ChartTypeRestricted.FCF_Column3D == chartType) return "FCF_Column3D.swf"; if (ChartTypeRestricted.FCF_Doughnut2D == chartType) return "FCF_Doughnut2D.swf"; if (ChartTypeRestricted.FCF_Funnel == chartType) return "FCF_Funnel.swf"; if (ChartTypeRestricted.FCF_Line == chartType) return "FCF_Line.swf"; if (ChartTypeRestricted.FCF_Pie2D == chartType) return "FCF_Pie2D.swf"; if (ChartTypeRestricted.FCF_Pie3D == chartType) return "FCF_Pie3D.swf"; // Default charts in case there was no match return "FCF_Area2D.swf"; } /// /// Returns sample XML data to feed and generate charts /// /// public static string GetXmlDataSample(ChartType type) { if (type == ChartType.FCF_Column2D || type == ChartType.FCF_Column3D || type == ChartType.FCF_Pie2D || type == ChartType.FCF_Pie3D || type == ChartType.FCF_Line || type == ChartType.FCF_Bar2D || type == ChartType.FCF_Area2D || type == ChartType.FCF_Doughnut2D || type == ChartType.FCF_Funnel) return SPContext.Current.Site.Url + ChartsPath + "DataSampleSS1.xml"; if (type == ChartType.FCF_Candlestick) return SPContext.Current.Site.Url + ChartsPath + "DataSampleCandleStick1.xml"; if (type == ChartType.FCF_MSArea2D || type == ChartType.FCF_MSBar2D || type == ChartType.FCF_MSColumn2D || type == ChartType.FCF_MSColumn3D || type == ChartType.FCF_MSLine) return SPContext.Current.Site.Url + ChartsPath + "DataSampleMS1.xml"; if (type == ChartType.FCF_MSColumn2DLineDY || type == ChartType.FCF_MSColumn3DLineDY) return SPContext.Current.Site.Url + ChartsPath + "DataSampleCombi1.xml"; if (type == ChartType.FCF_StackedArea2D || type == ChartType.FCF_StackedBar2D || type == ChartType.FCF_StackedColumn2D || type == ChartType.FCF_StackedColumn3D) return SPContext.Current.Site.Url + ChartsPath + "DataSampleStacked1.xml"; if (type == ChartType.FCF_Gantt) return SPContext.Current.Site.Url + ChartsPath + "DataSampleGantt1.xml"; // By default we return the Simple Serie sample #1 return SPContext.Current.Site.Url + ChartsPath + "DataSampleSS1.xml"; } /// /// Returns the default colors in a string. Colors are separated by semi columns (;) /// /// public static string GetDefaultColors() { StringBuilder sb = new StringBuilder(); foreach (string color in DefaultColors) { sb.Append(color + ";"); } // We return the string and remove the last semi column return sb.ToString().Substring(0, sb.Length-1); } } /// /// All Type of charts available /// public enum ChartType { FCF_Area2D, FCF_Bar2D, FCF_Candlestick, FCF_Column2D, FCF_Column3D, FCF_Doughnut2D, FCF_Funnel, FCF_Gantt, FCF_Line, FCF_MSArea2D, FCF_MSBar2D, FCF_MSColumn2D, FCF_MSColumn2DLineDY, FCF_MSColumn3D, FCF_MSColumn3DLineDY, FCF_MSLine, FCF_Pie2D, FCF_Pie3D, FCF_StackedArea2D, FCF_StackedBar2D, FCF_StackedColumn2D, FCF_StackedColumn3D }; /// /// Restricted type of charts that can be used with SharePoint Lists /// public enum ChartTypeRestricted { FCF_Area2D, FCF_Bar2D, FCF_Column2D, FCF_Column3D, FCF_Doughnut2D, FCF_Funnel, FCF_Line, FCF_Pie2D, FCF_Pie3D } }