+ Start a Discussion
Ryan Bell 16Ryan Bell 16 

Query to load data based on most recent date of a custom data field to use in a visualforce barchart

Hi All,

I am trying to load a barchart after pressing a button to load the most recent date inside a custom date field, keeping it dynamic. Is there any solution to do this inside of my controller that someone has implemented before?

Many thanks,
Ryan
Saroja MuruganSaroja Murugan
Hi Ryan,

Please try the below code.

VF Page
<apex:page controller="AccountRec_Barchart" docType="html-5.0" title="Bar chart" >
    <apex:form >
        
        <apex:outputText value="from Date">
        </apex:outputText>
        <apex:input type="date" value="{!fromdate}"/>
        <apex:outputText value="ToDate"></apex:outputText>
        <apex:input type="date" value="{!todate}" />
        <apex:commandButton value="submit" action="{!submit}" reRender="chartid" />
        
    </apex:form>
    
    <apex:chart id="chartid" height="300" width="650" data="{!data}" colorSet="2A98DC"  resizable="true" >
        
        <apex:axis type="Numeric" position="left" fields="count" title="Recordcount" minimum="0" grid="false" />
        <apex:axis type="Category" position="bottom" fields="billingcityname" title="billingcity"  />
        <apex:barSeries orientation="vertical" axis="left" xField="billingcityname" yField="count">
            
        </apex:barSeries>
        
    </apex:chart>  
</apex:page>

Controllor

public class AccountRec_Barchart
{
    public date fromDate{set;get;}
    public date todate{set;get;}
    public list<aggregateresult> listaggregate= new list<aggregateresult>();
    public list<barchartdata> lb = new list<barchartdata>();
    
    public void submit()
    {
        listaggregate = [select name, COUNT(Id) n
                         from Account 
                         where createddate > : fromDate and createddate <:todate
                         group by name
                        ];
    }
    
    public barchartdata[] getdata()
    {   
        system.debug('getdata');
        barchartdata[] datachart = new barchartdata[]{};
            
            for(aggregateresult a :listaggregate)
        {
            datachart.add(new barchartdata(a));     
        }
        return datachart;
    }
    
    // wrapper class 
    public class barchartdata
    {
        public string name {set;get;}
        public  integer count{set;get;}
        
        barchartdata(aggregateresult result)
        {
            this.count = (Integer) result.get('n');
            this.name = (string) result.get('billingcity');           
            
        }
    } 
}

In controllor you can replace the created date field with your custom date field.

Thanks,
Saroja
Sweet Potato Tec