0

I am trying to pull number of Accounts for every country in a pie chart using Visualforce, but no results in the page -

Here is my code, any help is appreciated -

public class PieChartControllerTest1 {
public PieChartControllerTest1 ()
{
}

public List<AggregateResult> getAccountCountry()
{
    AggregateResult[] result = [SELECT COUNT(Name) cnt, BillingCountry FROM Account GROUP BY BillingCountry];
    return result;
}

}

visualforce page:

<apex:page controller="PieChartControllerTest1" title="Pie Chart">
<apex:pageBlock >
<apex:pageBlockSection title="Account By Country">
<apex:pageBlockSectionItem >
<apex:chart height="450" width="750" data="{!AccountCountry}">
<apex:pieSeries labelField="Count" dataField="cnt">
<apex:chartLabel display="rotate" field="cnt"/>
</apex:pieSeries>
</apex:chart>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
0
1

Here is the updated Code!!

public class pieChartAccountCtrl {
    public pieChartAccountCtrl (){
    }

    public List<AggregateResult> getAccountCountry()
    {
        AggregateResult[] result = [SELECT COUNT(Name) cnt, BillingCountry FROM Account WHERE BillingCountry != null GROUP BY BillingCountry];
        return result;
    }

}

And Vf Page code is :

<apex:page controller="pieChartAccountCtrl" title="Pie Chart">
    <apex:pageBlock >
        <apex:pageBlockSection title="Account By Country">
            <apex:pageBlockSectionItem >
                <apex:chart height="450" width="750" data="{!AccountCountry}">
                    <apex:pieSeries labelField="BillingCountry" dataField="cnt">
                        <apex:chartLabel display="rotate" field="cnt"/>
                    </apex:pieSeries>
                </apex:chart>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Hope this will help you and if it is then don't forget to mark it as correct answer ;)

1

Hi Pratibha, I think there are some account records with null value in BillingCountry in your org, so use a where clause so that only those records having some value in BillingCountry will be queried.

AggregateResult[] result = [SELECT COUNT(Name) cnt, BillingCountry FROM Account WHERE BillingCountry != null GROUP BY BillingCountry];

/*****In VF Page*****/
<apex:pieSeries labelField="BillingCountry" dataField="cnt">

Hope this may help.

1
  • As per Keith's suggestion I modified the code line in my answer for VF page. – Nebula Jun 3 '15 at 8:47
0

In addition to Nebula's fix, you also need to ensure that both names in the apex:pieSeries match the names returned by the query. For the non-aggregated field you can just use the field name:

<apex:pieSeries labelField="BillingCountry" dataField="cnt">

For aggregated fields its best to use an alias as you already have done for "cnt".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.