function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
amritamrit 

How to display value in VFpage using javascript?

Hi,

 

Im trying to create a class and VF page.Here i need to show some reports in google chart.Below mentioned is the class .

public class GoogleChartsController
{
@RemoteAction
public list<AggregateResult> lstAR = new list<AggregateResult>();

public GoogleChartsController()
{
lstAR = [SELECT  SUM(Almost_Overdue__c) AO ,SUM(Balance_Overdue__c) BO ,SUM(Total_Balance__c) TB ,Account_NameLookup__c from Invoice__c GROUP BY Account_NameLookup__c];
}
public list<OppClass> getResults()
{
list<OppClass> lstResult = new list<OppClass>();
for (AggregateResult ar: lstAR)
{
oppClass objOppClass = new oppClass(ar);
lstResult.add(objOppClass);
}
return lstResult;
}

class oppClass
{
public Double AO
{ get;set; }
public Double BO
{ get;set; }
public Double TB
{ get;set; }



public oppClass(AggregateResult ar)
{
//Note that ar returns objects as results, so you need type conversion here
AO = (Double)ar.get('AO');
BO = (Double)ar.get('BO');
TB = (Double)ar.get('TB');


}
}
}

 

<apex:page controller="GoogleChartsController" sidebar="false"> 
    <!-- Google API inclusion -->
    <apex:includeScript id="a" value="https://www.google.com/jsapi" />
     
    <apex:sectionHeader title="Google Charts + Javascript Remoting" subtitle="Demoing - Opportunities by Exepected Revenue"/>
 
    <!-- Google Charts will be drawn in this DIV -->
    <div id="chartBlock" />
     
    <script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});
       
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(initCharts);
   
        function initCharts() {         
          // Following the usual Remoting syntax
          // [<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {...}
          // controller : GoogleChartsController
          // method : loadOpps
          GoogleChartsController.Results( 
                 function(result, event){  
                     // load Column chart
                     var visualization = new google.visualization.ColumnChart(document.getElementById('chartBlock'));
                     // Prepare table model for chart with columns
                     var data = new google.visualization.DataTable();
                     data.addColumn('number', 'AO');
                     data.addColumn('number', 'BO');
                     data.addColumn('number', 'TB');    
                     // add rows from the remoting results
                     for(var i =0; i<result.length;i++){
                        var r = result[i];
                        data.addRow([r.AO, r.BO, r.TB]); 
                      }
                    // all done, lets draw the chart with some options to make it look nice.
                    visualization.draw(data)
          } 
    </script>
</apex:page>

 I used @remote action also.But values are not displaying in VF page.Do we need to used global in class?If yes where i need to give the global variable.Can anyone help me on this??

Anu Raj.ax1269Anu Raj.ax1269

Hi amith

 

I have not look into the code you wrote here is the simple way to add the value from Javascript to Vf page 

 

<script>

document.getElementById("pageId:formId:theBlock:pageBlockSectionid1:outputId1").innerHTML=distance;

</script>

<apex:commandButton value="Find Distance" action="{!chkDistance}" onclick="checkDistance()" reRender="distanceID"/>
 <apex:outputText id="outputId1" label="Arc Distance :"></apex:outputText>

 

This a piece of code. In JavaScript you need to add the id of all the component in which the output text is. If id not defined the go to source code and find id.

 

Thanks

Anu

amritamrit

Thanks for your reply. In apex class im trying to call values from method using controller.I want to know how to get that values in VF page using javascript.

 

public list<OppClass> getResults()
{
list<OppClass> lstResult = new list<OppClass>();
for (AggregateResult ar: lstAR)
{
oppClass objOppClass = new oppClass(ar);
lstResult.add(objOppClass);
}
return lstResult;
}

 This is method to get those values.

 <script type="text/javascript">
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', {'packages':['corechart']});
       
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(initCharts);
   
        function initCharts() {         
          // Following the usual Remoting syntax
          // [<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {...}
          // controller : GoogleChartsController
          // method : loadOpps
          GoogleChartsController.Results( 
                 function(result, event){  
                     // load Column chart
                     var visualization = new google.visualization.ColumnChart(document.getElementById('chartBlock'));
                     // Prepare table model for chart with columns
                     var data = new google.visualization.DataTable();
                     data.addColumn('number', 'AO');
                     data.addColumn('number', 'BO');
                     data.addColumn('number', 'TB');    
                     // add rows from the remoting results
                     for(var i =0; i<result.length;i++){
                        var r = result[i];
                        data.addRow([r.AO, r.BO, r.TB]); 
                      }
                    // all done, lets draw the chart with some options to make it look nice.
                    visualization.draw(data)
          } 
    </script>

 im calling like this.But Values are not displaying.Please help me to find the error