1

I want to have a chart created using Apex. Below is the Object parameters: Pie chart should have Product types shown based on the total quantity of that particular product.

ProductType__c QuantitySold__c

A 10

B 20

A 30

C 25

C 30

My Apex Code is :

public class AFS {

public class Data 
    {  
    String name { get; set; }   
    public Integer data1 { get; set; }    
    public Data(String name, Integer data1)
     {    
       this.name = name;     
      this.data1 = data1;  
    } 
    }  

     public List<Data> getChartData()
     {      
         List<AggregateResult> arList = [SELECT Revenue__c.ProductType__c PT, SUM(Revenue__c.QuantitySold__c) sum from Revenue__c GROUP BY Revenue__c.ProductType__c];  
         List<Data> dataList = new List<Data>();      

      for (AggregateResult ar : arList)
     {   
      String name = String.ValueOf(ar.get('PT'));;
      Integer data1 = Integer.ValueOf(ar.get('sum')); 
      dataList.add(new Data(name, data1));
    }  

       return dataList;


     } 

}

VisualForce Code is :

<apex:page controller="AFS">
<apex:form >
<apex:pageBlock title="Quantity sold per product">
   <apex:chart height="350" width="450" data="{!chartData}">
    <apex:pieSeries dataField="data1" labelField="name"/>
    <apex:legend position="right"/>
    </apex:chart>
</apex:pageBlock>

</apex:form>

The Output only shows the below line :

Quantity sold per product

I am unable to find out a feasible solution to this problem. Any help will be highly appreciated. Thanks in advance ... !!!!!

1

I have tried your code with Quantity data and it is generating the chart. Please update your Query and it should work.

VF Page

<apex:page controller="AFS">
    <apex:form >
        <apex:pageBlock title="Quantity sold per product">
            <apex:chart height="350" width="450" data="{!chartData}">
                <apex:pieSeries dataField="data" labelField="name"/>
                <apex:legend position="right"/>
            </apex:chart>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public class AFS {

    public class Data {
        String name {get;set;}
        public Integer data {get;set;}

        public Data(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }

    public List < Data > getChartData() {
        List < AggregateResult > arList = [SELECT Account.Name PT, MAX(Amount) sum FROM Opportunity GROUP BY Account.Name];
        List < Data > dataList = new List < Data > ();

        for (AggregateResult ar: arList) {
            String name = String.ValueOf(ar.get('PT'));
            Integer data = Integer.ValueOf(ar.get('sum'));
            dataList.add(new Data(name, data));
        }
        return dataList;

    }

}

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.