1

I have a visualforce chart that is a gauge. We query the database and populate results from the query into the values in the gauge. This seems to work well until there are no results. In the controller we do an if else to see if there is a value and if not give it 0. The users have asked that instead of 0s in the chart if we can display a message that tells the user there are no records.

Is it better to figure out if there are no values in the controller or the visualforce page?

My assumption is that we would want to create a try to see if the chart will render and then catch if it does not to display an output of text, however I cannot get the syntax correct.

visualforce page:

<apex:page controller="ISOGaugeControllerTeamCurMo">
try({!max2} == 0){
<apex:outputText value="There are no records" />
}
catch{
<apex:chart height="180" width="400" animate="true" data="{!data}" legend="false">
    <apex:axis type="Gauge" position="top" title="(In Thousands  $$$)" minimum="0" maximum="{!max2}" margin="-9"/>          
    <apex:gaugeSeries dataField="size" donut="50"/>
</apex:chart>
}

Controller:

public with sharing class ISOGaugeControllerTeamCurMo {
public Integer getMax2() {
    Decimal max1;
    List<AggregateResult> max =   [select sum(Goal__c) goal 
                            FROM ISO_Goal__c 
                            Where Goal__c != null 
                            AND (Goal_Date__c = THIS_MONTH OR Goal_Date__c = LAST_MONTH)
                            AND RecordType.Name = 'Individual Goal'
                            AND ISO_Manager__c = :UserInfo.getUserId()];

    if(max[0].get('goal') == null){
        max1 = 0;
        }
    else {
         max1 = (Decimal)max[0].get('goal');      
         }
    Integer max2 = (Integer)max1.divide(1000, 0);
    return max2;
    }
public List<gaugeData> getData() {
    List<gaugeData> data = new List<gaugeData>();
    List<AggregateResult> Actuals =   [select sum(amount__c) sum 
                                FROM Actuals__c 
                                Where Amount__c != null 
                                AND (Actual_Date__c = THIS_MONTH OR Actual_Date__c = LAST_MONTH)
                                AND ISO_Goal__r.RecordType.Name = 'Individual Goal'                                    
                                AND ISO_Goal__r.ISO_Manager__c = :UserInfo.getUserId()];

    if(actuals[0].get('sum') == null) {
        data.add(new gaugeData('Actual', 0));
        }     
        else {
             data.add(new gaugeData('Actual', (Integer)((Decimal)actuals[0].get('sum')).divide(1000, 0)));
             }
    return data;
}
// Wrapper class
public class gaugeData {

public String name { get; set; }
public Integer size { get; set; }

public gaugeData(String name, Integer data) {
    this.name = name;
    this.size = data;
    }
}   
}
3

Could you do something along the lines of the following in your VF page to fulfil the requirement, using the rendered attribute on components is a great way of conditionally showing elements on the page. Looking at the logic you supplied i would try the following:

<!-- Use the rendered="" attribute to conditionally show the error if your {!max2} variable is 0 --> 
<apex:outputText value="There are no records" rendered="{!max2 == 0}"/>

<!-- Now do the opposite side of this check within your chart's rendered="" attribute -->
<apex:chart height="180" width="400" animate="true" data="{!data}" legend="false"  rendered="{!max2 != 0}">
    <apex:axis type="Gauge" position="top" title="(In Thousands  $$$)" minimum="0" maximum="{!max2}" margin="-9"/>          
    <apex:gaugeSeries dataField="size" donut="50"/>
</apex:chart>
| improve this answer | |

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.