+ Start a Discussion
jaganjagan 

Stacked bar chart using visual force charting

Hi, 

 

Can anyone help me on creating a stacked bar chart which shows count of leads captured from each lead source for particular date field ..say created date. 

 

I some how used wrapper class and arranged the data but here i am using static lead source values. We want to make this dynamic so that whenever a value is added in lead source we do not have to add the code for that value.

 

Please let me know if any one worked on this in any other way.

 

Thanks,

jagan

 

 

cwall_sfdccwall_sfdc

Show us source?... what you've done?

 

Could you do a SOQL statement w/ COUNT returning a List<SObjects>?  No wrapper.

jaganjagan

Below is the  part of VF code and controller i am using now.. 

 

<apex:chart height="350" width="700" data="{!ChartData}">
       <apex:axis type="Numeric" position="left" fields="Email,Phone,link_to_property,email_corporate"  title="Sum of Leads" grid="false" steps="10.0"/>
       <apex:legend position="right"/>
       <apex:axis type="Category" position="bottom" fields="name"  title="As of Date"/>
       <apex:barSeries orientation="vertical" axis="bottom" xField="name" yField="Email,Phone,link_to_property,email_corporate" stacked="true" showInLegend="true">
       <apex:chartTips height="20" width="120"/>  </apex:barSeries>
  </apex:chart>

 public static List<Data> getChartData() {
     List<Lead> accMetrics = new list<Lead>();


        List<Data> data = new List<Data>();
        Date lastdt = system.today() - 180;
        accMetrics =[select id,As_of_date__c,LeadSource,leads__c from Lead where as_of_date__c >:lastdt order by As_of_date__c];


        if(accMetrics.size()>0){
        Map<String,Integer> phonemap = new Map<String,Integer>();
        Map<String,Integer> emailmap = new Map<String,Integer>();
        Map<String,Integer> emailcorpmap = new Map<String,Integer>();
        Map<String,Integer> othermap = new Map<String,Integer>();
       
        	
        for(Lead p:accMetrics){


            String dt;
			Date tod = p.as_of_date__c;


			if(tod<>null)
			dt = ''+tod.year()+'/'+tod.month()+'/'+tod.day();
            Integer ttl=(integer) p.leads__c;
            if(ttl==null)
            ttl=0;

            if(p.LeadSource == 'Email/Fax Leads'){
                if(emailmap.containsKey(dt))
                emailmap.put(dt,emailmap.get(dt)+ttl);
                else {
                emailmap.put(dt,ttl);
                }
            }

            if(p.LeadSource == 'Email Lead to Corporate Property'){
                if(emailcorpmap.containsKey(dt))
                emailcorpmap.put(dt,emailcorpmap.get(dt)+ttl);
                else {
                emailcorpmap.put(dt,ttl);
                }
            }
            
            if(p.LeadSource == 'Toll-Free Leads'){
                if(phonemap.containsKey(dt))
                phonemap.put(dt,phonemap.get(dt)+ttl);
                else {
                phonemap.put(dt,ttl);
                }
            }
            if(p.LeadSource == 'Text4Info'){
                if(othermap.containsKey(dt))
                othermap.put(dt,othermap.get(dt)+ttl);
                else {
                othermap.put(dt,ttl);
                }
            }
            
       }

       Set<String> dateset = new Set<String>();
      
        for(Lead pm:accMetrics){
			String dt;
			Date tod = pm.as_of_date__c;
			if(tod<>null)
			dt = ''+tod.year()+'/'+tod.month()+'/'+tod.day();
            if(!dateset.contains(dt)){
            dateset.add(dt);
            Data d = new Data();
                d.name = dt;
                if(emailmap.containsKey(dt))
                d.Email=emailmap.get(dt);
                else 
                d.Email=0;

                if(emailcorpmap.containsKey(dt))
                d.email_corporate=emailcorpmap.get(dt);
                else 
                d.email_corporate=0;

                if(phonemap.containsKey(dt))
                d.Phone=phonemap.get(dt);
                else 
                d.Phone=0;

                if(othermap.containsKey(dt))
                d.link_to_property=othermap.get(dt);
                else
                d.link_to_property=0;
                
				d.totalcount = d.Email+ d.email_corporate+d.Phone+d.link_to_property;

				data.add(d);


            }
        }


        }
        else{

            ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.INFO, 'There is no data for the chart to display.' );
            ApexPages.addMessage(msg);  
            return null;       
        }
       system.debug(data);


        return data;
    }


    // Wrapper class 
    


    public class Data {
        public String name { get; set; }
        public Integer Email { get; set; }
        public Integer email_corporate { get; set; }
        public Integer Phone { get; set; }
        public Integer link_to_property { get; set; }
        

        public Integer totalcount {get;set;}
        public Data() {

        }
    }

 Its pretty raw way actually. I tried using soql grouping by lead source and date. But i was not able to get the chart in the page :( :(