一个django应用程序,可以直接从模型绘制图表和透视图。使用highcharts和jquery javascript库在网页上呈现图表。

django_chartit2的Python项目详细描述


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

django charit的fork添加了对python 3和django 1.8+的支持!

django chart是一个django应用程序,可以用来轻松地从数据中创建图表 在你的数据库里。图表使用Highcharts和^{tt2}呈现$ javascript库。数据库中的数据可以绘制为简单的直线 图表、柱状图、面积图、散点图和许多其他图表类型。 数据也可以绘制为数据分组的轴图和/或 由特定列旋转。

功能

  • 从模型绘制图表。
  • 从图表上的同一轴上的多个模型绘制数据。
  • 从模型绘制轴图。数据可以通过 柱。
  • 按多个列列出的图例透视图。
  • 将来自多个模型的数据合并到同一透视图上。
  • 绘制一个pareto图,由一个特定的列进行排序。
  • 在透视图中,每个类别仅绘制前几项。

对原始django charit的改进

  • 增加了python 3兼容性
  • 添加了django 1.8和1.9兼容性
  • 添加文档以阅读文档
  • 通过travis ci添加了自动测试
  • 通过工作服增加了测试覆盖率跟踪功能

安装

您可以从pypi安装django chartit 2。只要做

$ pip install django_chartit2

您还需要支持javascript库。见 Required JavaScript Libraries部分了解更多详细信息。

如何使用

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

  1. 创建指定什么数据的DataPoolPivotDataPool对象 你需要从哪里取回。
  2. 创建ChartPivotChart对象以在 ^分别为{tt3}$或PivotDataPool
  3. 从djangoview函数返回Chart/PivotChart对象 到django模板。
  4. 使用load_chartstemplate标记将图表加载到具有 特定的ids

用例子更容易解释上面的步骤。所以继续读下去。

如何创建图表

下面是一个如何创建折线图的简短示例。假设我们有一个 三场简单模式-一场为月,两场为波士顿气温 还有休斯顿。

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轴)和平均值。 3大城市平均降雨量(Y轴) 每月降雨量。

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>

文档

提供完整的文档 here

必需的javascript库

使用django chartit 2需要以下javascript库。

注意

Django-Chartit 2本身根据bsd许可证获得许可时, HighchartsHighcharts license下获得许可,jQuery在两者下都获得许可 麻省理工学院许可证和GNU通用公共许可证(GPL)第2版。这是你自己的 下载和使用时遵守各自许可证的责任 支持的javascript库。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JDBC URL DB2编码字符   java远程jprofiler集成   Java8中日期对象的after函数问题   Java Swing GlassPane拖动性能   Java中的递归导致堆栈溢出错误   JavaJersey客户端3。Android上的x   java Exchange日历,创建约会和唯一ID   java将键盘布局从AZERTY转换为QWERTY   java无法反序列化启动数组JSON/Spring MVC之外的对象实例   java创建一个Word(.doc)文件,将其转换为PDF和HTML,无需打开Office端口   java操作超时tomcat amazon服务器   java如何创建通用XSD类型   java将代码的测量时间添加到列表中,并获取最小/最大/中间/平均时间   JSpinner的fireStateChanged()方法存在java问题   通过蛮力的爪哇硬币组合