Silverlight Charting with Visifire
Comments(2)With Visifire you can create nice looking Silverlight chart within minutes. Let me explain you in simple steps.
First of all you require Silverlight 2.0 installed on your system. You can download it from here.
Download and extract the contents of Visifire Charts package into a directory in your SERVER. You can download it from here. Visifire is Open Source and free.
Visifire requires the data in the form of XML. XML for creating chart is simple and intuitive. Minimum basic XML required looks like below
<vc:Chart xmlns:vc=”clr-namespace:Visifire.Charts;assembly=Visifire.Charts”> </vc:Chart>
The above tag corresponds to a chart element. But there is not data to plot. For now you need not worry about “vc:”. Just remember that you need to add “vc:” to each element.
Now let me add some data into the chart. First I’ll create a Column Chart showing Sales details for a fictitious company, for the first four months….
Then I’ll show you how easy it is to switch to other chart types just by changing One Word!
<vc:DataSeries RenderAs=”Column”> <vc:DataPoint AxisLabel=”Jan” YValue=”160021″/> <vc:DataPoint AxisLabel=”Feb” YValue=”207223″/> <vc:DataPoint AxisLabel=”Mar” YValue=”225542″/> <vc:DataPoint AxisLabel=”Apr” YValue=”248827″/> <vc:DataPoint AxisLabel=”May” YValue=”273432″/> </vc:DataSeries>
Above is the Revenue details for my company. Each value that you are required to enter is a DataPoint and a set of similar DataPoint represents a DataSeries.
Now I require a Title for Chart. So I will create a Title tag.
<vc:Title Text=”Company Revenue Details”/>
And finally a Title for the AxisY.
<vc:AxisY Title=”Revenue” Prefix=”$”>
So now I have all the data ready to create my chart. So the next thing is to put all the data in one place. So here is the final XML code.
<vc:Chart xmlns:vc=”clr-namespace:Visifire.Charts;assembly=Visifire.Charts”> <vc:Title Text=”Company Revenue Details”/> <vc:AxisY Title=”Revenue” Prefix=”$”> </vc:AxisY> <vc:DataSeries RenderAs=”Column”> <vc:DataPoint AxisLabel=”Jan” YValue=”160021″/> <vc:DataPoint AxisLabel=”Feb” YValue=”207223″/> <vc:DataPoint AxisLabel=”Mar” YValue=”225542″/> <vc:DataPoint AxisLabel=”Apr” YValue=”248827″/> <vc:DataPoint AxisLabel=”May” YValue=”273432″/> </vc:DataSeries> </vc:Chart>
I’ll save the above XML into a file and call it as “data.xml”. Now you just have to reference this XML file from within the HTML page to get a chart. So you need to add these lines of code into your HTML page. make sure you have extracted Visifire.js and Visifire.xap in the same directory as the HTML page.
<script type=“text/javascript” src=“Visifire.js”></script> <div id=“VisifireChart”> <script language=“javascript” type=“text/javascript”> var vChart = new Visifire(“Visifire.xap”,500,300); // Visifire path, width, height vChart.setDataUri(“data.xml”);//xml file path vChart.render(“VisifireChart”);// DOM element in which chart is to be rendered. </script> </div>
And you are done. This is how the chart looks.
Visifire Charts Live Demo
Now you can change the RenderAs attribute in the DataSeries to Doughnut, Bar, Pie, Area and see the chart type change.
Also change the looks of the Chart by adding an attribute “Theme=Theme3″ to the Chart element.
In the next session I’ll be writing about some more cool features of Visifire.