Ejemplo n.º 1
0
def bar_chart2():
    # 不习惯链式调用的开发者依旧可以单独调用方法
    bar = Bar()
    bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
    bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
    bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
    bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
    return bar.dump_options()
Ejemplo n.º 2
0
 def post(self, request, format=None):
     post = request.POST
     province = post.get('province')
     city = post.get('city')
     month = post.get('date')
     print('province=%s, city=%s, month=%s' % (province, city, month))
     bar = Bar()
     x_axis = []
     y_axis = []
     if province == 'all':
         series_name = '全省销售额'
         if month == 'all':
             totals = runquery(
                 """SELECT SUM(money) AS total, province FROM t_sale_info WHERE `month` in (1,2,3,4,5,6) GROUP BY province ORDER BY total DESC;"""
             )
             title = '本年度各省销售情况'
         else:
             totals = runquery(
                 """SELECT SUM(money) AS total, province FROM t_sale_info WHERE `month` ="""
                 + str(month) +
                 """ GROUP BY province ORDER BY total DESC;""")
             title = str(month) + '月份各省销售情况'
         for total in totals:
             x_axis.append(total.get('province'))
             y_axis.append(total.get('total'))
     else:
         if city == 'all':
             if month == 'all':
                 series_name = province + '省月销售额'
                 totals = runquery(
                     """SELECT SUM(money) AS total, month FROM t_sale_info WHERE province = '"""
                     + str(province) + """' GROUP BY `month`;""")
                 title = province + '省本年度各月销售情况'
                 for total in totals:
                     x_axis.append(total.get('month'))
                     y_axis.append(total.get('total'))
                 x_axis = list(map(lambda m: str(m) + '月', x_axis))
             else:
                 series_name = month + '月市销售额'
                 totals = runquery(
                     """SELECT money,city FROM t_sale_info WHERE province='"""
                     + province + """' AND `month` = """ + str(month) +
                     """ ORDER BY money DESC;""")
                 title = province + '省' + str(month) + '月份各市销售情况'
                 for total in totals:
                     x_axis.append(total.get('city'))
                     y_axis.append(total.get('money'))
         else:
             totals = runquery(
                 """SELECT money,month FROM t_sale_info WHERE city = '""" +
                 city + """';""")
             series_name = province + '省' + city + '市月销售额'
             title = province + '省' + city + '市各月销售情况'
             for total in totals:
                 x_axis.append(total.get('month'))
                 y_axis.append(total.get('money'))
             x_axis = list(map(lambda m: str(m) + '月', x_axis))
     # 绘图
     bar.add_xaxis(x_axis)
     bar.add_yaxis(series_name=series_name,
                   yaxis_data=y_axis,
                   markpoint_opts=MarkPointOpts(data=[
                       MarkPointItem(name='最大值', type_='max'),
                       MarkPointItem(name='最小值', type_='min')
                   ]),
                   markline_opts=MarkLineOpts(
                       data=[MarkLineItem(type_='average', name='平均值')]))
     bar.set_global_opts(title_opts=opts.TitleOpts(title=title),
                         toolbox_opts={
                             'show': 'true',
                             'feature': {
                                 'dataView': {
                                     'show': 'false',
                                     'readOnly': 'false'
                                 },
                                 'magicType': {
                                     'show': 'true',
                                     'type': ['line', 'bar']
                                 },
                                 'restore': {
                                     'show': 'true'
                                 },
                                 'saveAsImage': {
                                     'show': 'true'
                                 }
                             }
                         })
     return response_success(data=bar.dump_options())