Documentation

This Django app enables you to configure and render Chart.JS charts directly from your Django codebase. These charts can be rendered directly in your html templates, or served asynchronously on a Django url.

Installation

install django-jchart

pip install django-jchart

Add django-chart to your installed apps.

INSTALLED_APPS = (
    '...',
    'jchart',
)

Enable template loading from app folders by adding the following property to your TEMPLATES django configuration:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        # ...
    }]

Frontend Dependencies

For the charts to be rendered inside the browser you will need to include the Chart.JS library. Add the following HTML before your closing </body> tag:

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js"></script>

If you want to make use of asynchronous loading charts you will also need to include jQuery:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

The Chart Class

At the heart of this charting library lies the Chart class. This class describes a chart and defines which data it should display. The chart's 'class fields' map to Chart.JS options which describe how the chart should render and behave. By overriding the get_datasets method on your Chart instance you can define which data should be displayed.

To define which type of chart you want to render (e.g. a line or bar chart), your chart class should set its class field chart_type to one of "line", "bar", "radar", "polarArea", "pie", or "bubble". A chart class without this field is invalid and initialization will result in an ImproperlyConfigured exception.

from jchart import Chart

class LineChart(Chart):
    chart_type = 'line'
get_datasets

The get_datasets method should return a list of datasets this chart should display. Where a dataset is a dictionary with key/value configuration pairs (see the Chart.JS documentation).

from jchart import Chart

class LineChart(Chart):
    chart_type = 'line'

    def get_datasets(self, **kwargs):
        return [{
            'label': "My Dataset",
            'data': [69, 30, 45, 60, 55]
        }]
get_labels

This method allows you to set the Chart.JS data.labels parameter. Which allows you to configure categorical axes. For an example on how to use this feature see this pie chart.

from jchart import Chart

class PieChart(Chart):
    chart_type = 'pie'

    def get_labels(self, **kwargs):
        return ['Red', 'Blue']

Configuring Charts

A chart can be configured through the following class fields:

scales layout title legend tooltips hover animation elements responsive

All of these fields map to the same key in the Chart.JS 'options' object. For instance, if you wanted to create a chart that does not render responsively you would set the responsive class field to false:

from jchart import Chart

class UnresponsiveLineChart(Chart):
    chart_type = 'line'
    responsive = False
    # ...

Most of these class fields require either a list of dicitonaries or a dictionary. With the exception of responsive which should be a boolean value. Be sure to read the Chart.JS documentation on how to use these configuration options.

For your convenience there are some methods located in jchart.config which can be used to produce correct dictionaries to configure Chart.JS properties. Most of these methods only serve as a validation step for your input configuration but some can also transform their input. Let's take a look at an example, how would you configure the X-Axis so it is not to be displayed:

from jchart import Chart
from jchart.config import Axes

class LineChart(Chart):
    chart_type = 'line'
    scales = {
        'xAxes': [Axes(display=False)],
    }

jchart.config also contains a method to create dataset configuration dictionaries. One of the advantages of using this method is that it includes a special property color which can be used to automatically set the values for: 'backgroundColor', 'pointBackgroundColor', 'borderColor', 'pointBorderColor', and 'pointStrokeColor'.

from jchart import Chart
from jchart.config import Axes

class LineChart(Chart):
    chart_type = 'line'
    
    def get_datasets(self, **kwargs):
        return [DataSet(color=(255, 255, 255), data=[])]

The jchart.config module contains methods for the properties listed below. Keep in mind that you are in no way obligated to use these methods, you could also supply Python dictionaries in the place of these method calls.

Available configuration methods:
Axes, ScaleLabel, Tick, DataSet, Tooltips, Legend, LegendLabel, Title, Hover, InteractionModes, Animation, Element, ElementArc, ElementLine, ElementPoint, ElementRectangle

Rendering Charts

Chart instances can be passed to your Django template context. Inside the template you can invoke the method `as_html` on the chart instance to render the chart.

# LineChart is a class inheriting from jchart.Chart

def some_view(request):
    render(request, 'template.html', {
        'line_chart': LineChart(),
    })

The following code is an example of how to render this line chart inside your html template:

{{ line_chart.as_html }}

Asynchronous Charts

When rendering the chart directly into your HTML template, all the data needed for the chart is transmitted on the page's HTTP request. It is also possible to load the chart (and its required data) asynchronous.

To do this we need to setup a url endpoint from which to load the chart's data. There is a classmethod available on jchart.views.ChartView to automatically create a view which exposes the chart's configuration data as JSON on a HTTP get request:

from jchart.views import ChartView

# LineChart is a class inheriting from jchart.Chart
line_chart = LineChart()

urlpatterns = [
    url(r'^charts/line_chart/$', ChartView.from_chart(line_chart), name='line_chart'),
]

You can use a custom templatetag inside your Django template to load this chart asynchronously. The custom tag behaves like the Django url templatetag and any positional or keyword arguments supplied to it are used to resolve the url for the given url name. In this example the url does not require any url parameters to be resolved:

{% load jchart %}

{% render_chart 'line_chart' %}

This tag will be expanded into the following JS/HTML code:

<canvas class="chart" id="unique-chart-id">
</canvas>

<script type="text/javascript">
window.addEventListener("DOMContentLoaded", function() {
    $.get('/charts/line_chart/', function(configuration) {
        var ctx = document.getElementById("unique-chart-id").getContext("2d");    

        new Chart(ctx, configuration);
    });
});
</script>

Chart Parameterization

It can often be useful to reuse the same chart for different datasets. This can either be done by subclassing an existing chart class and overriding its get_datasets method. But there is another way to do this. Any arguments given to the as_html method are supplied to your get_datasets method. This makes it possible to parameterize the output of get_datasets

Let's have a look at an example. Imagine we have price point data stored in a model called Price and this model has a field called currency_type. We could render the chart for different currency types by accepting the value for this field as a parameter to get_datasets.

from jchart import Chart
from core.models import Price

class PriceChart(Chart):
    chart_type = 'line'

    def get_datasets(self, currency_type):
        prices = Price.objects.filter(currency_type=currency_type)

        data = [{'x': price.date, 'y': price.point} for price in prices]

        return [DataSet(data=data)]

If we supply an instance of this chart to the context of our template, we could use this to render two different charts:

{{ price_chart.as_html 'euro' }}

{{ price_chart.as_html 'dollar' }}

For asynchronous charts any url parameters are passed to the get_datasets method.

from jchart.views import ChartView
from .charts import PriceChart

price_chart = PriceChart()

urlpatterns = [
    url(r'^currency_chart/(?P<>\w+)/$',
        ChartView.from_chart(price_chart))
]

To render this chart asynchronously we have to supply the url parameter as a second argument to the render_chart template tag, like so:

{% load jchart %}

{% render_chart 'price_chart' 'euro' %}

{% render_chart 'price_chart' 'dollar' %}

Example Charts

Time Series Chart

An example of a plot using time series data, where the X axis represents the time and the Y axis is any arbitrary value.

Code used for this example:

#charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class TimeSeriesChart(Chart):
    chart_type = 'line'
    scales = {
        'xAxes': [Axes(type='time', position='bottom')],
    }

    def get_datasets(self, **kwargs):
        data = [{'y': 0, 'x': '2017-01-02T00:00:00'}, {'y': 1, 'x': '2017-01-03T00:00:00'}, {'y': 4, 'x': '2017-01-04T00:00:00'}, {'y': 9, 'x': '2017-01-05T00:00:00'}, {'y': 16, 'x': '2017-01-06T00:00:00'}, {'y': 25, 'x': '2017-01-07T00:00:00'}, {'y': 36, 'x': '2017-01-08T00:00:00'}, {'y': 49, 'x': '2017-01-09T00:00:00'}, {'y': 64, 'x': '2017-01-10T00:00:00'}, {'y': 81, 'x': '2017-01-11T00:00:00'}, {'y': 100, 'x': '2017-01-12T00:00:00'}, {'y': 121, 'x': '2017-01-13T00:00:00'}, {'y': 144, 'x': '2017-01-14T00:00:00'}, {'y': 169, 'x': '2017-01-15T00:00:00'}, {'y': 196, 'x': '2017-01-16T00:00:00'}, {'y': 225, 'x': '2017-01-17T00:00:00'}, {'y': 256, 'x': '2017-01-18T00:00:00'}, {'y': 289, 'x': '2017-01-19T00:00:00'}, {'y': 324, 'x': '2017-01-20T00:00:00'}, {'y': 361, 'x': '2017-01-21T00:00:00'}, {'y': 400, 'x': '2017-01-22T00:00:00'}, {'y': 441, 'x': '2017-01-23T00:00:00'}, {'y': 484, 'x': '2017-01-24T00:00:00'}, {'y': 529, 'x': '2017-01-25T00:00:00'}, {'y': 576, 'x': '2017-01-26T00:00:00'}, {'y': 625, 'x': '2017-01-27T00:00:00'}, {'y': 676, 'x': '2017-01-28T00:00:00'}, {'y': 729, 'x': '2017-01-29T00:00:00'}, {'y': 784, 'x': '2017-01-30T00:00:00'}, {'y': 841, 'x': '2017-01-31T00:00:00'}, {'y': 900, 'x': '2017-02-01T00:00:00'}]

        return [DataSet(
            type='line',
            label='Time Series',
            data=data,
        )]

Scatter & Line Chart

An example of a scatter plot combined with a line chart. Such a plot could for instance be used to visualize the moving average of scattered data.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class ScatterLineChart(Chart):
    chart_type = 'line'
    scales = {
        'xAxes': [Axes(type='time', position='bottom')],
    }

    def get_datasets(self, **kwargs):
        data_scatter = [{'y': 24, 'x': '2017-01-01T21:00:00'}, {'y': 1, 'x': '2017-01-02T03:00:00'}, {'y': 7, 'x': '2017-01-02T14:00:00'}, {'y': 7, 'x': '2017-01-03T08:00:00'}, {'y': 13, 'x': '2017-01-04T00:00:00'}, {'y': 7, 'x': '2017-01-04T07:00:00'}, {'y': 19, 'x': '2017-01-05T01:00:00'}, {'y': 18, 'x': '2017-01-05T15:00:00'}, {'y': 14, 'x': '2017-01-06T00:00:00'}, {'y': 2, 'x': '2017-01-06T07:00:00'}, {'y': 18, 'x': '2017-01-07T06:00:00'}, {'y': 4, 'x': '2017-01-07T07:00:00'}, {'y': 21, 'x': '2017-01-07T21:00:00'}, {'y': 5, 'x': '2017-01-08T00:00:00'}, {'y': 16, 'x': '2017-01-08T07:00:00'}, {'y': 14, 'x': '2017-01-08T11:00:00'}, {'y': 21, 'x': '2017-01-09T04:00:00'}, {'y': 25, 'x': '2017-01-09T20:00:00'}, {'y': 9, 'x': '2017-01-10T15:00:00'}, {'y': 25, 'x': '2017-01-11T10:00:00'}, {'y': 17, 'x': '2017-01-11T17:00:00'}, {'y': 10, 'x': '2017-01-12T11:00:00'}, {'y': 7, 'x': '2017-01-12T17:00:00'}, {'y': 11, 'x': '2017-01-12T22:00:00'}, {'y': 2, 'x': '2017-01-13T04:00:00'}, {'y': 13, 'x': '2017-01-13T12:00:00'}, {'y': 12, 'x': '2017-01-14T12:00:00'}, {'y': 16, 'x': '2017-01-15T10:00:00'}, {'y': 15, 'x': '2017-01-16T00:00:00'}, {'y': 23, 'x': '2017-01-16T17:00:00'}, {'y': 15, 'x': '2017-01-17T02:00:00'}, {'y': 22, 'x': '2017-01-17T12:00:00'}, {'y': 18, 'x': '2017-01-17T15:00:00'}, {'y': 16, 'x': '2017-01-18T14:00:00'}, {'y': 7, 'x': '2017-01-19T09:00:00'}, {'y': 10, 'x': '2017-01-20T02:00:00'}, {'y': 7, 'x': '2017-01-20T13:00:00'}, {'y': 5, 'x': '2017-01-20T17:00:00'}, {'y': 15, 'x': '2017-01-20T20:00:00'}, {'y': 5, 'x': '2017-01-21T06:00:00'}, {'y': 13, 'x': '2017-01-21T18:00:00'}, {'y': 20, 'x': '2017-01-22T13:00:00'}, {'y': 20, 'x': '2017-01-22T16:00:00'}, {'y': 23, 'x': '2017-01-23T15:00:00'}, {'y': 3, 'x': '2017-01-23T20:00:00'}, {'y': 20, 'x': '2017-01-24T15:00:00'}, {'y': 19, 'x': '2017-01-24T16:00:00'}, {'y': 1, 'x': '2017-01-25T00:00:00'}, {'y': 3, 'x': '2017-01-25T02:00:00'}, {'y': 22, 'x': '2017-01-25T23:00:00'}, {'y': 6, 'x': '2017-01-26T19:00:00'}, {'y': 17, 'x': '2017-01-27T10:00:00'}, {'y': 7, 'x': '2017-01-28T09:00:00'}, {'y': 23, 'x': '2017-01-29T05:00:00'}, {'y': 19, 'x': '2017-01-29T17:00:00'}, {'y': 16, 'x': '2017-01-30T08:00:00'}, {'y': 19, 'x': '2017-01-30T09:00:00'}, {'y': 23, 'x': '2017-01-31T06:00:00'}, {'y': 18, 'x': '2017-02-01T05:00:00'}]
        data_line = [{'y': 20, 'x': '2017-01-02T00:00:00'}, {'y': 3, 'x': '2017-01-03T00:00:00'}, {'y': 2, 'x': '2017-01-04T00:00:00'}, {'y': 18, 'x': '2017-01-05T00:00:00'}, {'y': 19, 'x': '2017-01-06T00:00:00'}, {'y': 20, 'x': '2017-01-07T00:00:00'}, {'y': 5, 'x': '2017-01-08T00:00:00'}, {'y': 23, 'x': '2017-01-09T00:00:00'}, {'y': 18, 'x': '2017-01-10T00:00:00'}, {'y': 5, 'x': '2017-01-11T00:00:00'}, {'y': 6, 'x': '2017-01-12T00:00:00'}, {'y': 2, 'x': '2017-01-13T00:00:00'}, {'y': 23, 'x': '2017-01-14T00:00:00'}, {'y': 3, 'x': '2017-01-15T00:00:00'}, {'y': 24, 'x': '2017-01-16T00:00:00'}, {'y': 10, 'x': '2017-01-17T00:00:00'}, {'y': 9, 'x': '2017-01-18T00:00:00'}, {'y': 11, 'x': '2017-01-19T00:00:00'}, {'y': 10, 'x': '2017-01-20T00:00:00'}, {'y': 2, 'x': '2017-01-21T00:00:00'}, {'y': 16, 'x': '2017-01-22T00:00:00'}, {'y': 24, 'x': '2017-01-23T00:00:00'}, {'y': 3, 'x': '2017-01-24T00:00:00'}, {'y': 13, 'x': '2017-01-25T00:00:00'}, {'y': 7, 'x': '2017-01-26T00:00:00'}, {'y': 10, 'x': '2017-01-27T00:00:00'}, {'y': 7, 'x': '2017-01-28T00:00:00'}, {'y': 13, 'x': '2017-01-29T00:00:00'}, {'y': 1, 'x': '2017-01-30T00:00:00'}, {'y': 10, 'x': '2017-01-31T00:00:00'}, {'y': 7, 'x': '2017-02-01T00:00:00'}]

        return [
            DataSet(type='line',
                    label='Scatter',
                    showLine=False,
                    data=data_scatter),
            DataSet(type='line',
                    label='Line',
                    borderColor='red',
                    data=data_line)
        ]

Bar Chart

A bar chart using one dataset.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class BarChart(Chart):
    chart_type = 'bar'

    def get_labels(self, **kwargs):
        return ["January", "February", "March", "April",
                "May", "June", "July"]

    def get_datasets(self, **kwargs):
        data = [10, 15, 29, 30, 5, 10, 22]
        colors = [
            rgba(255, 99, 132, 0.2),
            rgba(54, 162, 235, 0.2),
            rgba(255, 206, 86, 0.2),
            rgba(75, 192, 192, 0.2),
            rgba(153, 102, 255, 0.2),
            rgba(255, 159, 64, 0.2)
        ]

        return [DataSet(label='Bar Chart',
                        data=data,
                        borderWidth=1,
                        backgroundColor=colors,
                        borderColor=colors)]

Radar Chart

A line chart is a way of plotting data points on a line. Often, it is used to show trend data, and the comparison of two data sets.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class RadarChart(Chart):
    chart_type = 'radar'

    def get_labels(self):
        return ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"]

    def get_datasets(self, **kwargs):
        return [DataSet(label="My First dataset",
                        color=(179, 181, 198),
                        data=[65, 59, 90, 81, 56, 55, 40]),
                DataSet(label="My Second dataset",
                        color=(255, 99, 132),
                        data=[28, 48, 40, 19, 96, 27, 100])
                ]

Polar Chart

A line chart is a way of plotting data points on a line. Often, it is used to show trend data, and the comparison of two data sets.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class PolarChart(Chart):
    chart_type = 'polarArea'

    def get_labels(self, **kwargs):
        return ["Red", "Green", "Yellow", "Grey", "Blue"]

    def get_datasets(self, **kwargs):
        return [DataSet(label="My DataSet",
                        data=[11, 16, 7, 3, 14],
                        backgroundColor=[
                            "#FF6384",
                            "#4BC0C0",
                            "#FFCE56",
                            "#E7E9ED",
                            "#36A2EB"
                        ])
                ]

Pie Chart

A line chart is a way of plotting data points on a line. Often, it is used to show trend data, and the comparison of two data sets.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class PieChart(Chart):
    chart_type = 'pie'

    def get_labels(self, **kwargs):
        return ["Red", "Blue", "Yellow"]

    def get_datasets(self, **kwargs):
        data = [300, 50, 100]
        colors = [
            "#FF6384",
            "#36A2EB",
            "#FFCE56"
        ]
        return [DataSet(data=data,
                        label="My Pie Data",
                        backgroundColor=colors,
                        hoverBackgroundColor=colors)]

Bubble Chart

A line chart is a way of plotting data points on a line. Often, it is used to show trend data, and the comparison of two data sets.

Code:

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class BubbleChart(Chart):
    chart_type = 'bubble'

    def get_datasets(self, **kwargs):
        data = [{
            'x': randint(1, 10),
            'y': randint(1, 25),
            'r': randint(1, 10),
        } for i in range(25)]

        return [DataSet(label="First DataSet",
                        data=data,
                        backgroundColor='#FF6384',
                        hoverBackgroundColor='#FF6384')]

Example Charts Async

Time Series Chart

#charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class TimeSeriesChart(Chart):
    chart_type = 'line'
    scales = {
        'xAxes': [Axes(type='time', position='bottom')],
    }

    def get_datasets(self, **kwargs):
        data = [{'y': 0, 'x': '2017-01-02T00:00:00'}, {'y': 1, 'x': '2017-01-03T00:00:00'}, {'y': 4, 'x': '2017-01-04T00:00:00'}, {'y': 9, 'x': '2017-01-05T00:00:00'}, {'y': 16, 'x': '2017-01-06T00:00:00'}, {'y': 25, 'x': '2017-01-07T00:00:00'}, {'y': 36, 'x': '2017-01-08T00:00:00'}, {'y': 49, 'x': '2017-01-09T00:00:00'}, {'y': 64, 'x': '2017-01-10T00:00:00'}, {'y': 81, 'x': '2017-01-11T00:00:00'}, {'y': 100, 'x': '2017-01-12T00:00:00'}, {'y': 121, 'x': '2017-01-13T00:00:00'}, {'y': 144, 'x': '2017-01-14T00:00:00'}, {'y': 169, 'x': '2017-01-15T00:00:00'}, {'y': 196, 'x': '2017-01-16T00:00:00'}, {'y': 225, 'x': '2017-01-17T00:00:00'}, {'y': 256, 'x': '2017-01-18T00:00:00'}, {'y': 289, 'x': '2017-01-19T00:00:00'}, {'y': 324, 'x': '2017-01-20T00:00:00'}, {'y': 361, 'x': '2017-01-21T00:00:00'}, {'y': 400, 'x': '2017-01-22T00:00:00'}, {'y': 441, 'x': '2017-01-23T00:00:00'}, {'y': 484, 'x': '2017-01-24T00:00:00'}, {'y': 529, 'x': '2017-01-25T00:00:00'}, {'y': 576, 'x': '2017-01-26T00:00:00'}, {'y': 625, 'x': '2017-01-27T00:00:00'}, {'y': 676, 'x': '2017-01-28T00:00:00'}, {'y': 729, 'x': '2017-01-29T00:00:00'}, {'y': 784, 'x': '2017-01-30T00:00:00'}, {'y': 841, 'x': '2017-01-31T00:00:00'}, {'y': 900, 'x': '2017-02-01T00:00:00'}]

        return [DataSet(
            type='line',
            label='Time Series',
            data=data,
        )]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/time_series_chart/$',
        ChartView.from_chart(charts.TimeSeriesChart()),
        name='time_series_chart')
]

# Template.html
{% render_chart 'time_series_chart' %}

Scatter & Line Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class ScatterLineChart(Chart):
    chart_type = 'line'
    scales = {
        'xAxes': [Axes(type='time', position='bottom')],
    }

    def get_datasets(self, **kwargs):
        data_scatter = [{'y': 24, 'x': '2017-01-01T21:00:00'}, {'y': 1, 'x': '2017-01-02T03:00:00'}, {'y': 7, 'x': '2017-01-02T14:00:00'}, {'y': 7, 'x': '2017-01-03T08:00:00'}, {'y': 13, 'x': '2017-01-04T00:00:00'}, {'y': 7, 'x': '2017-01-04T07:00:00'}, {'y': 19, 'x': '2017-01-05T01:00:00'}, {'y': 18, 'x': '2017-01-05T15:00:00'}, {'y': 14, 'x': '2017-01-06T00:00:00'}, {'y': 2, 'x': '2017-01-06T07:00:00'}, {'y': 18, 'x': '2017-01-07T06:00:00'}, {'y': 4, 'x': '2017-01-07T07:00:00'}, {'y': 21, 'x': '2017-01-07T21:00:00'}, {'y': 5, 'x': '2017-01-08T00:00:00'}, {'y': 16, 'x': '2017-01-08T07:00:00'}, {'y': 14, 'x': '2017-01-08T11:00:00'}, {'y': 21, 'x': '2017-01-09T04:00:00'}, {'y': 25, 'x': '2017-01-09T20:00:00'}, {'y': 9, 'x': '2017-01-10T15:00:00'}, {'y': 25, 'x': '2017-01-11T10:00:00'}, {'y': 17, 'x': '2017-01-11T17:00:00'}, {'y': 10, 'x': '2017-01-12T11:00:00'}, {'y': 7, 'x': '2017-01-12T17:00:00'}, {'y': 11, 'x': '2017-01-12T22:00:00'}, {'y': 2, 'x': '2017-01-13T04:00:00'}, {'y': 13, 'x': '2017-01-13T12:00:00'}, {'y': 12, 'x': '2017-01-14T12:00:00'}, {'y': 16, 'x': '2017-01-15T10:00:00'}, {'y': 15, 'x': '2017-01-16T00:00:00'}, {'y': 23, 'x': '2017-01-16T17:00:00'}, {'y': 15, 'x': '2017-01-17T02:00:00'}, {'y': 22, 'x': '2017-01-17T12:00:00'}, {'y': 18, 'x': '2017-01-17T15:00:00'}, {'y': 16, 'x': '2017-01-18T14:00:00'}, {'y': 7, 'x': '2017-01-19T09:00:00'}, {'y': 10, 'x': '2017-01-20T02:00:00'}, {'y': 7, 'x': '2017-01-20T13:00:00'}, {'y': 5, 'x': '2017-01-20T17:00:00'}, {'y': 15, 'x': '2017-01-20T20:00:00'}, {'y': 5, 'x': '2017-01-21T06:00:00'}, {'y': 13, 'x': '2017-01-21T18:00:00'}, {'y': 20, 'x': '2017-01-22T13:00:00'}, {'y': 20, 'x': '2017-01-22T16:00:00'}, {'y': 23, 'x': '2017-01-23T15:00:00'}, {'y': 3, 'x': '2017-01-23T20:00:00'}, {'y': 20, 'x': '2017-01-24T15:00:00'}, {'y': 19, 'x': '2017-01-24T16:00:00'}, {'y': 1, 'x': '2017-01-25T00:00:00'}, {'y': 3, 'x': '2017-01-25T02:00:00'}, {'y': 22, 'x': '2017-01-25T23:00:00'}, {'y': 6, 'x': '2017-01-26T19:00:00'}, {'y': 17, 'x': '2017-01-27T10:00:00'}, {'y': 7, 'x': '2017-01-28T09:00:00'}, {'y': 23, 'x': '2017-01-29T05:00:00'}, {'y': 19, 'x': '2017-01-29T17:00:00'}, {'y': 16, 'x': '2017-01-30T08:00:00'}, {'y': 19, 'x': '2017-01-30T09:00:00'}, {'y': 23, 'x': '2017-01-31T06:00:00'}, {'y': 18, 'x': '2017-02-01T05:00:00'}]
        data_line = [{'y': 20, 'x': '2017-01-02T00:00:00'}, {'y': 3, 'x': '2017-01-03T00:00:00'}, {'y': 2, 'x': '2017-01-04T00:00:00'}, {'y': 18, 'x': '2017-01-05T00:00:00'}, {'y': 19, 'x': '2017-01-06T00:00:00'}, {'y': 20, 'x': '2017-01-07T00:00:00'}, {'y': 5, 'x': '2017-01-08T00:00:00'}, {'y': 23, 'x': '2017-01-09T00:00:00'}, {'y': 18, 'x': '2017-01-10T00:00:00'}, {'y': 5, 'x': '2017-01-11T00:00:00'}, {'y': 6, 'x': '2017-01-12T00:00:00'}, {'y': 2, 'x': '2017-01-13T00:00:00'}, {'y': 23, 'x': '2017-01-14T00:00:00'}, {'y': 3, 'x': '2017-01-15T00:00:00'}, {'y': 24, 'x': '2017-01-16T00:00:00'}, {'y': 10, 'x': '2017-01-17T00:00:00'}, {'y': 9, 'x': '2017-01-18T00:00:00'}, {'y': 11, 'x': '2017-01-19T00:00:00'}, {'y': 10, 'x': '2017-01-20T00:00:00'}, {'y': 2, 'x': '2017-01-21T00:00:00'}, {'y': 16, 'x': '2017-01-22T00:00:00'}, {'y': 24, 'x': '2017-01-23T00:00:00'}, {'y': 3, 'x': '2017-01-24T00:00:00'}, {'y': 13, 'x': '2017-01-25T00:00:00'}, {'y': 7, 'x': '2017-01-26T00:00:00'}, {'y': 10, 'x': '2017-01-27T00:00:00'}, {'y': 7, 'x': '2017-01-28T00:00:00'}, {'y': 13, 'x': '2017-01-29T00:00:00'}, {'y': 1, 'x': '2017-01-30T00:00:00'}, {'y': 10, 'x': '2017-01-31T00:00:00'}, {'y': 7, 'x': '2017-02-01T00:00:00'}]

        return [
            DataSet(type='line',
                    label='Scatter',
                    showLine=False,
                    data=data_scatter),
            DataSet(type='line',
                    label='Line',
                    borderColor='red',
                    data=data_line)
        ]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/scatter_line_chart/$',
        ChartView.from_chart(charts.ScatterLineChart()),
        name='scatter_line_chart')
]

# Template.html
{% render_chart 'scatter_line_chart' %}

Bar Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class BarChart(Chart):
    chart_type = 'bar'

    def get_labels(self, **kwargs):
        return ["January", "February", "March", "April",
                "May", "June", "July"]

    def get_datasets(self, **kwargs):
        data = [10, 15, 29, 30, 5, 10, 22]
        colors = [
            rgba(255, 99, 132, 0.2),
            rgba(54, 162, 235, 0.2),
            rgba(255, 206, 86, 0.2),
            rgba(75, 192, 192, 0.2),
            rgba(153, 102, 255, 0.2),
            rgba(255, 159, 64, 0.2)
        ]

        return [DataSet(label='Bar Chart',
                        data=data,
                        borderWidth=1,
                        backgroundColor=colors,
                        borderColor=colors)]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/bar_chart/$',
        ChartView.from_chart(charts.BarChart()),
        name='bar_chart'),
]

# Template.html
{% render_chart 'bar_chart' %}

Radar Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class RadarChart(Chart):
    chart_type = 'radar'

    def get_labels(self):
        return ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"]

    def get_datasets(self, **kwargs):
        return [DataSet(label="My First dataset",
                        color=(179, 181, 198),
                        data=[65, 59, 90, 81, 56, 55, 40]),
                DataSet(label="My Second dataset",
                        color=(255, 99, 132),
                        data=[28, 48, 40, 19, 96, 27, 100])
                ]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/radar_chart/$',
        ChartView.from_chart(charts.RadarChart()),
        name='radar_chart')
]

# Template.html
{% render_chart 'radar_chart' %}

Polar Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class PolarChart(Chart):
    chart_type = 'polarArea'

    def get_labels(self, **kwargs):
        return ["Red", "Green", "Yellow", "Grey", "Blue"]

    def get_datasets(self, **kwargs):
        return [DataSet(label="My DataSet",
                        data=[11, 16, 7, 3, 14],
                        backgroundColor=[
                            "#FF6384",
                            "#4BC0C0",
                            "#FFCE56",
                            "#E7E9ED",
                            "#36A2EB"
                        ])
                ]

# urls.py 
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/polar_chart/$',
        ChartView.from_chart(charts.PolarChart()),
        name='polar_chart')
]

# Template.html
{% render_chart 'polar_chart' %}

Pie Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class PieChart(Chart):
    chart_type = 'pie'

    def get_labels(self, **kwargs):
        return ["Red", "Blue", "Yellow"]

    def get_datasets(self, **kwargs):
        data = [300, 50, 100]
        colors = [
            "#FF6384",
            "#36A2EB",
            "#FFCE56"
        ]
        return [DataSet(data=data,
                        label="My Pie Data",
                        backgroundColor=colors,
                        hoverBackgroundColor=colors)]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/pie_chart/$',
        ChartView.from_chart(charts.PieChart()),
        name='pie_chart'),
]

# Template.html
{% render_chart 'pie_chart' %}

Bubble Chart

# charts.py
from jchart import Chart
from jchart.config import Axes, DataSet, rgba

class BubbleChart(Chart):
    chart_type = 'bubble'

    def get_datasets(self, **kwargs):
        data = [{
            'x': randint(1, 10),
            'y': randint(1, 25),
            'r': randint(1, 10),
        } for i in range(25)]

        return [DataSet(label="First DataSet",
                        data=data,
                        backgroundColor='#FF6384',
                        hoverBackgroundColor='#FF6384')]

# urls.py
from demo import charts
from jchart.views import ChartView

urlpatterns = [
    url(r'^charts/bubble_chart/$',
        ChartView.from_chart(charts.BubbleChart()),
        name='bubble_chart'),
]

# Template.html
{% render_chart 'bubble_chart' %}
Fork me on GitHub