一个Django应用程序,可以直接从模型中绘制图表和交叉表。使用HighCharts和jQuery JavaScript库在网页上渲染图表。
项目描述
Django Charit的分支,添加了对Python 3和Django 1.8+的支持!
Django Chartit是一个Django应用程序,可用于轻松地从数据库中的数据创建图表。图表使用Highcharts和jQuery JavaScript库进行渲染。数据库中的数据可以绘制成简单的折线图、柱状图、面积图、散点图等多种图表类型。数据也可以绘制成交叉表,其中数据可以按特定列进行分组和/或旋转。
功能
从模型中绘制图表。
在图表上同一轴上绘制多个模型的数据。
从模型中绘制交叉表。数据可以跨多个列进行旋转。
通过多个列交叉表图例。
将多个模型的数据组合在一起绘制到同一交叉表上。
绘制按特定列旋转的帕累托图。
在交叉表中仅绘制每个类别中排名前几的项目。
原始 Django-Chartit 的改进
增加了 Python 3 兼容性
增加了 Django 1.8 和 1.9 兼容性
将文档添加到 ReadTheDocs
通过 Travis CI 添加了自动化测试
通过 Coveralls 添加了测试覆盖率跟踪
安装
您可以从 PyPI 安装 Django-Chartit 2。只需这样做
$ pip install django_chartit2
您还需要支持 JavaScript 库。有关更多详细信息,请参阅必需的 JavaScript 库部分。
如何使用
在网页上绘制图表或交叉表图表涉及以下步骤。
创建一个 DataPool 或 PivotDataPool 对象,该对象指定需要检索的数据及其来源。
创建一个 Chart 或 PivotChart 对象,分别用于绘制 DataPool 或 PivotDataPool 中的数据。
从 django 视图 函数返回 Chart/PivotChart 对象到 django 模板。
使用 load_charts 模板标签将图表加载到具有特定 ids 的 HTML 标签中。
以下将通过示例解释上述步骤。请继续阅读。
如何创建图表
以下是一个创建折线图的简单示例。假设我们有一个简单的模型,包含 3 个字段 - 一个用于月份,两个用于波士顿和休斯顿的温度。
class MonthlyWeatherByCity(models.Model): month = models.IntegerField() boston_temp = models.DecimalField(max_digits=5, decimal_places=1) houston_temp = models.DecimalField(max_digits=5, decimal_places=1)
假设我们想创建一个简单的折线图,其中 x 轴表示月份,y 轴表示两个城市的温度。
from chartit import DataPool, Chart def weather_chart_view(request): #Step 1: Create a DataPool with the data we want to retrieve. weatherdata = \ DataPool( series= [{'options': { 'source': MonthlyWeatherByCity.objects.all()}, 'terms': [ 'month', 'houston_temp', 'boston_temp']} ]) #Step 2: Create the Chart object cht = Chart( datasource = weatherdata, series_options = [{'options':{ 'type': 'line', 'stacking': False}, 'terms':{ 'month': [ 'boston_temp', 'houston_temp'] }}], chart_options = {'title': { 'text': 'Weather Data of Boston and Houston'}, 'xAxis': { 'title': { 'text': 'Month number'}}}) #Step 3: Send the chart object to the template. return render_to_response({'weatherchart': cht})
您可以使用 django 模板中的 load_charts 过滤器来渲染图表。
<head> <!-- code to include the highcharts and jQuery libraries goes here --> <!-- load_charts filter takes a comma-separated list of id's where --> <!-- the charts need to be rendered to --> {% load chartit %} {{ weatherchart|load_charts:"container" }} </head> <body> <div id='container'> Chart will be rendered here </div> </body>
如何创建交叉表图表
以下是一个创建交叉表图表的示例。假设我们有以下模型。
class DailyWeather(models.Model): month = models.IntegerField() day = models.IntegerField() temperature = models.DecimalField(max_digits=5, decimal_places=1) rainfall = models.DecimalField(max_digits=5, decimal_places=1) city = models.CharField(max_length=50) state = models.CharField(max_length=2)
我们想绘制一个交叉表图表,其中 x 轴表示月份,y 轴表示每个月份降雨量最高的前 3 个城市的平均降雨量。
from chartit import PivotDataPool, PivotChart def rainfall_pivot_chart_view(request): #Step 1: Create a PivotDataPool with the data we want to retrieve. rainpivotdata = \ PivotDataPool( series = [{'options': { 'source': DailyWeather.objects.all(), 'categories': ['month']}, 'terms': { 'avg_rain': Avg('rainfall'), 'legend_by': ['city'], 'top_n_per_cat': 3}} ]) #Step 2: Create the PivotChart object rainpivcht = \ PivotChart( datasource = rainpivotdata, series_options = [{'options':{ 'type': 'column', 'stacking': True}, 'terms':[ 'avg_rain']}], chart_options = {'title': { 'text': 'Rain by Month in top 3 cities'}, 'xAxis': { 'title': { 'text': 'Month'}}}) #Step 3: Send the PivotChart object to the template. return render_to_response({'rainpivchart': rainpivcht})
您可以使用 django 模板中的 load_charts 过滤器来渲染图表。
<head> <!-- code to include the highcharts and jQuery libraries goes here --> <!-- load_charts filter takes a comma-separated list of id's where --> <!-- the charts need to be rendered to --> {% load chartit %} {{ rainpivchart|load_charts:"container" }} </head> <body> <div id='container'> Chart will be rendered here </div> </body>
文档
完整文档可在此处找到。
必需的 JavaScript 库
以下 JavaScript 库是使用 Django-Chartit 2 所必需的。
项目详情
下载文件
下载适合您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
构建分发
django_chartit2-0.2.2.tar.gz的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 8cb46a107b2f0cd2f5072f995c33c5de92e63554ba245eee9cb62d8a834aac23 |
|
MD5 | 31f6ec74b0fab43bd5cf71efb148a9a1 |
|
BLAKE2b-256 | 758533674cea24d15e1c065005cf7bef029a51f0f2bab1a32a4bd21f73cfc367 |
django_chartit2-0.2.2-py2-none-any.whl的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | ebd1cb51f7d90fb840cc4525f7d245dc9319da7844a7a58191159f8e0ffffe88 |
|
MD5 | b8151e012232343e60165ebfa606f9fb |
|
BLAKE2b-256 | ed30ecac488d1882923382fbf4fc0b4785859ea2d268303ac826049efaaa2cbc |