Chart Renderers

Chart renderers control the way reports display on your site. Currently, the only included renderer is for Google Charts, but more are on the way, and it’s easy to write one for your own favourite charting package.

Included Renderers

Basic Usage

Renderers are typically defined on your report objects. For example:

class MyReport(Report):
    renderer = GoogleChartsRenderer

    column_chart = charts.ColumnChart(title="Pony Populations", width="500")

    def get_data_for_column_chart(self):
        ...

You can also select renderers on a chart-by-chart basis. For example:

class MyReport(Report):
    renderer = GoogleChartsRenderer

    column_chart = charts.ColumnChart(title="Pony Populations", width="500")

    column_chart_other_renderer = charts.ColumnChart(
        title="Pony Populations", 
        width="500", 
        renderer=SomeOtherRenderer
    )

    def get_data_for_column_chart(self):
        ...

    def get_data_for_column_chart_other_renderer(self):
        ...

Talking to Your Renderer

Above and beyond the basic options described in the chart documentation, individual renderers usually provide a lot of unique customization options. You can set these by passing in a renderer_options dictionary to the chart. For example, for a red background using the Google Charts renderer:

class MyReport(Report):
    renderer = GoogleChartsRenderer

    column_chart = charts.ColumnChart(
        title="Pony Populations", 
        width="500",
        renderer_options={
            'backgroundColor': "#ff0000"
        }
    )

    def get_data_for_column_chart(self):
        ...

For information on the the various options available, refer to the documentation for your chosen renderer above.

Writing Your Own

A very simple stub of a chart renderer looks something like the following:

from report_tools.renderers import ChartRenderer


class MyChartRenderer(ChartRenderer):
    @classmethod
    def render_piechart(cls, chart_id, options, data, renderer_options):
        return "<div id='%s' class='placeholder'>Pie Chart</div>" % chart_id

    @classmethod
    def render_columnchart(cls, chart_id, options, data, renderer_options):
        return "<div id='%s' class='placeholder'>Column Chart</div>" % chart_id

    @classmethod
    def render_barchart(cls, chart_id, options, data, renderer_options):
        return "<div id='%s' class='placeholder'>Bar Chart</div>" % chart_id

    @classmethod
    def render_linechart(cls, chart_id, options, data, renderer_options):
        return "<div id='%s' class='placeholder'>Line Chart</div>" % chart_id

When a chart is rendered, it goes to the selected chart renderer class and tries to call an appropriate class method. This method will typically be named render_xxx where xxx is a lower case representation of the chart’s class name. All rendering methods take the same parameters:

chart_id
A unique identifier for the chart. Safe for use as an html element id.
options
If a chart accepts additional parameters, such as width, height or template, they will be loaded into this dictionary.
data
The data returned by the chart’s get_data_for_xxx method. This typically comes in as a ChartData object, so you’ll need to wrangle it into something your charting package can read.
renderer_options
The renderer options specified when the chart was defined on the report.