Newer Version Available

This content describes an older version of this product. View Latest

Refreshing Chart Data Using <apex:actionSupport>

Update a Visualforce chart in response to a user’s actions by adding the <apex:actionSupport> component to Visualforce user interface elements that affect the chart’s data.
The following markup displays a pie chart that can be updated by choosing a new year from a menu next to the chart:

This markup attaches a chart component to its data source by setting the chart’s data attribute to the Visualforce expression {!pieData}. The expression calls the getPieData() controller method, which returns the data. The chart is wrapped in an <apex:outputPanel> with an id attribute of theChart.

An <apex:form> component is used to submit a new year back to the page’s controller when the chart needs to be updated. The <apex:selectList> tag displays the years available to chart, and a child <apex:actionSupport> tag submits the form whenever the menu changes. The id of the chart’s <apex:outputPanel>, theChart, is used in the <apex:actionSupport> reRender attribute to limit updating to the chart, instead of reloading the whole page. Finally, an <apex:actionStatus> component provides a status message while the chart is refreshing. It’s easy to replace the minimal text message with an animated graphic or text effect.

PieChartRemoteController

The controller for this page is an expansion of the pie chart controller used in A Simple Charting Example.
This controller supports providing data to a Visualforce chart two different ways:
  • Using a Visualforce expression, {!pieData}, which calls the instance method getPieData().
  • Using JavaScript remoting, by calling the @RemoteAction static method getRemotePieData() from a JavaScript method.