Chart.js integration for Django admin models
Project description
django-admincharts
Add Chart.js visualizations to your Django admin using a mixin class.
Example
from django.contrib import admin
from .models import BillingAccount
from admincharts.admin import AdminChartMixin
from admincharts.utils import months_between_dates
@admin.register(BillingAccount)
class BillingAccountAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_data(self, queryset):
if not queryset:
return {}
# Cannot reorder the queryset at this point
earliest = min([x.ctime for x in queryset])
labels = []
totals = []
for b in months_between_dates(earliest, timezone.now()):
labels.append(b.strftime("%b %Y"))
totals.append(
len(
[
x
for x in queryset
if x.ctime.year == b.year and x.ctime.month == b.month
]
)
)
return {
"labels": labels,
"datasets": [
{"label": "New accounts", "data": totals, "backgroundColor": "#79aec8"},
],
}
Installation
$ pip install django-admincharts
Use the AdminChartMixin
with an admin.ModelAdmin
class to add a chart to the changelist view.
Options can be set directly on the class:
from django.contrib import admin
from admincharts.admin import AdminChartMixin
@admin.register(MyModel)
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
list_chart_type = "bar"
list_chart_data = {}
list_chart_options = {"aspectRatio": 6}
list_chart_config = None # Override the combined settings
Or by using the class methods which gives you access to the queryset being used for the current view:
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, result_list):
...
def get_list_chart_type(self, queryset):
...
def get_list_chart_data(self, queryset):
...
def get_list_chart_options(self, queryset):
...
def get_list_chart_config(self, queryset):
...
The type
, data
, and options
are passed directly to Chart.js to render the chart.
Look at the Chart.js docs to see what kinds of settings can be used.
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
django-admincharts-0.1.1.tar.gz
(69.9 kB
view details)
Built Distribution
File details
Details for the file django-admincharts-0.1.1.tar.gz
.
File metadata
- Download URL: django-admincharts-0.1.1.tar.gz
- Upload date:
- Size: 69.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.5 Darwin/20.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb399c49b9f2711eeacfa64aaa47a8315c54ea80329349054eda3712057a86bb |
|
MD5 | 73f19a815667a2977198329961807468 |
|
BLAKE2b-256 | c4d7460eb7fce286cf58fe420e90f7ea2af3215e5c4e7b73235025e8f21227ac |
File details
Details for the file django_admincharts-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_admincharts-0.1.1-py3-none-any.whl
- Upload date:
- Size: 70.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.5 Darwin/20.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c461b03d165a4ba6668dbf74aac08f9c8aaca725c8dc900180cefe6d59e2a59f |
|
MD5 | cdb96b181454e4d0aa217e13b90a8fb4 |
|
BLAKE2b-256 | a65bbc762ef2e2774a8b4850d9ceb7fc15437418edcbbf43ed34e16fb0251763 |