0

I have a month field which stores the value of month as 1,2,3 etc. How can I convert this to Jan, Feb, March etc.

Following is my controller code:

public List<OpportunityData> getOpportunityData() {
Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;  
AggregateResult[] result = [SELECT Month__c, Sum(Rental_Revenue_Actual__c) Reve, Sum(Rental_Revenue_Goal__c) Gol FROM User_Stats__C where Year__c = '2013' and Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Actual__c != null and Rental_Revenue_Actual__c != 0 and Rental_Revenue_Goal__c != null and Rental_Revenue_Goal__c != 0 GROUP BY Month__c ORDER BY Month__c];

List<OpportunityData> oppsData = new List<OpportunityData>();
for (AggregateResult a : result)
{
    OpportunityData opp = new OpportunityData((String)a.get('Month__c'),
                                              (Double)a.get('Reve'),
                                              (Double)a.get('Gol'));    
    oppsData.add(opp);
}
oppsData.sort();
return oppsData;
}
public class OpportunityData implements Comparable
{
public Integer compareTo(Object o) {
    return month-((OpportunityData)o).month;
}
public Integer month { get; set; }
public Double Revenue { get; set; }
public Double Goal { get; set; }
public Double Attnmnt { get; set; }        
public OpportunityData(String mon, Double Reve, Double Gol)
{
    month = integer.valueof(mon);
    Revenue = Reve/1000;
    Goal = Gol/1000;
    Attnmnt = Reve / Gol * 100;            

}
public String getMonth() {
 return DateTime.newInstanceGMT(System.Date.newInstance(2000, month, 1), Time.newInstance(0, 0, 0, 0)).formatGMT('MMM');
}

VF page:

   <apex:chart height="350" width="700" data="{!OpportunityData}" background="#FFFFFF">
    <apex:legend position="right"/>
    <apex:axis type="Numeric" position="left" fields="Revenue,Goal" title="Revenue (In $ '000)" minimum="0"/>
    <apex:axis type="Category" position="bottom" fields="Month" title="Month of the Year">
        <apex:chartLabel rotate="315"/>
    </apex:axis>
    <apex:barSeries title="Actual, Goal" orientation="vertical" axis="left" xField="Month" yField="Revenue,Goal" colorset="#32CD32,#1E90FF" groupGutter="10">
        <apex:chartTips height="20" width="120"/>
    </apex:barSeries>
    <apex:axis type="Numeric" position="right" fields="Attnmnt" title="Attainment (%)" grid="true" minimum="0"/>
   <apex:lineSeries title="Attnmnt(%)" axis="right" xField="Month" yField="Attnmnt" fill="false" strokeColor="#FFD700" strokeWidth="2" Smooth="3" markerType="circle" markerSize="3" markerFill="#808080"/>
</apex:chart>

Please Help.

3

In your wrapper class, you can use:

public String getMonth() {
     return DateTime.newInstanceGMT(Date.newInstance(2000, month, 1), Time.newInstance(0, 0, 0, 0)).formatGMT('MMM'));
}

public class OpportunityData implements Comparable
{
public Integer compareTo(Object o) {
    return month-((OpportunityData)o).month;
}
public Integer month { get; set; }
public String monthString { get; set; }
public Double Revenue { get; set; }
public Double Goal { get; set; }
public Double Attnmnt { get; set; }        
public OpportunityData(String mon, Double Reve, Double Gol)
{   month = integer.valueof(mon);
    monthString = DateTime.newInstanceGMT(System.Date.newInstance(2000, month, 1), System.Time.newInstance(0, 0, 0, 0)).formatGMT('MMM');
    Revenue = Reve/1000;
    Goal = Gol/1000;
    Attnmnt = Reve / Gol * 100;            

}
| improve this answer | |
  • Thanks for the reply. I added the snippet to the wrapper class. It now gives an error: Compile Error: Variable does not exist: Date. Sorry if I am missing something very small. Please help. – Robin Jan 15 '14 at 12:30
  • 1
    You may have a class or variable called Date that's conflicting with the system libraries. This code works perfectly fine in my developer edition (I tested this before I posted). That said, you can change Date to System.Date and DateTime to System.DateTime to remove the ambiguity. – sfdcfox Jan 15 '14 at 12:38
  • I have edited my code with your code. Its not giving an error now, but when I use it in the chart, it does not populate the chart. I have also added the chart code in VF page for your reference – Robin Jan 15 '14 at 13:26
  • @Robin Throwing it out into a chart means you need to adjust your code a bit... – sfdcfox Jan 15 '14 at 13:53
  • Great! so at least I did not get it wrong. I am sure the change would be minor. Can you please help me figure out the modification that needs to be done? Thanks – Robin Jan 15 '14 at 13:57

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