{ "info": { "author": "Rolf H\u00e5vard Blindheim", "author_email": "rhblind@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "##Yet another Google Charts library for Django##\n\n- ~~Requires installation of the gviz_api library which is freely available from Google.~~\n- See http://code.google.com/p/google-visualization-python/ for details.\n\n**UPDATE: The gviz_api library is now included.**\n\n\n## Note ##\n**Development status updated to beta. The code is still short on unit tests, so bad stuff can happen!**\n\nPlease feel free submit patches/pull requests ;)\n\n### Disclaimer ###\nThis library is heavy influenced by [mvasilkov's django-google-charts](https://github.com/mvasilkov/django-google-charts),\nand some of the code (template tags and javascript code) are directly copied from him. I've done some minor adjustments to\nmake it work for my approach.\n\n## About django-gcharts ##\nAs I find mvasilkov's approach very clever, I think it would be nice if the model could deliver it's data in a format\nthe Google Visualization API can read.\n\nThis library is an attempt of doing that, by using a custom QuerySet and Manager which is plugged directly into the model,\nand some wrapper methods to bind the QuerySet data up against the gviz_api library.\nThe goal is to \"fully\" support the QuerySet (with aggregates, joins, extra, annotates, etc) so that we can gather data\nby using familiar QuerySet syntax.\n\n### Demo site ###\nThe git version now includes a demo site which can be run at your local machine once cloned. The demo site previews a few\nof the charts included in the Google Visualization API, and should contain enough working examples for you to figure out\nhow this stuff works.\n\nTo get started with the demo site, follow these steps.\n\n $ git clone https://github.com/rhblind/django-gcharts.git\n $ cd django-gcharts\n $ python manage.py syncdb\n $ python manage.py initdata\n $ python manage.py runserver\n\nThen point your browser to http://localhost:8000 and you should see a few different charts displayed.\n\n## Configuration ##\n\n### Installation ###\n \n $ pip install django-gcharts\n\n### settings.py ###\n GOOGLECHARTS_API = \"1.1\"\n GOOGLECHARTS_PACKAGES = [\"corechart\"]\n \n INSTALLED_APPS = (\n ...\n 'gcharts',\n ...\n )\n\n * `GOOGLECHARTS_API` - Optional. Defaults to 1.1\n * `GOOGLECHARTS_PACKAGES` - Optional. List of packages that should be loaded. Defaults to only `corechart`.\n \n### Packages ###\nThe charts in the Google Visualization API are separated into different packages. For the most basic charts\nyou would only need to load the `corechart` package (which is the default if none is specified).\nBelow follows a list of which charts are available in the different packages.\n\n**Please note that all packages specified in settings.py will load every time the `{% gcharts %} ... {% endgcharts %}`\n block is rendered.**\n\nOptionally, the package for the specific chart can be specified in the `{% render ... %}` tag as a the last option. \nThe tag should in that case be written as: `{% render \"div_id\" \"data\" \"options\" \"package name\" %}`. This will cause \nthe package to be applied to the current `{% gcharts %} ... {% endgcharts %}` block only, in addition to those specified\nin settings.py.\n\n\n* `corechart` contains these charts\n * [AreaChart](https://developers.google.com/chart/interactive/docs/gallery/areachart)\n * [BarChart](https://developers.google.com/chart/interactive/docs/gallery/barchart)\n * [BubbleChart](https://developers.google.com/chart/interactive/docs/gallery/bubblechart)\n * [CandleStickChart](https://developers.google.com/chart/interactive/docs/gallery/candlestickchart)\n * [ColumnChart](https://developers.google.com/chart/interactive/docs/gallery/columnchart)\n * [ComboChart](https://developers.google.com/chart/interactive/docs/gallery/combochart)\n * [LineChart](https://developers.google.com/chart/interactive/docs/gallery/linechart)\n * [PieChart](https://developers.google.com/chart/interactive/docs/gallery/piechart)\n * [ScatterChart](https://developers.google.com/chart/interactive/docs/gallery/scatterchart)\n * [SteppedAreaChart](https://developers.google.com/chart/interactive/docs/gallery/steppedareachart)\n* `gauge`\n * [Gauge](https://developers.google.com/chart/interactive/docs/gallery/gauge)\n* `geochart`\n * [GeoChart](https://developers.google.com/chart/interactive/docs/gallery/geochart)\n* `table`\n * [Table](https://developers.google.com/chart/interactive/docs/gallery/table)\n* `treemap`\n * [TreeMap](https://developers.google.com/chart/interactive/docs/gallery/treemap)\n\n\n### models.py ###\n\nRegister the GChartsManager to the model you'd like to draw charts from\n\n from django.db import models\n from gcharts import GChartsManager\n \n class MyModel(models.Model):\n \n # register the GChartsManager as default manager for this model.\n objects = GChartsManager()\n \n my_field = models.CharField(....)\n my_other_field = models.IntegerField()\n \n ...\n \n \n## Examples ##\n\nSpam Inc. needs to chart how much spam they sell.\n\n**models.py**\n \n from django.db import models\n from gcharts import GChartsManager\n \n class Spam(models.Model):\n \n objects = GChartsManager()\n \n name = models.Charfield(max_length=10)\n ...\n ...\n cdt = models.DateTimeField(auto_add_now=True, verbose_name=\"Creation datetime\")\n \n\n**views.py**\n \n from dateutil.relativedelta import relativedelta\n from django.shortcuts import render_to_response\n from django.template.context import RequestContext\n \n from models import Spam\n \n def render_chart(request):\n if request.method == \"GET\":\n \n # Get a point in time we want to render chart from\n series_age = datetime.today() - relativedelta(months=3)\n \n # Create a fairly advanced QuerySet using:\n # - filter() to get records newer than 'series_age'\n # - extra() to cast a PostgreSQL 'timestampz' to 'date' which translates to a pyton date object\n # - values() to extract fields of interest\n # - annotate() to group aggregate Count into 'id__count'\n # - order_by() to make the aggregate work\n qset = Spam.objects.filter(cdt__gt=series_age).extra(select={\"date\": \"cdt::date\"}) \\\n .values(\"date\").annotate(Count(\"id\")).order_by()\n \n # Call the qset.to_json() method to output the data in json\n # - labels is a dict which sets labels and the correct javascript data type for\n # fields in the QuerySet. The javascript data types are automatically set, \n # except for extra fields, which needs to be specified in a dict as:\n # {'extra_name': {'javascript data type': 'label for field'}}\n # - order is an iterable which sets the column order in which the data should be\n # rendered\n # - formatting is a dict {'field_name': 'expression'}, where expression is a \n # valid string.format() expression.\n spam_json = qset.to_json(labels={\"id__count\": \"Spam sold\", \"date\": {\"date\": \"Date\"}},\n order=(\"date\", \"id__count\"), \n formatting={\"id__count\": \"{0:d} units of spam\"})\n \n return render_to_response(\"sales_overviews/spamreport.html, {\"spam_data\": spam_json},\n context_instance=RequestContext(request))\n\n**spamreport.html**\n\n ...\n\n {% load gcharts %}\n\n {% gcharts %}\n \n options = {\n width: 500,\n height: 300\n };\n \n \n spam_opt = _clone(options);\n spam_opt.title = \"Units of Spam sold last 3 month\";\n \n {% options spam_opt %}\n kind: \"ColumnChart\",\n options: spam_opt,\n {% endoptions %}\n\n {% render \"spam_chart\" \"spam_data\" \"spam_opt\" %}\n \n {% endgcharts %}\n \n