-
Notifications
You must be signed in to change notification settings - Fork 19
Rendering a Chart with AmCharts.rb
Note: The code demonstrated on this page is available as part of the compendium_demo project, which you can clone and play with yourself. This sample application gives a simple report outlining spending over a given period.
This section makes use of the amcharts.rb gem to render charts. In order to render a chart, you must first add the chart provider to your Gemfile:
gem 'compendium-amcharts'
If you have multiple chart providers installed, create an initializer to tell Compendium to use AmCharts:
Compendium.configure do |config|
config.chart_provider = :AmCharts
end
Charts are rendered by specifying what type of chart you wish to use (:serial
for line/column/bar charts, :pie
, :funnel
, :gauge
, :radar
, :xy
), and a setup block to specify options. See the amcharts.rb documentation for more information about how to configure your chart.
####Loading Remote Data See Remote Data Charts for information on how to set up a remote chart.
With AmCharts.rb, you can set up a remote chart which defers loading until requested, by adding the following to your setup block:
query.render_chart(self, :serial, remote: true) do |c|
data_source.defer!
end
A deferred chart will be created but will not update its data set until requested (in order to do so, you will need to get a reference to the AmCharts chart object) via Javascript:
chart = AmCharts.charts[0] // or get the chart some other way
chart.remoteProvider.load()
####Notes
- By default, the
amcharts
helper provided by amcharts.rb, which is used by Compendium to render a chart, adds the necessary javascript files and stylesheets, usingcontent_for
. If your chart isn't rendering, ensure that you haveyield :javascript
andyield :stylesheets
in your layout. - A chart will not render if a height and width is not defined. Either set
chart.dimensions
orchart.height
andchart.width
in your chart setup block. - See the AmCharts API documentation for more details about chart options.