After you have set up your visualforce workflow and trained you employees, next thing you would want is to analyze your data. You would want to keep track of your business’ key performance indicators (KPIs) to have the big picture of where your organization stands and where its heading. Visual Force has in built charting API but the variety of charts that you can draw are limited. A better alternative is Fusion Charts and since the arrival of FusionCharts VisualForce API component, it integrates seemlessly with VisualForce.
Let’s try to draw a basic column chart of weekly sales data using FusionCharts VisualForce API. Here is what you will need to do.
Get Fusion Charts
First thing you will need is to import static FusionCharts resources on VisualForce. You can download evaluation version from here. You will need to upload all the charts as static resources.
- Unzip the archive
- Obtain
Charts
folder and compress it into a zip file - Go to Salesforce | Setup | Develop | Static Resources | New upload the zip under the name
FusionCharts
Upload the component
- Go to Salesforce | Setup | Develop | Components | New
- Copy the contents of component.xml and save under the name
fusioncharts
Now you are all set to use Fusioncharts Visualforce API inside your apex pages.
Controller
You will need to setup a apex controller to provide data to render the charts. The component will accept the name and the @RemoteAction method of this controller which will provide data. For our use case, we will need the following controller. Add this controller in Salesforce | Setup | Developer Console.
Remember to send the JSONified data using VisualForce JSON API
global class MyController{
public MyController(){}
@RemoteAction
public static String getJSONColumnData(){
List datalist = new List();
datalist.add(new Data("Week1", "14400"<span style="font-size: 13px; line-height: 19px;">));</span>
datalist.add(new Data("Week2", "19600"));
datalist.add(new Data("Week3", "24000"));
datalist.add(new Data("Week4", "15700"));
Wrapper data = new Wrapper(datalist);
return data.toJSON();
}
private class Data{
private String label;
private String value;
public Data(String label, String value){
this.label = label;
this.value = value;
}
}
private class Wrapper{
private List data;
public Wrapper(List list){
this.data = list;
}
}
}
Display the Chart
Now that your backend is set up all we need to do is use the <apex>
component API to draw the chart. Since we already have component.xml
in a component under the fusioncharts
namespace, we can refer to that component on any apex page using <c:fusionchart> tag.
It is as attributes of this tag that you will use the component api. Anything that you would have provided as an attribute to <chart/>
tag in XML api, or as a chart property in FusionCharts JSON api, you will provide as an attribute to this component. Additionally you will have to provide the chart
attribute to refer to the relevant FusionCharts static file for example, chart="{!URLFOR($Resource.FusionCharts, 'Charts/Column3D.swf')}"
, and a chartId
attribute to identify a chart uniquely on a page. You will also have to provide height
and width
attributes.
On any apex page, place the following tag.
<c:fusioncharts chartId="firstChart" chart="{!URLFOR($Resource.FusionCharts, 'Charts/Column3D.swf')}" width="400" height="300" debugMode="0" dataController="MyController.getJSONColumnData" caption="Weekly Sales Summary!" xAxisName="Week" yAxisName="Sales" numberPrefix="$"/>
And that’s it
You have created your first FusionCharts using Apex API. If everything went well, you should be able to see screen similar to this.
[Image missing due to VisualForce account expired.]
Recent Comments