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:
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dj_chartjs-0.1.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d956ae3b2673f79010a2a5025d483c92c0447edd25387046fa03a4ffe1593b9a |
|
MD5 | 846790b63da5e6c059e387125ed620f4 |
|
BLAKE2b-256 | c1a06908f55a2cb49590fcb8afa7af9842e02fe6aaf5b52a205a48aff78c1cf5 |