2
  • I have a Visualforce page that houses some Visualforce charts.

    • A link on the Account navigates the user to a Visualforce page that
      has the charts and passes the AccountId parameter the URL.
    • I have 3 number fields on the Account object that I wish to summarize in a nice pie chart.

    public List getAccountRollUpAggregate() { List data = new List(); for (Account a : [SELECT Name, of_Closed_Won_New_Business_Opps__c, of_Open_New_Business_Opps__c, of_Closed_Lost_New_Business_Opps__c FROM Account WHERE Id=:ApexPages.currentPage().getParameters().get('id')]) { data.add(new PieWedgeData(Integer.valueOf(a.get('of_Closed_Won_New_Business_Opps__c')), Integer.valueOf(a.get('of_Open_New_Business_Opps__c')), Integer.valueOf(a.get('of_Closed_Lost_New_Business_Opps__c')))); } return data; }

Where the wrapper class is:

// Wrapper class
    public class PieWedgeData {

        public String name { get; set; }
        public Integer data { get; set; }
        public Integer dataOpen {get; set;}
        public Integer dataCWNB {get; set;}
        public Integer dataCLNB {get; set;}

And the constructor is:

    public PieWedgeData(Integer dataOpen, Integer dataCWNB, Integer dataCLNB) {
        this.dataOpen = dataOpen; 
        this.dataCWNB = dataCWNB; 
        this.dataCLNB = dataCLNB; 
    }
}

The corresponding Visualforce I have so far is:

    <apex:pageBlockSection columns="1" showHeader="true">
        <apex:chart data="{!AccountRollUpAggregate}" width="900" height="400">
            <apex:legend position="left"/>
            <apex:pieSeries labelField="dataCWNB" dataField="dataCWNB" donut="50">
            </apex:pieSeries>
        </apex:chart>
    </apex:pageBlockSection>  

The piechart is displaying only 1 integer - and I am unclear how to simply get the three number fields from the account record and have them display in a pie chart.

In the other piecharts I have created using the structure before (wrapper class, constructor, some constructor methods etc), they are working as expected - they are returning a value for multiple records whereas for this simple piechart I want multiple values for but 1 record.

Any help would be much appreciated.

Thanks

2

You need to still return multiple values, as each wedge is its own line item, so you'll need a different sort of wrapper here (or change how your data is laid out). For example, you might provide the data as:

public class DonutWedge {
    public String label;
    public Decimal value;
    public DonutWedge(String label, Decimal value) {
        this.label = label;
        this.value = value;
    }
}

 public DonutWedge[] getAccountRollUpAggregate() {
    DonutWedge[] results = new DonutWedge[0];
    results.add(new DonutWedge('Open', record.of_Closed_Won_New_Business_Opps__c));
    results.add(new DonutWedge('Open/New', record.of_Open_New_Business_Opps__c));
    results.add(new DonutWedge('Closed/Lost', record.of_Closed_Lost_New_Business_Opps__c));
    return results; 
 }

Which you'd then render as follows:

    <apex:chart data="{!AccountRollUpAggregate}" width="900" height="400">
        <apex:legend position="left"/>
        <apex:pieSeries labelField="label" dataField="value" donut="10" colorset="#f88,#8f8,#88f" />
    </apex:chart>

Here's an example that I built that demonstrates this code:

Sample Pie Chart

  • As always, THANK YOU so much for your fantastic efforts! Really helped me! – Andy Hitchings Oct 30 '15 at 20:32
  • Is there a way we can add text inside the pie donut at center? Like total count? – VarunC Jan 18 '17 at 14:04

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.