0

I have created a VF chart to display a Line chart with 2 data series. Now I have written 2 differnt methods in order to fetch the data from 2 different unrelated objects. In order to call these 2 methods in data attribute of , I have created page reference method and called these 2 methods. Now when Im trying to call this page reference method in my VF page, its giving an error Unknown property 'MyController.mastermethod. Please suggest.

Controller-

 public PageReference masterMethod() {   
 getLineData();
 getLineData1();  
 return null;
}

//Method 1

 public List<LineChartData> getLineData() 
{  
    List<LineChartData> ldata = new List<LineChartData>();
    List<Profile__c> pv = new List<Profile__c>();        
    String sql = 'SELECT Name, Profile_Value__c, Date__c FROM Profile__c';
    pv = Database.Query(sql);
    for(Profile__c temp: pv)
    {
    if(temp.Profile_Value__c !=null && temp.Date__c !=null)       
    {           
        ldata.add(new LineChartData(temp.Date__c, temp.Profile_Value__c));           
    }
  }
    return ldata;  
}  

// Wrapper class  
public class LineChartData
{  
    public Date Date1 { get; set; }  
    public Decimal Profile { get; set; }        
    public LineChartData(Date Date1, Decimal Profile) 
    {  
        this.Date1 = Date1;  
        this.Profile = Profile;              
    }  
}



//Method 2
public List<LineChartData1> getLineData1() 
{  
    List<LineChartData1> ldata1 = new List<LineChartData1>();      
    List<Contact> cont1 = new List<Contact>();  


    String sql = 'SELECT id, Name, Date__c, Market_Value__c FROM Contact WHERE id= :selectedName';        
    cont1 = Database.Query(sql);
    for(Contact temp: cont1)
    {
    if(temp.Market_Value__c !=null && temp.Date__c !=null)       
    {           
        ldata1.add(new LineChartData1(temp.Date__c, temp.Market_Value__c));           
    }
  }
    return ldata1;  
}  

// Wrapper class  
public class LineChartData1
{  
    public Date Date2 { get; set; }         
    public Decimal MarketValue { get; set; }  

    public LineChartData1(Date Date2, Decimal MarketValue ) 
    {  
        this.Date2 = Date2;             
        this.MarketValue  = MarketValue ;  
    }  

VF Page-

<apex:outputPanel id="LineData"  style="width:100%; ">
<apex:pageblock id="Graph" title="Performance" >
    <apex:chart height="300" width="500" data="{!mastermethod}">       
        <apex:axis type="Numeric" position="left" fields="Profile,MarketValue"
        grid="true"/>            
        <apex:axis type="Category" position="bottom" fields="Date1">            
        </apex:axis>
        <apex:lineSeries axis="left" fill="true" xField="Date1" yField="Profile"
        markerType="cross" markerSize="4" markerFill="#FF0000"/>
        <apex:lineSeries axis="left" xField="Date1" yField="MarketValue"
        markerType="circle" markerSize="4" markerFill="#8E35EF"/>
        <apex:legend position="right"/>           
    </apex:chart>
</apex:pageblock>
</apex:outPutPanel>

1 Answer 1

1

The chart data attribute should reference a property that returns data not a method that returns null. The values for both series need to be represented by one object:

public class CombinedLineChartData implements Comparable {  
    public Date Datex { get; set; }  
    public Decimal Profile { get; set; }  
    public Decimal MarketValue { get; set; }
    public Integer compareTo(Object o) {
        CombinedLineChartData that = (CombinedLineChartData) o;
        if (that.Datex < this.Datex) return -1;
        else if (that.Datex> this.Datex) return 0;
        else return 0;
    }
}

and your logic in the property getter will need to merge the two queries into one list of objects:

public List<CombinedLineChartData> getCombinedLineData() {
    Map<Date, CombinedLineChartData> m = new Map<Date, CombinedLineChartData>();
    for (Profile__c p : [
           SELECT Date__c, Profile_Value__c
           FROM Profile__c
           WHERE Date__c != null
           AND Profile_Value__c != null
           ]) {
       CombinedLineChartData d = m.get(p.Date__c);
       if (d == null) {
           d = new CombinedLineChartData();
           d.Datex = p.Date__c;
           m.put(p.Date__c, d);
       }
       d.Profile = p.Profile_Value__c;
    }
    for (Contact c : [
           SELECT Date__c, Market_Value__c
           FROM Contact
           WHERE id= :selectedName
           AND Date__c != null
           AND Market_Value__c != null
           ]) {
       CombinedLineChartData d = m.get(c.Date__c);
       if (d == null) {
           d = new CombinedLineChartData();
           d.Datex = c.Date__c;
           m.put(c.Date__c, d);
       }
       d.MarketValue = c.Market_Value__c;
    }
    // Chart probably needs entries on order so sort
    List<CombinedLineChartData> l = m.values();
    l.sort();
    return l;
}

See Building a Complex Chart with Visualforce Charting.

4
  • Thanks for your inputs. I tried your suggestion, but its not working. Could you please help me with giving an idea on how to apply the same with my code. Commented Dec 12, 2014 at 11:08
  • @SalesforceLearner I've added code for how I would merge the data if that is the part you are asking about.
    – Keith C
    Commented Dec 12, 2014 at 12:12
  • Thanks for your valuable inputs. I tried your code, but it gave error. I removed others but got stuck at this one: "Variable does not exist: Date at line d.Date = p.Date__c;" Although this already has been defined. Commented Dec 16, 2014 at 9:28
  • @SalesforceLearner Yeah sorry about that. Try the code as posted now.
    – Keith C
    Commented Dec 16, 2014 at 10:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .