[7849] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text;
|
---|
| 4 | using System.ComponentModel;
|
---|
| 5 | using System.Web;
|
---|
| 6 | using System.Web.Caching;
|
---|
| 7 | using System.Web.UI;
|
---|
| 8 |
|
---|
| 9 | namespace OpenFlashChart
|
---|
| 10 | {
|
---|
| 11 | [Designer(typeof(ChartControlDesigner)), Description("Chart control for open flash chart"), ToolboxData("<{0}:OpenFlashChartControl runat=\"server\" ></{0}:OpenFlashChartControl>")]
|
---|
| 12 | public class OpenFlashChartControl : Control
|
---|
| 13 | {
|
---|
| 14 | private string width;
|
---|
| 15 | private string height;
|
---|
| 16 | private string externalSWFfile;
|
---|
| 17 | private string externalSWFObjectFile;
|
---|
| 18 | private string loadingmsg;
|
---|
| 19 | private OpenFlashChart chart;
|
---|
| 20 | private string chart_json;
|
---|
| 21 | private bool _enableCache;
|
---|
| 22 | /// <summary>
|
---|
| 23 | /// Used to hold internal chart
|
---|
| 24 | /// </summary>
|
---|
| 25 | public OpenFlashChart Chart
|
---|
| 26 | {
|
---|
| 27 | get
|
---|
| 28 | {
|
---|
| 29 | return chart;
|
---|
| 30 | }
|
---|
| 31 | set
|
---|
| 32 | {
|
---|
| 33 | chart = value;
|
---|
| 34 | chart_json = value.ToString();
|
---|
| 35 | ViewState["chart_json"] = chart_json;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | private string ChartJson
|
---|
| 40 | {
|
---|
| 41 | get
|
---|
| 42 | {
|
---|
| 43 | if (ViewState["chart_json"] != null)
|
---|
| 44 | return ViewState["chart_json"].ToString();
|
---|
| 45 | return chart_json;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | private string datafile;
|
---|
| 49 |
|
---|
| 50 | [DefaultValue("600px")]
|
---|
| 51 | [Category("Appearance")]
|
---|
| 52 | [PersistenceMode(PersistenceMode.Attribute)]
|
---|
| 53 | public string Width
|
---|
| 54 | {
|
---|
| 55 | get
|
---|
| 56 | {
|
---|
| 57 | width = "600px";
|
---|
| 58 | if (this.ViewState["width"] != null)
|
---|
| 59 | {
|
---|
| 60 | width = this.ViewState["width"].ToString();
|
---|
| 61 | }
|
---|
| 62 | return width;
|
---|
| 63 | }
|
---|
| 64 | set
|
---|
| 65 | {
|
---|
| 66 | if (!value.EndsWith("%") && !value.EndsWith("px"))
|
---|
| 67 | value = value + "px";
|
---|
| 68 | this.ViewState["width"] = value;
|
---|
| 69 | width = value;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | [DefaultValue("300px")]
|
---|
| 73 | [Category("Appearance")]
|
---|
| 74 | [PersistenceMode(PersistenceMode.Attribute)]
|
---|
| 75 | public string Height
|
---|
| 76 | {
|
---|
| 77 | get
|
---|
| 78 | {
|
---|
| 79 | height = "300px";
|
---|
| 80 | if (this.ViewState["height"] != null)
|
---|
| 81 | {
|
---|
| 82 | height = this.ViewState["height"].ToString();
|
---|
| 83 | }
|
---|
| 84 | return height;
|
---|
| 85 | }
|
---|
| 86 | set
|
---|
| 87 | {
|
---|
| 88 | if (!value.EndsWith("%") && !value.EndsWith("px"))
|
---|
| 89 | value = value + "px";
|
---|
| 90 | this.ViewState["height"] = value;
|
---|
| 91 | height = value;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | [Category("Appearance")]
|
---|
| 96 | [PersistenceMode(PersistenceMode.Attribute)]
|
---|
| 97 | public string ExternalSWFfile
|
---|
| 98 | {
|
---|
| 99 | get
|
---|
| 100 | {
|
---|
| 101 | if (this.ViewState["externalswffile"] != null)
|
---|
| 102 | {
|
---|
| 103 | externalSWFfile = this.ViewState["externalswffile"].ToString();
|
---|
| 104 | }
|
---|
| 105 | if (!string.IsNullOrEmpty(externalSWFfile))
|
---|
| 106 | {
|
---|
| 107 | if (externalSWFfile.StartsWith("~"))
|
---|
| 108 | {
|
---|
| 109 | externalSWFfile = this.ResolveUrl(externalSWFfile);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | return externalSWFfile;
|
---|
| 113 | }
|
---|
| 114 | set
|
---|
| 115 | {
|
---|
| 116 | this.ViewState["externalswffile"] = value.Trim();
|
---|
| 117 | externalSWFfile = value.Trim();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | [Category("Appearance")]
|
---|
| 121 | [PersistenceMode(PersistenceMode.Attribute)]
|
---|
| 122 | public string ExternalSWFObjectFile
|
---|
| 123 | {
|
---|
| 124 | get
|
---|
| 125 | {
|
---|
| 126 | if (this.ViewState["externalswfobjectfile"] != null)
|
---|
| 127 | {
|
---|
| 128 | externalSWFObjectFile = this.ViewState["externalswfobjectfile"].ToString();
|
---|
| 129 | }
|
---|
| 130 | if (!string.IsNullOrEmpty(externalSWFObjectFile))
|
---|
| 131 | {
|
---|
| 132 | if (externalSWFObjectFile.StartsWith("~"))
|
---|
| 133 | {
|
---|
| 134 | externalSWFObjectFile = this.ResolveUrl(externalSWFObjectFile);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | return externalSWFObjectFile;
|
---|
| 138 | }
|
---|
| 139 | set
|
---|
| 140 | {
|
---|
| 141 | this.ViewState["externalswfobjectfile"] = value.Trim();
|
---|
| 142 | externalSWFObjectFile = value.Trim();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | public string DataFile
|
---|
| 148 | {
|
---|
| 149 | get
|
---|
| 150 | {
|
---|
| 151 | if (this.ViewState["datafile"] != null)
|
---|
| 152 | {
|
---|
| 153 | datafile = this.ViewState["datafile"].ToString();
|
---|
| 154 | }
|
---|
| 155 | if (!string.IsNullOrEmpty(datafile))
|
---|
| 156 | {
|
---|
| 157 | if (datafile.StartsWith("~"))
|
---|
| 158 | {
|
---|
| 159 | datafile = this.ResolveUrl(datafile);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | return datafile;
|
---|
| 164 | }
|
---|
| 165 | set
|
---|
| 166 | {
|
---|
| 167 | this.ViewState["datafile"] = value;
|
---|
| 168 | datafile = value;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public string LoadingMsg
|
---|
| 173 | {
|
---|
| 174 | get { return loadingmsg; }
|
---|
| 175 | set { loadingmsg = value; }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | public bool EnableCache
|
---|
| 179 | {
|
---|
| 180 | get { return _enableCache; }
|
---|
| 181 | set { _enableCache = value; }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | protected override void OnInit(EventArgs e)
|
---|
| 185 | {
|
---|
| 186 | const string key = "swfobject";
|
---|
| 187 | string swfobjectfile = ExternalSWFObjectFile;
|
---|
| 188 | if (string.IsNullOrEmpty(ExternalSWFObjectFile))
|
---|
| 189 | swfobjectfile = Page.ClientScript.GetWebResourceUrl(this.GetType(), "OpenFlashChart.swfobject.js");
|
---|
| 190 |
|
---|
| 191 | if (!this.Page.ClientScript.IsClientScriptBlockRegistered(key))
|
---|
| 192 | {
|
---|
| 193 | this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), key, "<script type=\"text/javascript\" src=\"" + swfobjectfile + "\"></script>");
|
---|
| 194 | }
|
---|
| 195 | base.OnInit(e);
|
---|
| 196 | }
|
---|
| 197 | public override void RenderControl(HtmlTextWriter writer)
|
---|
| 198 | {
|
---|
| 199 | StringBuilder builder = new StringBuilder();
|
---|
| 200 | if (string.IsNullOrEmpty(ExternalSWFfile))
|
---|
| 201 | ExternalSWFfile = Page.ClientScript.GetWebResourceUrl(this.GetType(), "OpenFlashChart.open-flash-chart.swf");
|
---|
| 202 | builder.AppendFormat("<div id=\"{0}\">", this.ClientID);
|
---|
| 203 | builder.AppendLine("</div>");
|
---|
| 204 | builder.AppendLine("<script type=\"text/javascript\">");
|
---|
| 205 | builder.AppendFormat("swfobject.embedSWF(\"{0}\", \"{1}\", \"{2}\", \"{3}\",\"9.0.0\", \"expressInstall.swf\",",
|
---|
| 206 | ExternalSWFfile, this.ClientID, Width, Height);
|
---|
| 207 | builder.Append("{\"data-file\":\"");
|
---|
| 208 | //if both chart,datafile exists ,chart win.
|
---|
| 209 | if (ChartJson != null)
|
---|
| 210 | {
|
---|
| 211 | if (!EnableCache)
|
---|
| 212 | Page.Cache.Remove(this.ClientID);
|
---|
| 213 | Page.Cache.Add(this.ClientID, ChartJson, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0),
|
---|
| 214 | CacheItemPriority.Normal, null);
|
---|
| 215 | builder.Append("ofc_handler.aspx?chartjson=" + this.ClientID + "%26ec=" + (EnableCache ? "1" : "0"));
|
---|
| 216 | }
|
---|
| 217 | else
|
---|
| 218 | builder.Append(HttpUtility.UrlEncode(DataFile));
|
---|
| 219 | builder.Append("\"");
|
---|
| 220 | if (!string.IsNullOrEmpty(loadingmsg))
|
---|
| 221 | {
|
---|
| 222 | builder.AppendFormat(",\"loading\":\"{0}\"", loadingmsg);
|
---|
| 223 | }
|
---|
| 224 | builder.Append("});");
|
---|
| 225 | builder.AppendLine("</script>");
|
---|
| 226 |
|
---|
| 227 | writer.Write(builder.ToString());
|
---|
| 228 | base.RenderControl(writer);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|