跳转到主要内容

一个Django应用程序,可以直接从模型中绘制图表和交叉表。使用HighCharts和jQuery JavaScript库在网页上渲染图表。

项目描述

Documentation Status https://travis-ci.org/grantmcconnaughey/django-chartit2.svg?branch=master https://coveralls.io/repos/grantmcconnaughey/django-chartit2/badge.svg?branch=master&service=github

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 库部分。

如何使用

在网页上绘制图表或交叉表图表涉及以下步骤。

  1. 创建一个 DataPoolPivotDataPool 对象,该对象指定需要检索的数据及其来源。

  2. 创建一个 ChartPivotChart 对象,分别用于绘制 DataPoolPivotDataPool 中的数据。

  3. 从 django 视图 函数返回 Chart/PivotChart 对象到 django 模板。

  4. 使用 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 (22.0 kB 查看哈希值)

上传时间

构建分发

django_chartit2-0.2.2-py2-none-any.whl (26.5 kB 查看散列值)

上传时间 Python 2

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面