Metadata-Version: 2.1
Name: django-admincharts
Version: 0.1.2
Summary: Chart.js integration for Django admin models
Home-page: https://github.com/dropseed/django-admincharts
License: MIT
Keywords: static,site,generator,jinja
Author: Dave Gaeddert
Author-email: dave.gaeddert@dropseed.io
Requires-Python: >=3.7,<4.0
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Project-URL: Documentation, https://github.com/dropseed/django-admincharts
Project-URL: Repository, https://github.com/dropseed/django-admincharts
Description-Content-Type: text/markdown

# django-admincharts

Add [Chart.js](https://www.chartjs.org/docs/latest/) visualizations to your Django admin using a mixin class.

## Example

![django-admincharts example](https://user-images.githubusercontent.com/649496/124193149-f3c4c380-da8b-11eb-95d9-74e4f81c4c0a.png)

```python
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

Install from [pypi.org](https://pypi.org/project/django-admincharts/):

```console
$ pip install django-admincharts
```

Add `admincharts` to your Django `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    ...
    "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:

```python
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:

```python
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.](https://www.chartjs.org/docs/latest/configuration/)

