Integrating Visualforce And GoogleCharts


Google Charts is a very powerful, free javascript library that can be used to produce a wide variety of colorful and useful charts, from pie charts to histograms. Combined with Visuaforce, the  Google Charts can offer more flexibility and distribution potential than using a dashboard. Since the charts are generated through a URL, the visualizations can be shared and embedded wherever images are permitted.

In this post, we will focus on combining Visualforce to Google Charts.Below is an example,
Visualforce Page:
<apex:page controller="Goal" sidebar="true" showHeader="true" showChat="true"> 
    <apex:includeScript id="a" value="https://www.google.com/jsapi" />
    <div id="chartBlock" />
    <script type="text/javascript">
    google.load('visualization', '1.0', {'packages':['corechart']});
    google.setOnLoadCallback(initCharts);
    function initCharts() {       
        Goal.loadGoals(
            function(result, event){ 
                var visualization = new google.visualization.ColumnChart(document.getElementById('chartBlock'));
                var data = new google.visualization.DataTable();
                data.addColumn('string', 'Sales Reps');
                data.addColumn('number', 'Goals');
                data.addColumn('number', 'Completed');   
                for(var i =0; i<result.length;i++){
                    var r = result[i];
                    data.addRow([r.Name, r.Goals__c, r.Completed__c]);
                }
                visualization.draw(data, {legend : {position: 'top', textStyle: {color: 'blue', fontSize: 10}}, width:window.innerWidth,vAxis:{textStyle:{fontSize: 10}},hAxis:{textStyle:{fontSize: 10},showTextEvery:1,slantedText:false}});
            }, {escape:true});
    }
    </script>
</apex:page>

  •   <apex:includeScript id="a" value="https://www.google.com/jsapi" /> Using this Apex Component we can include Free Google Charts JavaScript URL in Visualforce.


Apex Controller:


global with sharing class Goal {
    @RemoteAction
    global static Sales_Reps__c[] loadGoals() {
        return [select  Name, Goals__c, Completed__c from Sales_Reps__c limit 5];
    } 

}
@RemoteAction :The RemoteAction annotation provides support for Apex Methods used in Visualforce to be called via JavaScipt.
The RemoteAction annotation provides support for Apex methods used in Visualforce to be called via JavaScript.
OutCome:








Comments

Post a Comment

Popular posts from this blog

Configur Docusign For Salesforce

How To Make DataTable Column Resizable

Lightning:recordForm - Lightning Data Service