Skip to main content

Library to render chart using chartjs

Project description

Documentation

The full documentation is at https://django-charts.readthedocs.io/en/latest/

Quickstart

Install dj-chartjs:

pip install dj-chartjs

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'dj_chartjs',
    ...
)

PS: You need define jquery and chartjs libraries in your html section script

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>

How to render chart as a View:

Available BarChartView, PieChartView, DoughnutChartView, RadarChartView, HorizontalBarChartView, PolarAreaChartView

in views.py import type chart to want use:

from django.views.generic.base import TemplateView
from django_charts.views import BarChartView

class ExampleChart(BarChartView, TemplateView):
    ...
    title = "Index of ..."
    id_chart = "barchart_example" #any value

    def generate_labels(self):
        return ["Africa","Brazil","Japan","EUA"]

    def generate_values(self):
        return [1,10,15,8]

in your template that you want render chart, use this tag:

{% load dj_chartjs %}
<html>
<head></head>
<body>

{% render_chart chart %}

</body>
</html>

How to use multiples charts as objects

in your views.py:

from django.views.generic import TemplateView
from dj_chartjs.charts import BarChart

class ExampleView(TemplateView):

    template_name = "core/example.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        barchart = BarChart()
        barchart.title = "Example charts title"

        labels = ["test 1","test 2", "test 3", "test 4"]
        data = [2,3,10,6]
        label = "Test"

        context["chart"] = barchart.generate_dataset(labels, data, label)
        return context

And in your “example.html” template use this:

<canvas id=”mychart”></canvas>

on script section:

$(function(){
    new Chart(document.getElementById("mychart"), {
        type: "{{ chart.type }}",
        data: {{ chart.data|safe }},
        options: {{ chart.options|safe }}
    });
})

You can be use chart object in any function in your views.py, for example:

class ExampleView(TemplateView):

    template_name = "core/example.html"

    def my_method(self):
        barchart = BarChart()
        barchart.title = "Example charts title"

        labels = ["test 1","test 2", "test 3", "test 4"]
        data = [2,3,10,6]
        label = "Test"

        return barchart.generate_dataset(labels, data, label)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["chart"] = self.my_method() #any key in context

        return context

The charts available in package is: BarChart, PieChart, HorizontalBarChart, DoughnutChart, PolarAreaChart, RadarChart, LineChart, GroupChart

It’s possible define options to object chart, for example:

barchart.title = “…”
barchart.legend = True

Features

  • TODO

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

History

0.1.0 (2020-03-30)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-chartjs-0.1.14.tar.gz (81.6 kB view hashes)

Uploaded Source

Built Distribution

dj_chartjs-0.1.14-py2.py3-none-any.whl (86.4 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page