I have been developing a fairly rich Silverlight application for the last couple of months that is meant to ultimately replace some of our existing UI. This project has evolved from prototype to framework authoring to actual wire-frame implementations. I have had a chance now to step back and create some small charting components for one of our existing pages. A change of pace can be refreshing, and this one allows me to stay in Silverlight land yet spend about a week writing some production code.
There are a number of charting components available, many of them free. I am going to take this opportunity to write my own from scratch since I really only need some simple pie charts. I want to keep it small and simple, and yet learn from the experience.
I have run into a new situation with these small components that I had not encountered with the large application. I need to pass “command line arguments”. Or rather, I need to embedd some data in the HTML page for the Silverlight to pick-up and render. This data will be the charting data. As it turns out this is dead simple. In the Silverlight object tag I simply include the initParams parameter with my data, comma-delimited:
<object type="application/x-silverlight" width="100%" height="100%">
<param name="source" value="ClientBin/MyChartingApp.xap"/>
<!-- Command Line Arguments -->
<param name="initParams" value="datum1=1,datum2=2,etc" />
</object>
Then I read that data in during the application’s Load event:
private void Application_Startup(object sender, StartupEventArgs e)
{
foreach (var key in e.InitParams.Keys)
{
var datum = e.InitParams[key];
//do something with the datum
}
}
Pretty simple! Now I just need to write some ASP.Net code to render out the desired HMTL, and I am all set. Oh, that and write the Silverlight component.