+ Start a Discussion
Maxime SavartMaxime Savart 

Developping a Radar Chart to display it on account layout page

Hi there, 

This is my first post on Salesforce developpers forum.
I have a special request for you since I'm a beginner on visualforce developpment.

I made an account scoring based on 5 fields (criterias) :
PotentielCa__c 
CapaciteDecision__c
RayonnementMarque__c
ConnaissanceMp__c
CompatibiliteOperationnelleDiatly__c

I would like to make a radar chart based on those 5 fields on the account object.

Could you help me? :)

Thanks a lot
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Maxime,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

First, you have to create Field Set (Available in classic only) and include your 5 fields in it.

Then, create a Visualforce Page with Standard Controller and extension. To embed a Visualforce page into page layout, the Visualforce page should be using the standard controller tag referencing the same entity that will be used in the page layout. You can use or add this Visualforce page by modifying the page layout.

Here, Field Set name is RadarSet

Visualforce:
<apex:page standardController="Account" extensions="RadarC">  
    <style>
        #vfext4-ext-gen1026 {
        width:800px !important;
        }
    </style>  
    
    <apex:chart name="myChart" height="600" width="650" legend="false" data="{!data}">
        <apex:legend position="left" />
        <apex:axis type="Radial" position="radial"/>
        <apex:radarSeries title="7 Ps" xField="field" yField="value" tips="true" opacity="0.4"/>
    </apex:chart>
</apex:page>

Controller:
public class RadarC {
    
    public List<Map<Object,Object>> data = new List<Map<Object,Object>>();
    public String acctId {get;set;}
    
    public RadarC(ApexPages.StandardController controller){
        acctId = controller.getRecord().Id ;
    }
    
    public List<Schema.FieldSetMember> getFields() {
        return SObjectType.Account.FieldSets.RadarSet.getFields();
    }
    
    public List<Map<Object,Object>> getData() {
        String query = 'SELECT ';
        List<String> fieldNames = new List<String>();
        
        for(Schema.FieldSetMember f : getFields()){
            query += f.getFieldPath() + ', ';
            fieldNames.add(f.getFieldPath());
        }
        query += 'Id, Name FROM Account where Id=\'' + acctId + '\' LIMIT 1';
        
        SObject myFieldResults = Database.Query(query);
        Schema.DescribeSObjectResult R = myFieldResults.getSObjectType().getDescribe();
        Map<String, Schema.SObjectField> fieldMap = R.fields.getmap();
        
        //creates a map of labels and api names
        Map<String,String> labelNameMap = new Map<String,String>();
        for(String key : fieldMap.keySet()){
            labelNameMap.put(fieldMap.get(key).getDescribe().getName(), fieldMap.get(key).getDescribe().getlabel());
        }
        
        //creates a map of labels and values
        for(String f : fieldNames){
            String fieldLabel = labelNameMap.get(f);
            String fieldValue = String.valueOf(myFieldResults.get(f));
            
            Map<Object, Object> m = new Map<Object,Object>();
            m.put('field', fieldLabel);
            m.put('value', fieldValue);
            data.add(m);
        }
        
        return data;
    }
}

Reference: ​https://github.com/developerforce/Visualforce-Charting-Examples/tree/master/src%20files​


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas