378 lines
18 KiB
C#
378 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Web;
|
|
using Microsoft.SharePoint;
|
|
|
|
namespace FusionCharts.WebParts
|
|
{
|
|
/// <summary>
|
|
/// Contains several very useful and required methods used to generate charts.
|
|
/// Most the methods are statics.
|
|
/// </summary>
|
|
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;
|
|
|
|
|
|
/// <summary>
|
|
/// Determines if the current connection is SSL based or not
|
|
/// </summary>
|
|
/// <returns>True if SSL, False if not SSL</returns>
|
|
public static bool IsSSL()
|
|
{
|
|
return HttpContext.Current.Request.IsSecureConnection;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate a random number that will become the HTML ID of the chart
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static int GenerateRandomId()
|
|
{
|
|
Random random = new Random();
|
|
return random.Next(0, 10000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a boolean into a numeric value
|
|
/// </summary>
|
|
/// <param name="value">the boolean value to convert</param>
|
|
/// <returns>0 or 1 (integer)</returns>
|
|
private static int boolToNum(bool value)
|
|
{
|
|
return (value ? 1 : 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates the Fusion Chart based on script
|
|
/// </summary>
|
|
/// <param name="chartSWF"></param>
|
|
/// <param name="strURL"></param>
|
|
/// <param name="strXML"></param>
|
|
/// <param name="chartId"></param>
|
|
/// <param name="chartWidth"></param>
|
|
/// <param name="chartHeight"></param>
|
|
/// <param name="debugMode"></param>
|
|
/// <param name="registerWithJS"></param>
|
|
/// <returns></returns>
|
|
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("<!-- START Script Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
builder.AppendFormat("<div id='{0}Div' align='center'>" + Environment.NewLine, chartId);
|
|
builder.Append("Chart." + Environment.NewLine);
|
|
builder.Append("</div>" + Environment.NewLine);
|
|
builder.Append("<script type=\"text/javascript\">" + Environment.NewLine);
|
|
builder.AppendFormat("var chart_{0} = new FusionCharts(\"{1}\", \"{0}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\");" + Environment.NewLine, new object[] { chartId, chartSWF, chartWidth, chartHeight, boolToNum(debugMode), boolToNum(registerWithJS) });
|
|
if (strXML.Length == 0)
|
|
{
|
|
builder.AppendFormat("chart_{0}.setDataURL(\"{1}\");" + Environment.NewLine, chartId, strURL);
|
|
}
|
|
else
|
|
{
|
|
builder.AppendFormat("chart_{0}.setDataXML(\"{1}\");" + Environment.NewLine, chartId, strXML);
|
|
}
|
|
builder.AppendFormat("chart_{0}.render(\"{1}Div\");" + Environment.NewLine, chartId, chartId);
|
|
builder.Append("</script>" + Environment.NewLine);
|
|
builder.AppendFormat("<!-- END Script Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate the fusion chart based on HTML
|
|
/// </summary>
|
|
/// <param name="chartType"></param>
|
|
/// <param name="strURL"></param>
|
|
/// <param name="strXML"></param>
|
|
/// <param name="chartId"></param>
|
|
/// <param name="chartWidth"></param>
|
|
/// <param name="chartHeight"></param>
|
|
/// <param name="debugMode"></param>
|
|
/// <returns></returns>
|
|
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:
|
|
/*
|
|
<script type="text/javascript">
|
|
//Instantiate the Chart
|
|
var chart_TopCustomers = new FusionCharts("FusionCharts/FCF_MSColumn3DLineDY.swf", "TopCustomers", "175", "160");
|
|
//Set the dataURL of the chart
|
|
chart_TopCustomers.setDataURL("TopCustomersData%2Easp%3Fyear%3D1996%26count%3D5%26thumb%3D1");
|
|
//Finally, render the chart.
|
|
chart_TopCustomers.render("TopCustomersDiv");
|
|
</script>
|
|
* * */
|
|
|
|
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("<!-- START Code Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
builder.AppendFormat("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"" + protocol + "fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"{0}\" height=\"{1}\" name=\"{2}\">" + Environment.NewLine, chartWidth, chartHeight, chartId);
|
|
builder.Append("<param name=\"allowScriptAccess\" value=\"always\" />" + Environment.NewLine);
|
|
builder.AppendFormat("<param name=\"movie\" value=\"{0}\"/>" + Environment.NewLine, chartSWF);
|
|
builder.AppendFormat("<param name=\"FlashVars\" value=\"{0}\" />" + Environment.NewLine, str);
|
|
builder.Append("<param name=\"quality\" value=\"high\" />" + Environment.NewLine);
|
|
builder.AppendFormat("<embed src=\"{0}\" FlashVars=\"{1}\" quality=\"high\" width=\"{2}\" height=\"{3}\" name=\"{4}\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"" + protocol + "www.macromedia.com/go/getflashplayer\" />" + Environment.NewLine, new object[] { chartSWF, str, chartWidth, chartHeight, chartId });
|
|
builder.Append("</object>" + Environment.NewLine);
|
|
builder.AppendFormat("<!-- END Code Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate the fusion chart based on HTML
|
|
/// </summary>
|
|
/// <param name="chartType"></param>
|
|
/// <param name="strURL"></param>
|
|
/// <param name="strXML"></param>
|
|
/// <param name="chartId"></param>
|
|
/// <param name="chartWidth"></param>
|
|
/// <param name="chartHeight"></param>
|
|
/// <param name="debugMode"></param>
|
|
/// <returns></returns>
|
|
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("<!-- START Code Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
builder.AppendFormat("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\""+ protocol + "fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"{0}\" height=\"{1}\" name=\"{2}\">" + Environment.NewLine, chartWidth, chartHeight, chartId);
|
|
builder.Append("<param name=\"allowScriptAccess\" value=\"always\" />" + Environment.NewLine);
|
|
builder.AppendFormat("<param name=\"movie\" value=\"{0}\"/>" + Environment.NewLine, chartSWF);
|
|
builder.AppendFormat("<param name=\"FlashVars\" value=\"{0}\" />" + Environment.NewLine, str);
|
|
builder.Append("<param name=\"quality\" value=\"high\" />" + Environment.NewLine);
|
|
builder.AppendFormat("<embed src=\"{0}\" FlashVars=\"{1}\" quality=\"high\" width=\"{2}\" height=\"{3}\" name=\"{4}\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"" + protocol + "www.macromedia.com/go/getflashplayer\" />" + Environment.NewLine, new object[] { chartSWF, str, chartWidth, chartHeight, chartId });
|
|
builder.Append("</object>" + Environment.NewLine);
|
|
builder.AppendFormat("<!-- END Code Block for Chart {0} -->" + Environment.NewLine, chartId);
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a ChartType into a path that can be used with RenderChartHTML function
|
|
/// </summary>
|
|
/// <param name="chartType">The type of the chart</param>
|
|
/// <returns></returns>
|
|
public static string ConvertChartTypeToPath(ChartType chartType)
|
|
{
|
|
string path = SPContext.Current.Site.Url + ChartsPath + ConvertChartTypeToFilename(chartType);
|
|
return path;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a ChartType into a chart filename
|
|
/// </summary>
|
|
/// <param name="chartType">The type of the chart</param>
|
|
/// <returns></returns>
|
|
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";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a ChartType into a path that can be used with RenderChartHTML function
|
|
/// </summary>
|
|
/// <param name="chartType">The type of the chart</param>
|
|
/// <returns></returns>
|
|
public static string ConvertChartTypeToPath(ChartTypeRestricted chartType)
|
|
{
|
|
string path = SPContext.Current.Site.Url + ChartsPath + ConvertChartTypeToFilename(chartType);
|
|
return path;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a ChartType into a chart filename
|
|
/// </summary>
|
|
/// <param name="chartType">The type of the chart</param>
|
|
/// <returns></returns>
|
|
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";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns sample XML data to feed and generate charts
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the default colors in a string. Colors are separated by semi columns (;)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// All Type of charts available
|
|
/// </summary>
|
|
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
|
|
};
|
|
|
|
/// <summary>
|
|
/// Restricted type of charts that can be used with SharePoint Lists
|
|
/// </summary>
|
|
public enum ChartTypeRestricted
|
|
{
|
|
FCF_Area2D,
|
|
FCF_Bar2D,
|
|
FCF_Column2D,
|
|
FCF_Column3D,
|
|
FCF_Doughnut2D,
|
|
FCF_Funnel,
|
|
FCF_Line,
|
|
FCF_Pie2D,
|
|
FCF_Pie3D
|
|
}
|
|
}
|