I currently have a Visual Force page that displays some charts using Apex. In my controller I have a "getAvgContribForMonth()" function that returns average of last 6 months. I pretty much want to make this value create a straight line across the chart to show the average. Here is what I have so far:

In my controller this is the avg function:

public void setAvgContribForMonth() {
    AggregateResult ch = [Select avg(ContribForMonth__c) Result from ChurchHist__c
                        where ChurchId__c = :ChurchId and 
                        EndOfMonth__c >= LAST_N_MONTHS:6];

    AvgContribForMonth = Double.valueOf( ch.get('Result') );


public Double getAvgContribForMonth() {
    return AvgContribForMonth;

In my page I have:

    <apex:pageBlock title="Church History: {!churchid} - {!churchname} : {!avgcontribformonth}">

    <apex:outputPanel id="out">

    <apex:pageBlockSection columns="1" collapsible="true" id="contribPerMonth">
    <apex:facet name="header">
        <span style="color:#000000">Contribution Per Month</span>
    <apex:chart data="{!data}" height="200" width="600">
        <apex:axis type="Numeric" position="left" fields="ContribForMonth__c" title="$ / Month" grid="true" minimum="0"/>
        <apex:axis type="Category" position="bottom" fields="MonthName__c" title="6 Month History"/>
        <apex:lineSeries title="Contributions by Month"  axis="left" xField="EndOfMonth__c" yField="ContribForMonth__c">
            <apex:chartTips height="20" width="120" labelField="MonthName__c" />
        <apex:lineSeries title="Avg" axis="left" xField="EndOfMonth__c" yField="{!avgcontribformonth}">

I'm assuming the second "lineSeries" in the chart is what is causing the problem since it's wanting yField to be a field in "data" instead of an other value from my class? I get:

java.lang.RuntimeException: java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String

when I try to view the page. If I take that line out it works. Also note that I display the average in the PageBlock title just as a test and it works fine there. I want to make this value be a line on the chart. Any ideas or suggestions? I wrote the the charts a few months ago before and didn't need this average at the time so I didn't really plan for it.

1 Answer 1


yField should contain the name of a field, rather than numeric data - i.e. a string, rather than a number. From the docs for yField in apex:lineSeries:

The field in each record provided in the chart data from which to retrieve the y-axis value for each data point in the series. This field must exist in every record in the chart data.

So, you need to add an avgContribForMonth field to each data item, and populate that with the average value, so you get a horizontal line across the chart (I'm guessing this is what you're looking for). You might need to add a wrapper class to make that work. The docs have an example.

  • 1
    I like that example and will try it tomorrow at work. So I'm basically creating a List of a class and in the class I will populate all the fields already defined and then the EXTRA field for the average in the class. Then use the class as the data source for the chart? Makes sense to me, since the actual object was just a list too. Will let you know how it goes but looks good.
    – Randy R
    Commented Jan 23, 2014 at 3:25
  • I am still working on getting my apex chart to work. Think I'm going to start over from scratch in sandbox and develop every chart with it's own wrapper class. For some reason I'm not getting any data back but I understand the concept of the example you posted. Thank you very much.
    – Randy R
    Commented Jan 24, 2014 at 3:16

You must log in to answer this question.

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