General Fixes and Updates to the .NET OpenFlashChart Control

We tried using the .NET version of the OpenFlashChart Control and found some frustrating nuances and bugs. We found this to be a very useful control, so it was worth my time to make some fixes. This write up describes some of the fixes that we made to overcome these issues. This write up applies to version 1.9.6 of the chart (dot-net-ofc-library), which can be downloaded here

Javascript error in Internet Explorer

If you can read Italian, there is a tutorial on the .NET version of the OpenFlashChart Control here. I don't speak Italian, but the solution to the javascript error that can be found in this tutorial.



The author suggests (I think) that a change be made to the Chart.cs file's RenderContents() function. The change is to add a sequence number to the ClientId in the three spots that it is referenced. We made a slightly different change in that we added "_1" to the client id so every reference to this property now looks like this.ClientID + "_1". Here is the code:

I get a Javascript error saying 'SWFObject' not found in Internet Explorer

I found this when I rolled my code from my development machine to my website. "It works in dev, so it must be the server". Well, not exactly. This has to do with RenderContents() in Chart.cs again. If you look where the relative paths for the javascript and flash objects are getting built, you'll see that HttpRuntime.AppDomainAppVirtualPath are getting prepended to the path. On my production site, this value was just "/", so the relative path to swfojbect.js was //aspnet_client/OpenFlashChart/js/swfobject.js. IE did not like the "//". I changed all calls from HttpRuntime.AppDomainAppVirtualPath and replaced it with this.Page.Request.ApplicationPath.TrimEnd('/'). This worked for both IE and Firefox. Here is the code for the function:

Labels on the X axis don't show up!

We spent about an hour trying to figure out why the labels on the X axis of the chart were not displaying. At first we thought we had a bug in our data page. The bug has to do with the fact that the labels were not taken care of in the ToString() function of Graph.cs. So, I added this:



Here is the complete code for this function with my change in the middle:

A bug in LabelStyle.cs

A recurring theme in some of the bugs we found have to do with the ToString() functions. There is a subtle one in LabelStyle.cs with the instantiation of the StringBuilder. The Size property is of type int, so the original code sets the capacity of the StringBuilder. However, I think the intention was to initialize the StringBuilder with the string value of Size. We found this once we started to see the X axis labels with the fix listed in the section above. Here is the ToString() function for LabelStyle.cs (the change is this.Size.ToString() ):

Null references in the ToString() functions

Let's keep going with ToString() bugs. Well, it's not really a bug per se, but too much confidence is placed on the consumer of this control to set every string property of the classes. A lot of the constructors in the OpenFlashChart Control library are overloaded. This is always useful, but the problem is that there are cases when the class contains strings that are initialized with null. This is fine if you assume that the library consumer will think to set these properties before ToString() executes, but it wasn't fine for me because I didn't set all of the properties. Let's just look at LabelStylex.cs as an example. This class has a property called GridColor, which is not required in 4 of the 5 constructors. The problem is that we did not set this property, so when ToString() was executed we got an "Object reference not set to an instance of an object" exception. Yay! You could probably do anything from change the constructors to initialize these values to something other than null to something like putting a null check in the ToString() function. We chose the latter:

The infamous IE7 caching problem

Ok, enough with ToString() - at least from me anyway. Let's talk about the caching problem in IE7 that prevents your charts from updating. We almost quit on this control because of this. I mean, I'm browser agnostic, but too many people use IE to ignore this. The problem here comes when when you update the URL property on the control and rebind using DataBind(). You should see your data right? Well probably not if you use (at least) IE7. IE7 will cache your calls to the data page which prevents your chart from refreshing on calls after the first (and sometimes not even then). This problem is well documented on the forums, but we never saw the .NET solution, so we'll post ours here. Sorry if someone else found this - we're not making any claims of being the first. Let's assume your data page is called data.aspx and you do all of your work in the Load event handler. Here is a sample Page_Load (in VB - sorry):



The fix is really easy actually. The first line takes care of it: Response.CacheControl = "no-cache". This adds the "Cache-Control" parameter to the response headers and tells IE not to cache your page. NOTE: If this doesn't work for you, make sure you clear your temporary internet files and try again.

The Grand Finale - AJAX!!!!

We've seen in the forums requests for AJAX support for the OpenFlashChart Control and my website uses AJAX, so we figured we'd give it a shot. Someone else may have a better solution, but this worked for us.

The reason that the OpenFlashChart Control doesn't work using AJAX, as is, is because the output of the control uses javascript to load the flash chart (view the page source to see what I'm talking about). Since javascript will only fire when the page loads (or on some event like a button click), the chart will render the first time, but it won't update when placed in an UpdatePanel on an AJAX enabled website (since only the controls in the UpdatePanel are replaced, not the whole page).

The way that I've handled this with other AJAX controls is by registering the script with the ScriptManager class. Basically, we just added a second StringBuilder to the RenderContents() function, called sb2, in Chart.cs. This is where the javascript gets built now instead. Once the script is built, I don't output the string with the rest of the flash code, but instead I register that script using the ScriptManager class. This makes the script fire every time the control is loaded in my UpdatePanel, which refreshes the chart, and voila! AJAX!

Here is the ToString() function for Chart.cs with AJAX support (with the ClientId and relative path fixes mentioned before):





That's all we have for now. We got everything working the way we needed so hopefully some of the solutions described in this write up will help you. If you have questions or comments email us.

We added a quickstart .NET tutorial here.

An AJAX demo can be found here.