We are trying to create a Visualforce page which will include a graph displaying budget data. For testing purposes, I want to display 3 columns as defined in the code below.
Running the query in the apex class from the editor provides the results I'm expecting. Only the graph is refusing to display the data.
Following error is provided in the component inspection console: BudgetGraph?core.apexpages.request.devconsole=1:34 Uncaught (in promise) TypeError: Cannot read property 'length' of undefined at drawChart (BudgetGraph?core.apexpages.request.devconsole=1:34)
I really cannot get my head around this simple one...
Visual Force Page (header section):
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
Array.prototype.reduce = undefined;
// Load Google Chart
google.charts.load('current', {'packages':['line']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
var bdgtData; //Variable to store data
// Call remote action method
BudgetGraphController.getBudgetData(function(result, event){
bdgtData = result; //get data from Apex controller
},{escape:true});
// Draw Google chart
function drawChart() {
//Create the data table
var data = new google.visualization.DataTable();
data.addColumn('string', 'Timing');
data.addColumn('string', 'Budget Engineering');
data.addColumn('string', 'Budget PM');
//Add datatable rows
for(i = 0; i< bdgtData.length; i++){
data.addRow([bdgtData[i].Timing__c, bdgtData[i].Budget_Engineering__c, bdgtData[i].Budget_Project_Management__c]);
}
var options = {
title: 'Project Budget',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, google.charts.Line.convertOptions(options));
}
</script>
Apex Class:
global with sharing class BudgetGraphController {
@RemoteAction
global static List<Budget__c> getBudgetData(){
List<Budget__c> bdgtGroupList = [Select Timing__c, Budget_Engineering__c, Budget_Project_Management__c From Budget__c ORDER BY Timing__c ASC];
return bdgtGroupList;
}
}
curve_chart
on a raw HTML element e.g. adiv
? If it is not and is on an<apex:...>
element, a$=
selector is one way to ignore the prefixes that Visualforce applies to the Id that you supply.