Visualforce Charts with Javascript

Hi All,

This post talks about creating CUSTOM CHARTS in visualforce pages with javascript.

There are multiple ways to show charts in visualforce pages, such as we could create a report, dashboard and map the image to visualforce page, or use google charts api, or we can use charts tag which is direct and dynamic.

Requirement: Embed a chart which shows the salesrep the quarterly Targets and Booked.

Solution: It is assumed that we already have the data with the following fields.

Q1_Target__cQ2_Target__c
Q3_Target__c
Q4_Target__c

Q1_Booked__cQ2_Booked__c
Q3_Booked__c
Q4_Booked__c

Targets are set by the managers, and Booked are calculated using Rollup summaries on Opportunities.

Now that we have the data, lets create a visualforce page with charts.

There is this tag <apex:chart> to render data as charts.
Please read this link for more information.

create a VF page and run the following code.

<apex:page standardController="Account" >
 <apex:pageBlock title="Quarterly Performance">
         
         <script> 
         
            var dataArray = new Array();
           // the data in this array will be referred in the tags 
            dataArray.push({'data1':{!account.Q1_Target__c},'data2':{!account.Q1_Booked__c}, 'name':'Q1'});
            dataArray.push({'data1':{!account.Q2_Target__c},'data2':{!account.Q2_Booked__c}, 'name':'Q2'});
            dataArray.push({'data1':{!account.Q3_Target__c},'data2':{!account.Q3_Booked__c}, 'name':'Q3'});
            dataArray.push({'data1':{!account.Q4_Target__c},'data2':{!account.Q4_Booked__c}, 'name':'Q4'});
             
          </script>
        

<apex:chart height="400" width="800" data="dataArray">
    <apex:axis type="Numeric" position="left" fields="data1" minimum="2"
        title="In Millions"/> 
    <apex:axis type="Category" position="bottom" fields="name"
        title="Quarters"/>
        
    <apex:barSeries title="Quarterly Distributions" orientation="vertical" axis="left"
        xField="name" yField="data1,data2" colorSet="#F0F,#F00" groupGutter="1">
        
        <apex:chartTips height="20" width="120"/>
        
    </apex:barSeries> 
    
</apex:chart>

                  
         </apex:pageBlock>
</apex:page>

Point it to an account and see the chart.



\\,,//







Notes:
1 - Array() function is a dynamic array, u can push multi dimensional data into it. 
2 - groupGutter attribute in barseries tag specifies the gap between the twi bars.
3 - colorSet is used to assign the custom colors. we could use a legend tag and show the pattern.

This can be used in PE, this solution doesnt have apex. 


HTH
Prabhan

Comments

Popular posts from this blog

Lightning spinner inside Button

Passing URL parameters to controller in Lightning components/ AURA

Nested AURA:IFs in Lightning Components Salesforce