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
Mike 317Mike 317 

visualforce chart won't render

Hey, 

I'm trying to create a narrow chart that will show the color status of an account. Data is populated in a related object weekly. Currently, I'm not getting an error, but nothing is rendering. Debug logs show my query is returning the records. I've never used charts before, so it's probably something silly i've missed. Any thoughts?

Page
<apex:page standardcontroller="Account" extensions="ClientHealthExtension">
    <apex:chart height="125" width="100%" data="{!trendData}">
        <apex:axis type="Numeric" position="bottom" fields="week"/>
        <apex:axis type="Category" position="left" fields="xName"/>
        <apex:barSeries orientation="vertical" axis="bottom" xField="week" yField="xName"/>
    </apex:chart>
</apex:page>

Controller ext.
public class ClientHealthExtension {

    private final Account acct;
    public Account acctName;
    public ID acctId;
    public List<Account> nameList;
    
    public ClientHealthExtension (ApexPages.StandardController stdController){
        this.acct = (Account) stdController.getRecord();
        acctId = acct.Id;
        nameList = [SELECT Name FROM Account WHERE Id=:acctId limit 1];
        if(!nameList.isEmpty()){
            acctName = nameList[0];
        }
    }
    
    public List<Account_Trend__c> gettrendData(){
        List<Account_Trend__c> trends = [SELECT Account_Name__c, Client_Health_Status_Indicator__c, CreatedDate FROM Account_Trend__c WHERE Account_Name__c =:acctName.Name ORDER BY CreatedDate];
        system.debug('trends: '+trends);
        return trends;
    }
    // Wrapper class
    public class trendData {

        public String xName { get; set; }
        public String health { get; set; }
        public date week { get; set; }
        public ID xId { get; set; }
        public String xIso { get; set; }

        public trendData(String xName, String health, date week, ID xId, String xIso) {
            this.xName = xName;
            this.health = health;
            this.week = week;
        }
    }    
}

 
Steven NsubugaSteven Nsubuga
Hey Mike, you are not referring to the wrapper class at all.
Try this, I have modified your gettrendData method to return a list of the wrapper object.
public class ClientHealthExtension {
    
    private final Account acct;
    public Account acctName;
    public ID acctId;
    public List<Account> nameList;
    
    public ClientHealthExtension (ApexPages.StandardController stdController){
        this.acct = (Account) stdController.getRecord();
        acctId = acct.Id;
        nameList = [SELECT Name FROM Account WHERE Id=:acctId limit 1];
        if(!nameList.isEmpty()){
            acctName = nameList[0];
        }
    }
    
   
    
    public List<trendData> gettrendData() {
        List<Account_Trend__c> trends = [SELECT Account_Name__c, Client_Health_Status_Indicator__c, CreatedDate FROM Account_Trend__c WHERE Account_Name__c =:acctName.Name ORDER BY CreatedDate];
        List<trendData> trendWrappers = new List<trendData>();
        for (Account_Trend__c trend : trends) {
            trendWrappers.add(new trendData(trend.Account_Name__c, trend.Client_Health_Status_Indicator__c, 
                                    date.newinstance(trend.CreatedDate.year(), trend.CreatedDate.month(), trend.CreatedDate.day()), trend.Id, 'testString'));
        }
        return trendWrappers;
    }
    
    // Wrapper class
    public class trendData {
        
        public String xName { get; set; }
        public String health { get; set; }
        public date week { get; set; }
        public ID xId { get; set; }
        public String xIso { get; set; }
        
        public trendData(String xName, String health, date week, ID xId, String xIso) {
            this.xName = xName;
            this.health = health;
            this.week = week;
        }
    }    
}

 
Raj VakatiRaj Vakati
My code is not 100 % correct but you have to do it like this
 
public class ClientHealthExtension {

    private final Account acct{get;set;}
    public Account acctName{get;set;}
    public ID acctId{get;set;}
    public List<Account> nameList{get;set;}
    
    public ClientHealthExtension (ApexPages.StandardController stdController){
        this.acct = (Account) stdController.getRecord();
        acctId = acct.Id;
        nameList = [SELECT Name FROM Account WHERE Id=:acctId limit 1];
        if(!nameList.isEmpty()){
            acctName = nameList[0];
        }
    }
    
    public List<trendData> gettrendData(){
		List<trendData> td = new list<trendData>() ; 
		
        List<Account_Trend__c> trends = [SELECT Account_Name__c, Client_Health_Status_Indicator__c, CreatedDate FROM Account_Trend__c WHERE Account_Name__c =:acctName.Name ORDER BY CreatedDate];
       for(Account_Trend__c t :trends){
		   // parse and set the  value
	   }
        return td;
    }
    // Wrapper class
    public class trendData {

        public String xName { get; set; }
        public String health { get; set; }
        public date week { get; set; }
        public ID xId { get; set; }
        public String xIso { get; set; }

        public trendData(String xName, String health, date week, ID xId, String xIso) {
            this.xName = xName;
            this.health = health;
            this.week = week;
        }
    }    
}

Refer this link 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_charting_example.htm
Mike 317Mike 317
Hey Steven,
I've added your code and i'm getting an illegal List to List conversion. "Illegal conversion from List<ClientHealthExtension.trendData> to List<Account_Trend__c>"

Did I miss something? Here's the code with updates:
 
public class ClientHealthExtension {

    private final Account acct;
    public Account acctName;
    public ID acctId;
    public List<Account> nameList;
    
    public ClientHealthExtension (ApexPages.StandardController stdController){
        this.acct = (Account) stdController.getRecord();
        acctId = acct.Id;
        nameList = [SELECT Name FROM Account WHERE Id=:acctId limit 1];
        if(!nameList.isEmpty()){
            acctName = nameList[0];
        }
    }
    
    public List<Account_Trend__c> gettrendData(){
        List<Account_Trend__c> trends = [SELECT Account_Name__c, Client_Health_Status_Indicator__c, CreatedDate FROM Account_Trend__c WHERE Account_Name__c =:acctName.Name ORDER BY CreatedDate];
        List<trendData> trendWrappers = new List<trendData>();
        for (Account_Trend__c trend : trends) {
            trendWrappers.add(new trendData(
                trend.Account_Name__c, 
                trend.Client_Health_Status_Indicator__c, 
                date.newinstance(trend.CreatedDate.year(), trend.CreatedDate.month(), trend.CreatedDate.day()), 
                trend.Id, 'testString'));
        }
        return trendWrappers;
    }
    // Wrapper class
    public class trendData {

        public String xName { get; set; }
        public String health { get; set; }
        public date week { get; set; }
        public ID xId { get; set; }
        public String xIso { get; set; }

        public trendData(String xName, String health, date week, ID xId, String xIso) {
            this.xName = xName;
            this.health = health;
            this.week = week;
        }
    }    
}

 
Steven NsubugaSteven Nsubuga
Hey Mike, you forgot to change the return type of the method, change it from List<Account_Trend__c> to List<trendData>.