【Django-meetings】dashboard制作条形图-20220928

8 篇文章 0 订阅

背景:

利用echarts来制作dashboard。

views.py



# 定义全局函数def
booking_all = booking.objects.all()

def count_approve_hour_and_percentage(classroom_name,booking_start_date,booking_end_date):
    # booking_end_date结束日期加上一天
    booking_end_date = booking_end_date + dt.timedelta(days=1)
    diff= (booking_end_date - booking_start_date)
    diff_days = diff.days
    #diff_days = 30
    #print('diff.days',diff.days)
    
    fenzi = booking_all.filter(Q(booking_classroom_name__contains=classroom_name) & Q(booking_date__gte=booking_start_date) & Q(booking_date__lte=booking_end_date) & Q(booking_approve_Y_N='Y'))
    fenzi_sum = 0
    for each in fenzi:
        today_d = dt.date.today()
        total_time = dt.datetime.combine(today_d,each.booking_end_time) - dt.datetime.combine(today_d,each.booking_start_time)
        total_minutes = int(str(total_time).split(':')[0])*60 + int(str(total_time).split(':')[1])+1
        fenzi_sum += total_minutes
    # fenzi_percentage = round(100* fenzi_sum / (30*60*8),1)
    fenzi_percentage = round(100* fenzi_sum / (diff_days*60*8),1)
    # print(fenzi_percentage,fenzi_sum)
    return fenzi_percentage,fenzi_sum



# dashboard
@login_required
def dashboard_month(request):
    # 验证权限
    hr_permission = request.user.has_perm('myclassroom.add_classpermission')
    print('hr_permission',hr_permission)
    
    # 获取日期筛选数据
    now = dt.date.today().year
    paradata = request.POST.get('paradata', now)
    if paradata=='':
        paradata = now
    else:
        pass
    paradata = int(paradata)
    # print('paradata',paradata)

    # 默认赋值
    source = [['classroom', '1月', '2月', '3月', '4月','5月','6月','7月', '8月', '9月', '10月','11月','12月',],
            ['孟子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['庄子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['墨子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['荀子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['国际语言', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['会议室', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            ['企业文化展厅', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],]

    # 循环修改source的数据
    for i in range(1,len(source)):
        if source[i][0] == 'classroom':
            pass
        else:
            # 当月第一天和最后一天
            # source[i][12],_ = count_approve_hour_and_percentage(source[i][0],'2022-12-01','2022-12-30')
            for j in range(1,len(source[i])):
                source[i][j],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(paradata, j, 1), dt.datetime(paradata, j, calendar.monthrange(paradata, j)[1]),)
                
    ###### 上下文内容字典
    context = {
        'paradata':paradata,
        'value_month':json.dumps(source),
        }
    return render(request, 'dashboard_month.html',context=context) 





@login_required
def dashboard_week(request):
    # 验证权限
    hr_permission = request.user.has_perm('myclassroom.add_classpermission')
    print('hr_permission',hr_permission)

    # 获取日期筛选数据
    now_year = dt.date.today().year #2022
    # print('now_year',now_year)
    now_month = dt.date.today().month # 1
    # print('now_month',now_month)
    paradata = request.POST.get('paradata', now_month)
    if paradata=='':
        paradata = now_month
    else:
        pass
    paradata = int(paradata)
    # print('paradata',paradata)

    # 默认赋值
    source =  [['classroom', '1号-7号', '8号-14号', '15号-21号', '22号-月底',],
              ['孟子', 0, 0, 0, 0, ],
              ['庄子', 0, 0, 0, 0, ],
              ['墨子', 0, 0, 0, 0, ],
              ['荀子', 0, 0, 0, 0, ],
              ['国际语言', 0, 0, 0, 0, ],
              ['会议室', 0, 0, 0, 0, ],
              ['企业文化展厅', 0, 0, 0, 0, ],]
    print("source",source)

    # 循环修改source的数据
    for i in range(1,len(source)):
        if source[i][0] == 'classroom':
            pass
        else:
            #source[i][1],_ = count_approve_hour_and_percentage(source[i][0], '2022-07-01', '2022-07-30')
            # 当月指定日期
            source[i][1],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 1), dt.datetime(now_year, paradata, 7))
            source[i][2],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 8), dt.datetime(now_year, paradata, 14))
            source[i][3],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 15), dt.datetime(now_year, paradata, 21))
            source[i][4],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 22), dt.datetime(now_year, paradata, calendar.monthrange(now_year, paradata)[1]))

    print("source",source)

    ###### 上下文内容字典
    context = {
        'paradata':paradata,
        'value_week':json.dumps(source),
        }
    return render(request, 'dashboard_week.html',context=context) 







@login_required
def dashboard_day(request):
    # 验证权限
    hr_permission = request.user.has_perm('myclassroom.add_classpermission')
    print('hr_permission',hr_permission)

    # 获取日期筛选数据
    now_year = dt.date.today().year #2022
    # print('now_year',now_year)
    now_month = dt.date.today().month # 1
    # print('now_month',now_month)
    paradata = request.POST.get('paradata', now_month)
    if paradata=='':
        paradata = now_month
    else:
        pass
    paradata = int(paradata)
    # print('paradata',paradata)

    # 默认赋值
    source =  [['classroom', '1号', '2号', '3号', '4号','5号', '6号', '7号', '8号','9号', '10号', '11号', '12号','13号', '14号', '15号', '16号','17号', '18号', '19号', '20号','21号', '22号', '23号', '24号','25号', '26号', '27号', '28号','29号','30号','31号',],
              ['孟子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ],
              ['庄子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
              ['墨子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
              ['荀子', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
              ['国际语言', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
              ['会议室', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
              ['企业文化展厅', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],]

    # 循环修改source的数据
    for i in range(1,len(source)):
        if source[i][0] == 'classroom':
            pass
        else:
            # 当月指定日期
            print('calendar.monthrange(now_year, paradata)[1]',calendar.monthrange(now_year, paradata)[1])
            # for j in range(1,len(source[i])):
            for j in range(1,calendar.monthrange(now_year, paradata)[1]):
                source[i][j],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, j), dt.datetime(now_year, paradata, j))
    
            # source[i][1],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 1), dt.datetime(now_year, paradata, 7))
            # source[i][2],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 8), dt.datetime(now_year, paradata, 14))
            # source[i][3],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 15), dt.datetime(now_year, paradata, 21))
            # source[i][4],_ = count_approve_hour_and_percentage(source[i][0], dt.datetime(now_year, paradata, 22), dt.datetime(now_year, paradata, calendar.monthrange(now_year, paradata)[1]))
    
    # print("source",source)
    ###### 上下文内容字典
    context = {
        'paradata':paradata,
        'value_day':json.dumps(source),
        }


    return render(request, 'dashboard_day.html',context=context)

template

dashboard_month.html

在这里插入图片描述

{% extends 'dashboard-base.html' %}

{% block main_block %}
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
  <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
    <h1 class="h2">Dashboard</h1>
    <div class="btn-toolbar mb-2 mb-md-0">

    <!--筛选行-->
    <div class="row m-1">
      <form action="" method="post">
        {% csrf_token %}
        <div class="form-group row" >
            <select class="custom-select col-6" name="paradata" id="paradata">
                <option value='2022'>2022</option>
                <option value='2023'>2023</option>
                <option value='2024'>2024</option>
                <option value='2025'>2025</option>
            </select>
            <input class="btn-small btn-primary col-6" type="submit" value="查询">
        </div>
      </form>
    </div>

    </div>
  </div>

  <!-- 看板 -->
  <canvas class="my-4" id="chart1" width="1200" height="400"></canvas>
  <script type="text/javascript">
          var value_month_js = JSON.parse('{{ value_month|safe }}');
          console.log('value_month_js',value_month_js);


          var chartDom = document.getElementById('chart1');
          var myChart = echarts.init(chartDom);
          var option;

          option = {
            title: {
              text: '教室利用率'
            },
            // 按坐标轴显示数据
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {},
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            toolbox: {
              feature: {
                saveAsImage: {}
              }
            },
            xAxis: {
              type: 'category',
              boundaryGap: false,
            },
            yAxis: [
              {
                type: 'value',
                name: '百分比',
                min: 0,
                // max: 100,
                // interval: 10,
                axisLabel: {
                  formatter: '{value}%'
                }
              }
            ],

            dataset: {
              // source: [
              //   ['product', '1月', '2月', '3月', '4月','5月','6月','7月', '8月', '9月', '10月','11月','12月',],
              //   ['孟子', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1, 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
              //   ['庄子', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7, 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
              //   ['墨子', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5, 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
              //   ['荀子', 25.2, 37.1, 41.2, 18, 33.9, 49.1, 25.2, 37.1, 41.2, 18, 33.9, 49.1],
              //   ['国际语言', 25.2, 37.1, 41.2, 18, 33.9, 49.1, 25.2, 37.1, 41.2, 18, 33.9, 49.1],
              //   ['会议室', 25.2, 37.1, 41.2, 18, 33.9, 49.1, 25.2, 37.1, 41.2, 18, 33.9, 49.1],
              //   ['企业文化展厅', 25.2, 37.1, 41.2, 18, 33.9, 49.1, 25.2, 37.1, 41.2, 18, 33.9, 49.1],
              // ]
              source: value_month_js
            },
            series: [
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
            ]
          };

          option && myChart.setOption(option);
  </script>


</main>
{% endblock %}

dashboard_week.html

在这里插入图片描述

{% extends 'dashboard-base.html' %}

{% block main_block %}
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
  <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
    <h1 class="h2">Dashboard</h1>
    <div class="btn-toolbar mb-2 mb-md-0">

    <!--筛选行-->
    <div class="row m-1">
      <form action="" method="post">
        {% csrf_token %}
        <div class="form-group row" >
            <select class="custom-select col-6" name="paradata" id="paradata">
                <option value=''>---</option>
                <option value='1'>1月</option>
                <option value='2'>2月</option>
                <option value='3'>3月</option>
                <option value='4'>4月</option>
                <option value='5'>5月</option>
                <option value='6'>6月</option>
                <option value='7'>7月</option>
                <option value='8'>8月</option>
                <option value='9'>9月</option>
                <option value='10'>10月</option>
                <option value='11'>11月</option>
                <option value='12'>12月</option>
            </select>
            <input class="btn-small btn-primary col-6" type="submit" value="查询">
        </div>
      </form>
    </div>

    </div>
  </div>

  <!-- 看板 -->
  <canvas class="my-4" id="chart1" width="1200" height="400"></canvas>
  <script type="text/javascript">
          var value_week_js = JSON.parse('{{ value_week|safe }}');
          console.log('value_week_js',value_week_js);

          var chartDom = document.getElementById('chart1');
          var myChart = echarts.init(chartDom);
          var option;

          option = {
            title: {
              text: '教室利用率'
            },
            // 按坐标轴显示数据
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {},
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            toolbox: {
              feature: {
                saveAsImage: {}
              }
            },
            xAxis: {
              type: 'category',
              boundaryGap: false,
            },
            yAxis: [
              {
                type: 'value',
                name: '百分比',
                min: 0,
                // max: 100,
                // interval: 10,
                axisLabel: {
                  formatter: '{value}%'
                }
              }
            ],

            dataset: {
              // source: [
              //   ['product', '1号-7号', '8号-14号', '15号-21号', '22号-月底',],
              //   ['孟子', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1, ],
              //   ['庄子', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7, ],
              //   ['墨子', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5, ],
              //   ['荀子', 25.2, 37.1, 41.2, 18, 33.9, 49.1, ],
              //   ['国际语言', 25.2, 37.1, 41.2, 18, 33.9, 49.1, ],
              //   ['会议室', 25.2, 37.1, 41.2, 18, 33.9, 49.1, ],
              //   ['企业文化展厅', 25.2, 37.1, 41.2, 18, 33.9, ],
              // ]
              source:value_week_js
            },
            series: [
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
            ]
          };

          option && myChart.setOption(option);
  </script>



</main>
{% endblock %}

dashboard_day.html

在这里插入图片描述

{% extends 'dashboard-base.html' %}

{% block main_block %}
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
  <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
    <h1 class="h2">Dashboard</h1>
    <div class="btn-toolbar mb-2 mb-md-0">

    <!--筛选行-->
    <div class="row m-1">
      <form action="" method="post">
        {% csrf_token %}
        <div class="form-group row" >
            <select class="custom-select col-6" name="paradata" id="paradata">
                <option value=''>---</option>
                <option value='1'>1月</option>
                <option value='2'>2月</option>
                <option value='3'>3月</option>
                <option value='4'>4月</option>
                <option value='5'>5月</option>
                <option value='6'>6月</option>
                <option value='7'>7月</option>
                <option value='8'>8月</option>
                <option value='9'>9月</option>
                <option value='10'>10月</option>
                <option value='11'>11月</option>
                <option value='12'>12月</option>
            </select>
            <input class="btn-small btn-primary col-6" type="submit" value="查询">
        </div>
      </form>
    </div>

    </div>
  </div>

  <!-- 看板 -->
  <canvas class="my-4" id="chart1" width="1200" height="400"></canvas>
  <script type="text/javascript">
          var value_day_js = JSON.parse('{{ value_day|safe }}');
          console.log('value_day_js',value_day_js);

          var chartDom = document.getElementById('chart1');
          var myChart = echarts.init(chartDom);
          var option;

          option = {
            title: {
              text: '教室利用率'
            },
            // 按坐标轴显示数据
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {},
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            toolbox: {
              feature: {
                saveAsImage: {}
              }
            },
            xAxis: {
              type: 'category',
              boundaryGap: false,
            },
            yAxis: [
              {
                type: 'value',
                name: '百分比',
                min: 0,
                // max: 100,
                // interval: 10,
                axisLabel: {
                  formatter: '{value}%'
                }
              }
            ],

            dataset: {
              // source: [
              //   ['product', '周一', '周二', '周三', '周四','周五','周六','周日',],
              //   ['孟子', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1, 56.5, ],
              //   ['庄子', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7, 51.1, ],
              //   ['墨子', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5, 40.1, ],
              //   ['荀子', 10, 10, 10, 18, 10, 10, 10, ],
              //   ['国际语言', 20, 20, 20, 20, 20, 20, 20, ],
              //   ['会议室', 30, 30, 30, 30, 30, 30, 30, ],
              //   ['企业文化展厅', 40, 40, 40, 40, 40, 40, 40,],
              // ]
              source:value_day_js,
            },
            series: [
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },
              {
                seriesLayoutBy: 'row',
                type: 'line',
                smooth: true,
                label: {show: true,position: 'top'},
              },

            ]
          };

          option && myChart.setOption(option);
  </script>

</main>
{% endblock %}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值