using System; using System.Text; using System.Collections; using System.Web.UI.WebControls; using System.Web; using System.Collections.Generic; using System.Globalization; namespace FusionCharts.Charts { /// /// Contains static methods to render FusionCharts in the Page. /// public class Chart: ICloneable { private Hashtable __CONFIG__ = null; private static Hashtable __PARAMMAP__ = null; private string events = ""; //private string message = ""; /// /// User configurable chart parameter list /// public enum ChartParameter { chartType, chartId, chartWidth, chartHeight, dataFormat, dataSource, renderAt, bgColor, bgOpacity } /// /// List of supported data formats /// public enum DataFormat { json, jsonurl, xml, xmlurl, csv } #region constructor methods /// /// Chart constructor /// Chart configuration parameters can be supplyed to the constructor also. /// public Chart() { __INIT(); } /// /// Chart constructor /// /// The type of chart that you intend to plot public Chart(string chartType) { __INIT(); SetChartParameter("type", chartType); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. public Chart(string chartType, string chartId) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("id", chartId); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) public Chart(string chartType, string chartId, string chartWidth) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) public Chart(string chartType, string chartId, string chartWidth, string chartHeight) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl public Chart(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart public Chart(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart /// Background color of the chart container public Chart(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource, string bgColor) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); SetChartParameter("containerBackgroundColor", bgColor); } /// /// Chart constructor /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart /// Back-ground-color of the chart container /// Background opacity of the chart container public Chart(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource, string bgColor, string bgOpacity) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); SetChartParameter("containerBackgroundColor", bgColor); SetChartParameter("containerBackgroundOpacity", bgOpacity); } public Chart(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, TimeSeries timeSeries) { __INIT(); SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetChartParameter("dataSource", timeSeries); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); } #endregion #region RenderALL methods /// /// Generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// JavaScript + HTML code required to embed a chart private string RenderChartALL() { TimeSeries timeSeries = null; string dataSource = GetChartParameter("dataSource"); if (dataSource.Equals("FusionCharts.Charts.TimeSeries", StringComparison.OrdinalIgnoreCase)) { timeSeries = GetChartParameter("dataSource"); } string dataFormat = GetChartParameter("dataFormat"); string chartId = GetChartParameter("id"); string renderAt = GetChartParameter("renderAt"); StringBuilder builder = new StringBuilder(); builder.AppendFormat("" + Environment.NewLine, chartId); // if the user has provided renderAt then assume that the HTML container is already present in the page. if (renderAt.Trim().Length == 0) { renderAt = chartId + "_div"; // Now create the container div also. builder.AppendFormat("
" + Environment.NewLine, renderAt); builder.Append("Chart..." + Environment.NewLine); builder.Append("
" + Environment.NewLine); } string chartConfigJSON = fc_encodeJSON(GetConfigurationGroup("params"), true, timeSeries); builder.Append("" + Environment.NewLine); builder.AppendFormat("" + Environment.NewLine, chartId); return builder.ToString(); } #endregion #region Public Methods /// /// public method to attach event from client side /// /// /// public void AddEvent(string eventName, string funcName) { string eventHTML; string chartId = GetChartParameter("id"); eventHTML = string.Format("FusionCharts(\"{0}\").addEventListener(\"{1}\",{2});" + Environment.NewLine, chartId,eventName,funcName); events += eventHTML; } /// /// public method to add attributes for message customization /// /// /// public void AddMessage(string messageAttribute, string messageAttributeValue) { string messageHTML; messageHTML = string.Format("{0}:\"{1}\",", messageAttribute, messageAttributeValue); SetChartParameter("message", messageHTML); } /// /// Public method to clone an exiting FusionCharts instance /// To make the chartId unique, this function will add "_clone" as suffix in the clone chart's Id. /// public object Clone() { Chart ChartClone = new Chart(); ChartClone.__CONFIG__ = (Hashtable)this.__CONFIG__.Clone(); ChartClone.SetChartParameter("id", ((Hashtable)ChartClone.__CONFIG__["params"])["id"].ToString() + "_clone"); return ChartClone; } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// JavaScript + HTML code required to embed a chart public string Render() { return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// JavaScript + HTML code required to embed a chart public string Render(string chartType) { SetChartParameter("type", chartType); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId) { SetChartParameter("type", chartType); SetChartParameter("id", chartId); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth) { SetChartParameter("type", chartType); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth, string chartHeight) { SetChartParameter("type", chartType); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat) { SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource) { SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart /// Background color of the chart container /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource, string bgColor) { SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); SetChartParameter("containerBackgroundColor", bgColor); return RenderChartALL(); } /// /// Public method to generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// The type of chart that you intend to plot /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Data format. e.g. json, jsonurl, csv, xml, xmlurl /// Data for the chart /// Background color of the chart container /// Background opacity of the chart container /// JavaScript + HTML code required to embed a chart public string Render(string chartType, string chartId, string chartWidth, string chartHeight, string dataFormat, string dataSource, string bgColor, string bgOpacity) { SetChartParameter("type", chartType); SetChartParameter("dataFormat", dataFormat); SetData(dataSource); SetChartParameter("id", chartId); SetChartParameter("width", chartWidth); SetChartParameter("height", chartHeight); SetChartParameter("containerBackgroundColor", bgColor); SetChartParameter("containerBackgroundOpacity", bgOpacity); return RenderChartALL(); } /// /// SetChartParameter sets various configurations of a FusionCharts instance /// /// Name of chart parameter /// Value of chart parameter public void SetChartParameter(ChartParameter param, object value) { SetChartParameter(__PARAMMAP__[param.ToString()].ToString(), value); } /// /// GetChartParameter returns the value of a parameter of a FusionCharts instance /// /// Name of chart parameter /// String public string GetChartParameter(ChartParameter param) { return GetChartParameter(__PARAMMAP__[param.ToString()].ToString()); } /// /// This method to set the data for the chart /// /// Data for the chart public void SetData(string dataSource) { SetChartParameter("dataSource", dataSource); } /// /// This method to set the data for the chart /// /// Data for the chart /// Data format. e.g. json, jsonurl, csv, xml, xmlurl public void SetData(string dataSource, DataFormat format) { SetChartParameter("dataSource", dataSource); SetChartParameter("dataFormat", format.ToString()); } #endregion #region Helper Private Methods /// /// SetConfiguration sets various configurations of FusionCharts /// It takes configuration names as first parameter and its value a second parameter /// There are config groups which can contain common configuration names. All config names in all groups gets set with this value /// unless group is specified explicitly /// /// Name of configuration /// Value of configuration private void SetChartParameter(string setting, object value) { if (((Hashtable)__CONFIG__["params"]).ContainsKey(setting)) { if (setting.Equals("message", StringComparison.InvariantCultureIgnoreCase)) { ((Hashtable)__CONFIG__["params"])[setting] += value.ToString(); } else { ((Hashtable)__CONFIG__["params"])[setting] = value; } } } private void SetParamsMap() { if (__PARAMMAP__ == null) { __PARAMMAP__ = new Hashtable(StringComparer.InvariantCultureIgnoreCase); __PARAMMAP__["chartType"] = "type"; __PARAMMAP__["chartId"] = "id"; __PARAMMAP__["chartWidth"] = "width"; __PARAMMAP__["chartHeight"] = "height"; __PARAMMAP__["message"] = "message"; __PARAMMAP__["dataFormat"] = "dataFormat"; __PARAMMAP__["dataSource"] = "dataSource"; __PARAMMAP__["renderAt"] = "renderAt"; __PARAMMAP__["bgColor"] = "containerBackgroundColor"; __PARAMMAP__["bgOpacity"] = "containerBackgroundOpacity"; } } private void __INIT() { __CONFIG__ = new Hashtable(StringComparer.InvariantCultureIgnoreCase); Hashtable param = new Hashtable(StringComparer.InvariantCultureIgnoreCase); param["type"] = ""; param["width"] = ""; param["height"] = ""; param["message"] = ""; param["renderAt"] = ""; param["dataSource"] = ""; param["dataFormat"] = ""; param["id"] = Guid.NewGuid().ToString().Replace("-", "_"); param["containerBackgroundColor"] = ""; param["containerBackgroundOpacity"] = ""; __CONFIG__["params"] = param; param = null; SetParamsMap(); } /// /// Transform the meaning of boolean value in integer value /// /// true/false value to be transformed /// 1 if the value is true, 0 if the value is false private static int boolToNum(bool value) { return value ? 1 : 0; } private T GetChartParameter(string setting) { if (((Hashtable)__CONFIG__["params"]).ContainsKey(setting)) { object result = ((Hashtable)__CONFIG__["params"])[setting]; return (T)Convert.ChangeType(result, typeof(T)); } return (T)Convert.ChangeType(null, typeof(T)); ; } private string GetChartParameter(string setting) { if (((Hashtable)__CONFIG__["params"]).ContainsKey(setting)) { return ((Hashtable)__CONFIG__["params"])[setting].ToString(); } return null; } private string fc_encodeJSON(Hashtable json, bool enclosed, TimeSeries timeSeries) { string strjson = "", Key = "", Value = ""; foreach (DictionaryEntry ds in json) { if (ds.Value.ToString().Trim() != "") { Key = ds.Key.ToString(); Value = ds.Value.ToString(); // If this is not the dataSource then convert the value as JavaScript string if (Key.ToLower().Equals("datasource")) { if (timeSeries == null) {// Remove new line char from the dataSource Value.Replace("\n\r", ""); // detect if non-JSON format then wrap with quot '"' if (!(Value.StartsWith("{") && Value.EndsWith("}"))) { Value = "\"" + Value + "\""; } strjson = strjson + Environment.NewLine + "\"" + Key + "\" : " + Value + ", "; } else { strjson = strjson + Environment.NewLine + "\"" + Key + "\" : " + timeSeries.GetDataSource() + ", "; } } else if (Key.ToLower().Equals("message")) { strjson = strjson + Environment.NewLine + Value; } else { Value = "\"" + Value + "\""; strjson = strjson + Environment.NewLine + "\"" + Key + "\" : " + Value + ", "; } } else if (ds.Key.ToString().Equals("renderAt")) { strjson = strjson + Environment.NewLine + "\"renderAt\" : \"" + ((Hashtable)json)["id"].ToString() + "_div\", "; } } // remove ending comma if (strjson.EndsWith(",")) strjson = strjson.Remove(strjson.Length - 1); if (enclosed == true) { strjson = "{" + strjson + Environment.NewLine + "}"; } return strjson; } private Hashtable GetConfigurationGroup(string setting) { if (__CONFIG__.ContainsKey(setting)) { return (Hashtable)__CONFIG__[setting]; } return null; } #endregion } #region TimeSeries chart public class TimeSeries { private List> attributes = new List>(); private FusionTable fusionTableObject = null; public TimeSeries(FusionTable fusionTable) { this.fusionTableObject = fusionTable; } public void AddAttribute(string Key, string Value) { attributes.Add(new KeyValuePair(Key, Value)); } public string GetDataSource() { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair attrib in attributes) { sb.Append(string.Format("{0}:{1},{2}", attrib.Key, attrib.Value, Environment.NewLine)); } sb.Append(string.Format("{0}:{1}{2}", "data", "fusionTable", Environment.NewLine)); return "{" + Environment.NewLine + sb.ToString() + Environment.NewLine + "}"; } public string GetDataStore() { return fusionTableObject.GetDataTable(); } } public class FusionTable { public enum OrderBy { ASC = 0, DESC = 1 } public enum FilterType { Equals = 0, Greater = 1, GreaterEquals = 2, Less = 3, LessEquals = 4, Between = 5 } private StringBuilder stringBuilder = null; public FusionTable(string schema, string data) { this.stringBuilder = new StringBuilder(); stringBuilder.AppendLine("let schema = " + schema + ";"); stringBuilder.AppendLine("let data = " + data + ";"); stringBuilder.AppendLine("let fusionDataStore = new FusionCharts.DataStore();"); stringBuilder.AppendLine("let fusionTable = fusionDataStore.createDataTable(data, schema);"); } public void Select(params string[] columnName) { if (columnName.Length > 0) { string columns = string.Format("'{0}'", string.Join("', '", columnName)); stringBuilder.AppendLine("fusionTable = fusionTable.query(FusionCharts.DataStore.Operators.select([" + columns + "]));"); } } public void Sort(String columnName, OrderBy columnOrderBy) { string data = string.Format("{{column: '{0}', order: '{1}'}}", columnName, columnOrderBy.Equals(OrderBy.ASC) ? "asc" : "desc"); string sortedData = string.Format("sort([{0}])", data); stringBuilder.AppendLine("fusionTable = fusionTable.query(" + sortedData + ");"); } public string CreateFilter(FilterType filterType, String columnName, params object[] values) { string fx = filterType.ToString(); fx = char.ToLower(fx[0]) + fx.Substring(1); string filter = string.Empty; switch (filterType) { case FilterType.Equals: filter = string.Format("FusionCharts.DataStore.Operators.{0}('{1}', '{2}')", fx, columnName, values[0].ToString()); break; case FilterType.Between: if (values.Length > 1) { filter = string.Format("FusionCharts.DataStore.Operators.{0}('{1}', {2}, {3})", fx, columnName, values[0], values[1]); } break; default: filter = string.Format("FusionCharts.DataStore.Operators.{0}('{1}', {2})", fx, columnName, values[0]); break; } return filter; } public void ApplyFilter(String filter) { if (!string.IsNullOrEmpty(filter)) { stringBuilder.AppendLine("fusionTable = fusionTable.query(" + filter + ");"); } } public void ApplyFilterByCondition(String filter) { if (!string.IsNullOrEmpty(filter)) { stringBuilder.AppendLine("fusionTable = fusionTable.query(" + filter + ");"); } } public void Pipe(params string[] filters) { if (filters.Length > 0) { string columns = string.Format("'{0}'", string.Join(", ", filters)); stringBuilder.AppendLine("fusionTable = fusionTable.query(FusionCharts.DataStore.Operators.pipe(" + columns + "));"); } } public string GetDataTable() { return stringBuilder.ToString(); } } #endregion } namespace InfoSoftGlobal { /// /// Contains static methods to render FusionCharts in the Page. /// /// @version: v3.2.2.2 /// @date: 15 August 2012 /// /// public class FusionCharts { //private static Hashtable __CONFIG__ = new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()); private static Hashtable __CONFIG__ = new Hashtable(StringComparer.InvariantCultureIgnoreCase); private static bool __CONFIG__Initialized = false; #region RenderALL methods /// /// Generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// Back Ground Color /// Set Scale Mode /// Set SWF file Language /// JavaScript + HTML code required to embed a chart private static string RenderChartALL(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent, string bgColor, string scaleMode, string language) { __INIT(); // Creating a local copy of global Configuration. Hashtable __CONFIGCLONE__ = (Hashtable)__CONFIG__.Clone(); // string dataprovider_js_code; SetConfiguration(ref __CONFIGCLONE__, "debugMode", boolToNum(debugMode)); SetConfiguration(ref __CONFIGCLONE__, "registerWithJS", boolToNum(true)); // setup debug mode js parameter int debugMode_js_param = boolToNum(debugMode); // setup register with js js parameter int registerWithJS_js_param = boolToNum(true); string dataFormat = GetConfiguration(ref __CONFIGCLONE__, "dataFormat"); dataFormat = (dataFormat == "" ? "xml" + (dataStr == "" ? "url" : "") : dataFormat + (dataStr == "" ? "url" : "")); if (GetConfiguration(ref __CONFIGCLONE__, "renderAt") == "") SetConfiguration(ref __CONFIGCLONE__, "renderAt", chartId + "Div"); string wmode = GetConfiguration(ref __CONFIGCLONE__, "wMode"); if (wmode.Trim() == "" || wmode == null) { wmode = allowTransparent ? "transparent" : "opaque"; } SetConfiguration(ref __CONFIGCLONE__, "swfUrl", chartSWF); SetConfiguration(ref __CONFIGCLONE__, "dataFormat", dataFormat); SetConfiguration(ref __CONFIGCLONE__, "id", chartId); SetConfiguration(ref __CONFIGCLONE__, "width", chartWidth); SetConfiguration(ref __CONFIGCLONE__, "height", chartHeight); SetConfiguration(ref __CONFIGCLONE__, "wMode", wmode); SetConfiguration(ref __CONFIGCLONE__, "bgColor", bgColor); SetConfiguration(ref __CONFIGCLONE__, "scaleMode", scaleMode); SetConfiguration(ref __CONFIGCLONE__, "lang", language); string dataSource = (dataStr == "" ? dataUrl : dataStr.Replace("\n\r", "")); string dataSourceJSON = "\"dataSource\" : " + (dataFormat == "json" ? dataSource : "\"" + dataSource + "\""); string chartConfigJSON = "{" + fc_encodeJSON(GetConfigurationGroup(ref __CONFIGCLONE__, "params"), false) + "," + dataSourceJSON + "}"; 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); // Re-Initializing... __fc__initialize__(); __CONFIGCLONE__ = null; return builder.ToString(); } /// /// Renders the HTML code for the chart. This /// method does NOT embed the chart using JavaScript class. Instead, it uses /// direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll /// see the "Click to activate..." message on the chart. /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// Back Ground Color /// Set Scale Mode /// Set SWF file Language /// private static string RenderChartHTMLALL(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent, string bgColor, string scaleMode, string language) { __INIT(); // Creating a local copy of global Configuration. Hashtable __CONFIGCLONE__ = (Hashtable)__CONFIG__.Clone(); string wmode = GetConfiguration(ref __CONFIGCLONE__, "wMode"); if (wmode.Trim() == "" || wmode == null) { wmode = allowTransparent ? "transparent" : "opaque"; } SetConfiguration(ref __CONFIGCLONE__, "data", chartSWF); SetConfiguration(ref __CONFIGCLONE__, "movie", chartSWF); SetConfiguration(ref __CONFIGCLONE__, "dataURL", dataUrl); SetConfiguration(ref __CONFIGCLONE__, "dataXML", dataStr); SetConfiguration(ref __CONFIGCLONE__, "DOMId", chartId); SetConfiguration(ref __CONFIGCLONE__, "id", chartId); SetConfiguration(ref __CONFIGCLONE__, "width", chartWidth); SetConfiguration(ref __CONFIGCLONE__, "chartWidth", chartWidth); SetConfiguration(ref __CONFIGCLONE__, "height", chartHeight); SetConfiguration(ref __CONFIGCLONE__, "chartHeight", chartHeight); SetConfiguration(ref __CONFIGCLONE__, "debugMode", boolToNum(debugMode)); SetConfiguration(ref __CONFIGCLONE__, "registerWithJS", boolToNum(true)); SetConfiguration(ref __CONFIGCLONE__, "wMode", wmode); SetConfiguration(ref __CONFIGCLONE__, "bgColor", bgColor); SetConfiguration(ref __CONFIGCLONE__, "scaleMode", scaleMode); SetConfiguration(ref __CONFIGCLONE__, "lang", language); string strFlashVars = FC_Transform(GetConfigurationGroup(ref __CONFIGCLONE__, "fvars"), "&{key}={value}", true); SetConfiguration(ref __CONFIGCLONE__, "flashvars", strFlashVars); string strObjectNode = FC_Transform(GetConfigurationGroup(ref __CONFIGCLONE__, "object"), " {key}=\"{value}\"", true) ; string strObjectParamsNode = FC_Transform(GetConfigurationGroup(ref __CONFIGCLONE__, "objparams"), "\t\n", true); StringBuilder htmlcodes = new StringBuilder(); htmlcodes.AppendFormat("\n", chartId); htmlcodes.AppendFormat("\n" , strObjectNode); htmlcodes.Append(strObjectParamsNode + Environment.NewLine); htmlcodes.AppendFormat("\n\n{1}\n\n\n", strObjectNode, strObjectParamsNode); htmlcodes.AppendFormat("\n", chartId); // Re-Initializing... __fc__initialize__(); return htmlcodes.ToString(); } #endregion /// /// Generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// JavaScript + HTML code required to embed a chart [Obsolete("")] public static string RenderChart(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS) { return RenderChartALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, false, "", "noScale", "EN"); } /// /// Generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// JavaScript + HTML code required to embed a chart [Obsolete("")] public static string RenderChart(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent) { return RenderChartALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, allowTransparent, "", "noScale", "EN"); } /// /// Generate html code for rendering chart /// This function assumes that you've already included the FusionCharts JavaScript class in your page /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// Back Ground Color /// Set Scale Mode /// Set SWF file Language /// JavaScript + HTML code required to embed a chart [Obsolete("")] public static string RenderChart(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent, string bgColor, string scaleMode, string language) { return RenderChartALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, allowTransparent, bgColor, scaleMode, language); } /// /// Renders the HTML code for the chart. This /// method does NOT embed the chart using JavaScript class. Instead, it uses /// direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll /// see the "Click to activate..." message on the chart. /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// [Obsolete("")] public static string RenderChartHTML(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode) { return RenderChartHTMLALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, false, false, "", "noScale", "EN"); } /// /// Renders the HTML code for the chart. This /// method does NOT embed the chart using JavaScript class. Instead, it uses /// direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll /// see the "Click to activate..." message on the chart. /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// [Obsolete("")] public static string RenderChartHTML(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS) { return RenderChartHTMLALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, false, "", "noScale", "EN"); } /// /// Renders the HTML code for the chart. This /// method does NOT embed the chart using JavaScript class. Instead, it uses /// direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll /// see the "Click to activate..." message on the chart. /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// [Obsolete("")] public static string RenderChartHTML(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent) { return RenderChartHTMLALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, allowTransparent, "", "noScale", "EN"); } /// /// Renders the HTML code for the chart. This /// method does NOT embed the chart using JavaScript class. Instead, it uses /// direct HTML embedding. So, if you see the charts on IE 6 (or above), you'll /// see the "Click to activate..." message on the chart. /// /// SWF File Name (and Path) of the chart which you intend to plot /// If you intend to use dataURL method for this chart, pass the URL as this parameter. Else, set it to "" (in case of dataXML method) /// If you intend to use dataXML method for this chart, pass the XML data as this parameter. Else, set it to "" (in case of dataURL method) /// Id for the chart, using which it will be recognized in the HTML page. Each chart on the page needs to have a unique Id. /// Intended width for the chart (in pixels) /// Intended height for the chart (in pixels) /// Whether to start the chart in debug mode /// Whether to ask chart to register itself with JavaScript /// Whether allowTransparent chart (true / false) /// Back Ground Color /// Set Scale Mode /// Set SWF file Language /// [Obsolete("")] public static string RenderChartHTML(string chartSWF, string dataUrl, string dataStr, string chartId, string chartWidth, string chartHeight, bool debugMode, bool registerWithJS, bool allowTransparent, string bgColor, string scaleMode, string language) { return RenderChartHTMLALL(chartSWF, dataUrl, dataStr, chartId, chartWidth, chartHeight, debugMode, registerWithJS, allowTransparent, bgColor, scaleMode, language); } /// /// encodes the dataURL before it's served to FusionCharts /// If you have parameters in your dataURL, you'll necessarily need to encode it /// /// dataURL to be fed to chart /// Whether to add aditional string to URL to disable caching of data /// Encoded dataURL, ready to be consumed by FusionCharts [Obsolete("")] public static string EncodeDataURL(string dataUrl, bool noCacheStr) { string result = dataUrl; if (noCacheStr) { result += (dataUrl.IndexOf("?") != -1) ? "&" : "?"; //Replace : in time with _, as FusionCharts cannot handle : in URLs result += "FCCurrTime=" + DateTime.Now.ToString().Replace(":", "_"); } return System.Web.HttpUtility.UrlEncode(result); } /// /// Enables Print Manager for Mozilla browsers /// This function returns a small JavaScript snippet which can be added to ClientScript's RegisterClientScriptBlock method /// /// ClientScript.RegisterClientScriptBlock(Page.GetType(), "", FusionCharts.enableFCPrintManager()); /// String with the JavaScript code [Obsolete("")] public static string EnablePrintManager() { string strHTML = ""; return (strHTML); } /// /// Enables Print Manager for Mozilla browsers /// /// Current page reference [Obsolete("")] public static void EnablePrintManager(object CurrentPage) { System.Web.UI.Page HostPage; HostPage = (System.Web.UI.Page)CurrentPage; string strHTML = ""; HostPage.ClientScript.RegisterClientScriptBlock(HostPage.GetType(), "", strHTML); } private static void __INIT() { if (__CONFIG__Initialized == false) { __fc__initialize__(); __fc__initstatic__(); __CONFIG__Initialized = true; } } /// /// Sets the dataformat to be provided to charts (json/xml) /// /// Data format. Default is 'xml'. Other format is 'json' [Obsolete("")] public static void SetDataFormat(string format) { __INIT(); if (format.Trim().Length == 0) { format = "xml"; } // Stores the dataformat in global configuration store SetConfiguration("dataFormat", format); } /// /// Sets renderer type (flash/javascript) /// /// Name of the renderer. Default is 'flash'. Other possibility is 'javascript' [Obsolete("")] public static void SetRenderer(string renderer) { __INIT(); if (renderer.Trim().Length == 0) { renderer = "flash"; } // stores the renderer name in global configuration store SetConfiguration("renderer", renderer); } /// /// Explicitely sets window mode (window[detault]/transpatent/opaque) /// /// Name of the mode. Default is 'window'. Other possibilities are 'transparent'/'opaque' [Obsolete("")] public static void SetWindowMode(string mode) { __INIT(); SetConfiguration("wMode", mode); } /// /// FC_SetConfiguration sets various configurations of FusionCharts /// It takes configuration names as first parameter and its value a second parameter /// There are config groups which can contain common configuration names. All config names in all groups gets set with this value /// unless group is specified explicitly /// /// Name of configuration /// Value of configuration [Obsolete("")] public static void SetConfiguration(string setting, object value) { foreach (DictionaryEntry de in __CONFIG__) { if (((Hashtable)__CONFIG__[de.Key]).ContainsKey(setting)) { ((Hashtable)__CONFIG__[de.Key])[setting] = value; } } } /// /// FC_SetConfiguration sets various configurations of FusionCharts /// It takes configuration names as first parameter and its value a second parameter /// There are config groups which can contain common configuration names. All config names in all groups gets set with this value /// unless group is specified explicitly /// /// Name of configuration /// Value of configuration private static void SetConfiguration(ref Hashtable __TEMPHASH__, string setting, object value) { foreach (DictionaryEntry de in __TEMPHASH__) { if (((Hashtable)__TEMPHASH__[de.Key]).ContainsKey(setting)) { ((Hashtable)__TEMPHASH__[de.Key])[setting] = value; } } } #region Helper Private Methods private static string GetHTTP() { //Checks for protocol type. string isHTTPS = HttpContext.Current.Request.ServerVariables["HTTPS"]; //Checks browser type. bool isMSIE = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].Contains("MSIE"); //Protocol initially sets to http. string sHTTP = "http"; if (isHTTPS.ToLower() == "on") { sHTTP = "https"; } return sHTTP; } /// /// Transform the meaning of boolean value in integer value /// /// true/false value to be transformed /// 1 if the value is true, 0 if the value is false private static int boolToNum(bool value) { return value ? 1 : 0; } private static void SetCONSTANTConfiguration(string setting, object value) { ((Hashtable)__CONFIG__["constants"])[setting] = value; } private static string GetConfiguration(string setting) { foreach (DictionaryEntry de in __CONFIG__) { if (((Hashtable)__CONFIG__[de.Key]).ContainsKey(setting)) { return ((Hashtable)__CONFIG__[de.Key])[setting].ToString(); } } return null; } private static string GetConfiguration(ref Hashtable __TEMPHASH__, string setting) { foreach (DictionaryEntry de in __TEMPHASH__) { if (((Hashtable)__TEMPHASH__[de.Key]).ContainsKey(setting)) { return ((Hashtable)__TEMPHASH__[de.Key])[setting].ToString(); } } return null; } private static Hashtable GetConfigurationGroup(string setting) { if (__CONFIG__.ContainsKey(setting)) { return (Hashtable)__CONFIG__[setting]; } return null; } private static Hashtable GetConfigurationGroup(ref Hashtable __TEMPHASH__, string setting) { if (__TEMPHASH__.ContainsKey(setting)) { return (Hashtable)__TEMPHASH__[setting]; } return null; } private static string FC_Transform(Hashtable arr, string tFormat, bool ignoreBlankValues) { string converted = ""; string Key = "", Value = ""; foreach (DictionaryEntry ds in arr) { if (ignoreBlankValues == true && ds.Value.ToString().Trim() == "") continue; Key = ds.Key.ToString(); Value = ds.Value.ToString(); if (Key.ToLower().Equals("codebase")) { Value = Value.Replace("http", GetHTTP()); } string TFApplied = tFormat.Replace("{key}", Key); TFApplied = TFApplied.Replace("{value}", Value); converted = converted + TFApplied; } return converted; } private static string fc_encodeJSON(Hashtable json, bool enclosed) { string strjson = ""; if (enclosed == true) strjson = "{"; strjson = strjson + FC_Transform(json, "\"{key}\" : \"{value}\", ", true); strjson = strjson.Trim(); if (strjson.EndsWith(",")) strjson = strjson.Remove(strjson.Length - 1); return strjson; } private static void __fc__initstatic__() { Hashtable constant = new Hashtable(StringComparer.InvariantCultureIgnoreCase); constant["scriptbaseUri"] = ""; __CONFIG__["constants"] = constant; constant = null; } private static void __fc__initialize__() { __CONFIG__ = null; __CONFIG__ = new Hashtable(StringComparer.InvariantCultureIgnoreCase); Hashtable param = new Hashtable(StringComparer.InvariantCultureIgnoreCase); param["swfUrl"] = ""; param["width"] = ""; param["height"] = ""; param["renderAt"] = ""; param["renderer"] = ""; param["dataSource"] = ""; param["dataFormat"] = ""; param["id"] = ""; param["lang"] = ""; param["debugMode"] = ""; param["registerWithJS"] = ""; param["detectFlashVersion"] = ""; param["autoInstallRedirect"] = ""; param["wMode"] = ""; param["scaleMode"] = ""; param["menu"] = ""; param["bgColor"] = ""; param["quality"] = ""; __CONFIG__["params"] = param; Hashtable fvar = new Hashtable(StringComparer.InvariantCultureIgnoreCase); fvar["dataURL"] = ""; fvar["dataXML"] = ""; fvar["chartWidth"] = ""; fvar["chartHeight"] = ""; fvar["DOMId"] = ""; fvar["registerWithJS"] = "1"; fvar["debugMode"] = "0"; fvar["scaleMode"] = "noScale"; fvar["lang"] = "EN"; fvar["animation"] = "undefined"; __CONFIG__["fvars"] = fvar; Hashtable oBject = new Hashtable(StringComparer.InvariantCultureIgnoreCase); oBject["height"] = ""; oBject["width"] = ""; oBject["id"] = ""; oBject["lang"] = "EN"; oBject["class"] = "FusionCharts"; oBject["data"] = ""; __CONFIG__["object"] = oBject; Hashtable objparam = new Hashtable(StringComparer.InvariantCultureIgnoreCase); objparam["movie"] = "noScale"; objparam["scaleMode"] = "noScale"; objparam["scale"] = ""; objparam["wMode"] = ""; objparam["allowScriptAccess"] = "always"; objparam["quality"] = "best"; objparam["FlashVars"] = ""; objparam["bgColor"] = ""; objparam["swLiveConnect"] = ""; objparam["base"] = ""; objparam["align"] = ""; objparam["salign"] = ""; objparam["menu"] = ""; __CONFIG__["objparams"] = objparam; Hashtable embeds = new Hashtable(StringComparer.InvariantCultureIgnoreCase); embeds["height"] = ""; embeds["width"] = ""; embeds["id"] = ""; embeds["src"] = ""; embeds["flashvars"] = ""; embeds["name"] = ""; embeds["scaleMode"] = "noScale"; embeds["wMode"] = ""; embeds["bgColor"] = ""; embeds["quality"] = "best"; embeds["allowScriptAccess"] = "always"; embeds["type"] = "application/x-shockwave-flash"; embeds["pluginspage"] = "http://www.macromedia.com/go/getflashplayer"; embeds["swLiveConnect"] = ""; embeds["base"] = ""; embeds["align"] = ""; embeds["salign"] = ""; embeds["scale"] = ""; embeds["menu"] = ""; __CONFIG__["embed"] = embeds; param = null; fvar = null; oBject = null; objparam = null; embeds = null; } #endregion } }