Apex chart average line

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.


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.

Source : Link , Question Author : Randy R , Answer Author : metadaddy

Leave a Comment