Skip to main content

Create Charts with the awesome Chart.js Library

Project description

dj_chart

https://badge.fury.io/py/dj_chart.png

The goal of dj_chart is generate charts with the awesome Chart.js library.

Requirements

Django 1.9

Quickstart

Install dj_auth:

pip install dj_chart

Put dj_auth into your INSTALLED_APPS at settings module:

INSTALLED_APPS = (
   ...
   'dj_chart',
)

TEMPLATES = [
    {'BACKEND': 'django.template.backends.django.DjangoTemplates',
     'DIRS': (your dirs, ),
     'APP_DIRS': True,
     'OPTIONS': { },
     },
]

Features

ChartMixin

You can use this Mixin for example with a Templateview or also ListView:

from django.views.generic import TemplateView

from dj_chart.views import ChartMixin

from .models import Fruits

class YourView(ChartMixin, Templateview):

    chart_title_show = True
    chart_title = u"Your Chart Title"
    chart_types_available = [PIE, DOUGHNUT]
    show_grid = False

    def get_chart_data(self):
        # populate self.chart_data
        # Apples
        self.chart_data['labels'].append("Apple")
        self.chart_data['datasets'][0]['backgroundColor'].append("rgb(0,148,63)")
        self.chart_data['datasets'][0]['data'].append(Fruits.filter(fruit_type=1).count())
        # Pear
        self.chart_data['labels'].append(Pear")
        self.chart_data['datasets'][0]['backgroundColor'].append("rgb(222,6,19)")
        self.chart_data['datasets'][0]['data'].append(Fruits.filter(fruit_type=2).count())
        # Banana
        self.chart_data['labels'].append("Banana")
        self.chart_data['datasets'][0]['backgroundColor'].append("rgb(187,187,187)")
        self.chart_data['datasets'][0]['data'].append(Fruits.filter(fruit_type=3).count())

    def get_context_data(self, **kwargs):
        context = super(YourView, self).get_context_data(**kwargs)
        context = self.set_chart_context(context)
        return context

Example with Dates on the x-axis:

from django.db import models
from django.views.generic import TemplateView

from dj_chart.constants import MONTH
from dj_chart.views import ChartMixin

class Category(models.Model):
    description models.CharField(max_length=100, verbose_name='Decscription')

class Person(models.Model):
    first_name = models.CharField(max_length=100, verbose_name='Firstname')
    last_name = models.CharField(max_length=100, verbose_name='Lastname')
    birthday = models.DateField(verbose_name=_(u'Birthday'))
    category = models.ForeignKey(Category, verbose_name=_(u'Category'))

class ChartPie(ChartMixin, TemplateView):

    chart_title_show = True
    chart_title = u"My Chart"
    chart_types_available = [PIE, DOUGHNUT, BAR]
    show_grid = False
    x_axis = MONTH
    steps = 6

    def get_chart_data(self):
        if self.queryset:
            self.set_labels_and_filter_values_for_xaxes(steps=self.steps, type=self.x_axis)

            for label in self.chart_data['labels']:
                self.label_urls[label] = {}
            self.chart_data['datasets'] = []
            for record in self.queryset:
                qs = Person.objects.filter(category=record)
                for i in range(1, len(self.filter_values)):
                    data.append(qs.filter(birthday__gte=self.filter_values[i - 1], birthday__lt=self.filter_values[i]).count())
                    self.label_urls["%s" % self.chart_data['labels'][i]]["%s" % record.description] = {"url": "%s" % reverse('your-url')}

    def get_context_data(self, **kwargs):
        context = super(ChartPie, self).get_context_data(**kwargs)
        self.queryset = Category.objects.all()
        context = self.write_chart_to_context(context)
        return context

Todo

  • Python 3

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ coverage run --source=dj_auth runtests.py && coverage html

Credits

Tools used in rendering this package:

History

0.1.0 (2016-10-05)

  • First release

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_chart-1.1.10.tar.gz (63.8 kB view details)

Uploaded Source

Built Distribution

dj_chart-1.1.10-py2.py3-none-any.whl (61.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dj_chart-1.1.10.tar.gz.

File metadata

  • Download URL: dj_chart-1.1.10.tar.gz
  • Upload date:
  • Size: 63.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.8

File hashes

Hashes for dj_chart-1.1.10.tar.gz
Algorithm Hash digest
SHA256 20f606a3c2f8fe84ea4575a450ebe8051311f2ec54a7724034b7c2435cbd20a7
MD5 7e347cc6b3f273c86a0c39a5abc101af
BLAKE2b-256 00750af438a792a8f7cee7bae10fb72b634ed07b512efd609b894c5ffc9d7d82

See more details on using hashes here.

File details

Details for the file dj_chart-1.1.10-py2.py3-none-any.whl.

File metadata

  • Download URL: dj_chart-1.1.10-py2.py3-none-any.whl
  • Upload date:
  • Size: 61.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.8

File hashes

Hashes for dj_chart-1.1.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 370bdaa245a8968c289da013850c185dfb24402948f034858da3170ba37a19ca
MD5 23d0bcd46e7927595eface280bd46c33
BLAKE2b-256 57677e8ca1a5d0f8f9f7d83b18c6d383e34c05778c1728f707336ae5375d65d2

See more details on using hashes here.

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