Coder Social home page Coder Social logo

django-chartflo's Introduction

Django Chartflo

Charts for the lazy ones in Django using Amcharts. Just make your query, pack the data and its ready.

Install

Clone and add 'chartflo', to INSTALLED_APPS

Usage

Generic view

from chartflo.views import ChartsView
from django.contrib.auth.models import User


class MyView(ChartsView):
 graph_type = "pyramid"

 def get_data(self):
     users = User.objects.all()
     staff = users.filter(is_staff=True)
     superusers = users.filter(is_superuser=True)
     others = users.filter(is_superuser=False, is_staff=False)
     dataset = {"users": others.count(), "staff": staff.count(),
                "superuser": superusers.count()}
     return dataset

Custom view

from chartflo.utils import ChartDataPack
from myapp.models import MyModelToChart

def special_check(value):
  if somecheck(value) is True:
  	return True
  return False

class MyChartsView(TemplateView):
  template_name = 'mytemplate.html'

  def get_context_data(self, **kwargs):
      context = super(MyChartsView, self).get_context_data(**kwargs)
      # get the data
      query = MyModelToChart.objects.all()
      # process count
      P = ChartDataPack()
      dataset = {}
      dataset["all_objects"] = P.count(query)
      dataset["published_objects"] = P.count(query.filter(published=True))
      dataset["special_objects"] = P.count(query, {"fieldname", special_check})
      # package the data
      datapack = P.package("chart_id", "Data label", dataset)
      context['datapack'] = datapack
      # options
      datapack['legend'] = True
      datapack['export'] = True
      return context

You must give a query to ChartDataPack.count. It is also possible to pass field names associated to functions to make some custom checks: if this function returns False the instance will not be counted.

In the template

{% include "chartflo/charts/pie.html" %}
   <div id="{{ datapack.chart_id }}" style="width: 100%; height: 600px; background-color: #FFFFFF;">
</div>

Available charts: pie.html, bar.html, pyramid.html, timeline.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.