Merge branch 'develop' of https://github.com/myems/myems into develop

pull/1/head
13621160019@163.com 2021-02-28 12:11:01 +08:00
commit 9754b0b8cb
4 changed files with 125 additions and 68 deletions

View File

@ -351,8 +351,13 @@ def generate_excel(report,
current_row_number = 19 current_row_number = 19
has_child_flag = True has_child_flag = True
if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \
len(report['child_space']["energy_category_names"]) == 0: len(report['child_space']["energy_category_names"]) == 0 \
or 'child_space_names_array' not in report['child_space'].keys() \
or report['child_space']['energy_category_names'] is None \
or len(report['child_space']['child_space_names_array']) == 0 \
or len(report['child_space']['child_space_names_array'][0]) == 0:
has_child_flag = False has_child_flag = False
if has_child_flag: if has_child_flag:
@ -362,6 +367,7 @@ def generate_excel(report,
ws['B' + str(current_row_number)] = name + ' 子空间数据' ws['B' + str(current_row_number)] = name + ' 子空间数据'
current_row_number += 1 current_row_number += 1
table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
@ -413,20 +419,21 @@ def generate_excel(report,
ws[col + row] = round(every_day_sum, 2) ws[col + row] = round(every_day_sum, 2)
ws[col + row].border = f_border ws[col + row].border = f_border
table_end_row_number = current_row_number
current_row_number += 1 current_row_number += 1
chart_start_row_number = current_row_number
# Pie # Pie
for i in range(0, ca_len): for i in range(0, ca_len):
pie = PieChart() pie = PieChart()
labels = Reference(ws, min_col=2, min_row=current_row_number - space_len, max_row=current_row_number - 1) labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
pie_data = Reference(ws, min_col=3 + i, min_row=current_row_number - space_len - 1, pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number,
max_row=current_row_number - 1) max_row=table_end_row_number)
pie.add_data(pie_data, titles_from_data=True) pie.add_data(pie_data, titles_from_data=True)
pie.set_categories(labels) pie.set_categories(labels)
pie.height = 6.6 pie.height = 6.6
pie.width = 8 pie.width = 8
col = chr(ord('C') + i) pie.title = ws.cell(column=3+i, row=table_start_row_number).value
pie.title = ws[col + '20'].value
s1 = pie.series[0] s1 = pie.series[0]
s1.dLbls = DataLabelList() s1.dLbls = DataLabelList()
s1.dLbls.showCatName = False s1.dLbls.showCatName = False
@ -434,15 +441,17 @@ def generate_excel(report,
s1.dLbls.showPercent = True s1.dLbls.showPercent = True
chart_cell = '' chart_cell = ''
if i % 2 == 0: if i % 2 == 0:
chart_cell = 'B' + str(current_row_number) chart_cell = 'B' + str(chart_start_row_number)
else: else:
chart_cell = 'E' + str(current_row_number) chart_cell = 'E' + str(chart_start_row_number)
current_row_number += 5 chart_start_row_number += 5
ws.add_chart(pie, chart_cell) ws.add_chart(pie, chart_cell)
# chart_col = chr(ord('B') + 2 * i) # chart_col = chr(ord('B') + 2 * i)
# chart_cell = chart_col + str(current_row_number) # chart_cell = chart_col + str(current_row_number)
# ws.add_chart(pie, chart_cell) # ws.add_chart(pie, chart_cell)
current_row_number = chart_start_row_number
if ca_len % 2 == 1: if ca_len % 2 == 1:
current_row_number += 5 current_row_number += 5

View File

@ -395,9 +395,16 @@ def generate_excel(report,
has_child_flag = True has_child_flag = True
# Judge if the space has child space, if not, delete it. # Judge if the space has child space, if not, delete it.
if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \
len(report['child_space']["energy_category_names"]) == 0: len(report['child_space']["energy_category_names"]) == 0 \
or 'child_space_names_array' not in report['child_space'].keys() \
or report['child_space']['energy_category_names'] is None \
or len(report['child_space']['child_space_names_array']) == 0 \
or len(report['child_space']['child_space_names_array'][0]) == 0:
has_child_flag = False has_child_flag = False
current_row_number = 19
if has_child_flag: if has_child_flag:
child = report['child_space'] child = report['child_space']
child_spaces = child['child_space_names_array'][0] child_spaces = child['child_space_names_array'][0]
@ -411,6 +418,8 @@ def generate_excel(report,
ws['B20'].border = f_border ws['B20'].border = f_border
ca_len = len(child['energy_category_names']) ca_len = len(child['energy_category_names'])
table_start_row_number = 20
for i in range(0, ca_len): for i in range(0, ca_len):
row = chr(ord('C') + i) row = chr(ord('C') + i)
ws[row + '20'].fill = table_fill ws[row + '20'].fill = table_fill
@ -420,6 +429,7 @@ def generate_excel(report,
ws[row + '20'] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')' ws[row + '20'] = child['energy_category_names'][i] + ' (' + child['units'][i] + ')'
space_len = len(child['child_space_names_array'][0]) space_len = len(child['child_space_names_array'][0])
for i in range(0, space_len): for i in range(0, space_len):
row = str(i + 21) row = str(i + 21)
@ -434,58 +444,74 @@ def generate_excel(report,
ws[col + row].alignment = c_c_alignment ws[col + row].alignment = c_c_alignment
ws[col + row] = round(child['subtotals_array'][j][i], 2) ws[col + row] = round(child['subtotals_array'][j][i], 2)
ws[col + row].border = f_border ws[col + row].border = f_border
# pie
# 25~30: pie table_end_row_number = 20 + space_len
pie = PieChart() chart_start_row_number = 20 + space_len + 1
pie.title = ws.cell(column=3 + j, row=20).value
labels = Reference(ws, min_col=2, min_row=21, max_row=24) for i in range(0, ca_len):
pie_data = Reference(ws, min_col=3 + j, min_row=20, max_row=24) # pie
pie.add_data(pie_data, titles_from_data=True) # 25~30: pie
pie.set_categories(labels) pie = PieChart()
pie.height = 6.6 # cm 1.05*5 1.05cm = 30 pt pie.title = ws.cell(column=3 + i, row=table_start_row_number).value
pie.width = 8 labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
# pie.title = "Pies sold by category" pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
s1 = pie.series[0] pie.add_data(pie_data, titles_from_data=True)
s1.dLbls = DataLabelList() pie.set_categories(labels)
s1.dLbls.showCatName = True # 标签显示 pie.height = 6.6 # cm 1.05*5 1.05cm = 30 pt
s1.dLbls.showVal = True # 数量显示 pie.width = 8
s1.dLbls.showPercent = True # 百分比显示 # pie.title = "Pies sold by category"
# s1 = CharacterProperties(sz=1800) # 图表中字体大小 *100 s1 = pie.series[0]
chart_col = chr(ord('B') + 2 * j) s1.dLbls = DataLabelList()
chart_cell = chart_col + '25' s1.dLbls.showCatName = False # 标签显示
ws.add_chart(pie, chart_cell) s1.dLbls.showVal = True # 数量显示
else: s1.dLbls.showPercent = True # 百分比显示
for i in range(19, 36 + 1): # s1 = CharacterProperties(sz=1800) # 图表中字体大小 *100
ws.row_dimensions[i].height = 0.1 chart_cell = ''
for i in range(30, 35 + 1): if i % 2 == 0:
ws.row_dimensions[i].height = 0.1 chart_cell = 'B' + str(chart_start_row_number)
else:
chart_cell = 'E' + str(chart_start_row_number)
chart_start_row_number += 5
# ws.add_chart(pie, chart_cell)
# chart_col = chr(ord('B') + 2 * j)
# chart_cell = chart_col + '25'
ws.add_chart(pie, chart_cell)
current_row_number = chart_start_row_number
if ca_len % 2 == 1:
current_row_number += 5
current_row_number += 1
################################################ ################################################
# Fourth: 能耗详情 # Fourth: 能耗详情
# 37: title # current_row_number: title
# 38~ 38+ca_len*6-1: line # current_row_number+1 ~ current_row_number+1+ca_len*6-1: line
# 38+ca_len*6: table title # current_row_number+1+ca_len*6: table title
# 38+ca_len*6~: table_data # current_row_number+1+ca_len*6~: table_data
################################################ ################################################
reporting_period_data = report['reporting_period'] reporting_period_data = report['reporting_period']
times = reporting_period_data['timestamps'] times = reporting_period_data['timestamps']
has_detail_data_flag = True has_detail_data_flag = True
ca_len = len(report['reporting_period']['names']) ca_len = len(report['reporting_period']['names'])
table_row = 38 + ca_len*6 table_row = current_row_number + 1 + ca_len*6
chart_start_row_number = current_row_number + 1
if "timestamps" not in reporting_period_data.keys() or \ if "timestamps" not in reporting_period_data.keys() or \
reporting_period_data['timestamps'] is None or \ reporting_period_data['timestamps'] is None or \
len(reporting_period_data['timestamps']) == 0: len(reporting_period_data['timestamps']) == 0:
has_detail_data_flag = False has_detail_data_flag = False
if has_detail_data_flag: if has_detail_data_flag:
ws['B37'].font = title_font ws['B' + str(current_row_number)].font = title_font
ws['B37'] = name+' 能耗详情' ws['B' + str(current_row_number)] = name+' 详细数据'
ws.row_dimensions[table_row].height = 60 ws.row_dimensions[table_row].height = 60
ws['B'+str(table_row)].fill = table_fill ws['B'+str(table_row)].fill = table_fill
ws['B' + str(table_row)].font = title_font ws['B' + str(table_row)].font = title_font
ws['B'+str(table_row)].border = f_border ws['B'+str(table_row)].border = f_border
ws['B'+str(table_row)].alignment = c_c_alignment ws['B'+str(table_row)].alignment = c_c_alignment
ws['B'+str(table_row)] = '时间' ws['B'+str(table_row)] = '日期时间'
time = times[0] time = times[0]
has_data = False has_data = False
max_row = 0 max_row = 0
@ -526,31 +552,45 @@ def generate_excel(report,
ws[col + row].alignment = c_c_alignment ws[col + row].alignment = c_c_alignment
ws[col + row] = round(reporting_period_data['values'][i][j], 2) ws[col + row] = round(reporting_period_data['values'][i][j], 2)
ws[col + row].border = f_border ws[col + row].border = f_border
current_row_number = table_row + 1 + len(times[0])
ws['B' + str(current_row_number)].font = title_font
ws['B' + str(current_row_number)].alignment = c_c_alignment
ws['B' + str(current_row_number)].border = f_border
ws['B' + str(current_row_number)] = '小计'
for i in range(0, ca_len):
col = chr(ord('C') + i)
ws[col + str(current_row_number)].font = title_font
ws[col + str(current_row_number)].alignment = c_c_alignment
ws[col + str(current_row_number)].border = f_border
ws[col + str(current_row_number)] = reporting_period_data['subtotals'][i]
# line # line
# 39~: line # 39~: line
line = LineChart() line = LineChart()
line.title = '报告期消耗 - ' line.title = '报告期消耗 - ' + ws.cell(column=3+i, row=table_row).value
labels = Reference(ws, min_col=2, min_row=table_row+1, max_row=max_row + 1) labels = Reference(ws, min_col=2, min_row=table_row+1, max_row=max_row)
line_data = Reference(ws, min_col=3 + i, min_row=table_row, max_row=max_row + 1) # openpyxl bug line_data = Reference(ws, min_col=3 + i, min_row=table_row, max_row=max_row) # openpyxl bug
line.add_data(line_data, titles_from_data=True) line.add_data(line_data, titles_from_data=True)
line.set_categories(labels) line.set_categories(labels)
line_data = line.series[0] line_data = line.series[0]
line_data.marker.symbol = "circle" line_data.marker.symbol = "circle"
line_data.smooth = False line_data.smooth = True
line.x_axis.crosses = 'min'
line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt line.height = 8.25 # cm 1.05*5 1.05cm = 30 pt
line.width = 24 line.width = 24
# pie.title = "Pies sold by category" # pie.title = "Pies sold by category"
line.dLbls = DataLabelList() line.dLbls = DataLabelList()
line.dLbls.dLblPos = 't'
# line.dLbls.showCatName = True # label show # line.dLbls.showCatName = True # label show
line.dLbls.showVal = True # val show line.dLbls.showVal = True # val show
line.dLbls.showPercent = True # percent show line.dLbls.showPercent = True # percent show
# s1 = CharacterProperties(sz=1800) # font size *100 # s1 = CharacterProperties(sz=1800) # font size *100
chart_col = 'B' chart_col = 'B'
chart_cell = chart_col + str(38 + 6*i) chart_cell = chart_col + str(chart_start_row_number + 6*i)
ws.add_chart(line, chart_cell) ws.add_chart(line, chart_cell)
else:
for i in range(37, 69 + 1):
ws.row_dimensions[i].height = 0.1
filename = str(uuid.uuid4()) + '.xlsx' filename = str(uuid.uuid4()) + '.xlsx'
wb.save(filename) wb.save(filename)

View File

@ -321,8 +321,13 @@ def generate_excel(report,
##################################### #####################################
has_child_flag = True has_child_flag = True
if "child_space" not in report.keys() or "energy_item_names" not in report['child_space'].keys() or \ if "child_space" not in report.keys() or "energy_item_names" not in report['child_space'].keys() or \
len(report['child_space']["energy_item_names"]) == 0: len(report['child_space']["energy_item_names"]) == 0 \
or 'child_space_names_array' not in report['child_space'].keys() \
or report['child_space']['child_space_names_array'] is None \
or len(report['child_space']['child_space_names_array']) == 0 \
or len(report['child_space']['child_space_names_array'][0]) == 0:
has_child_flag = False has_child_flag = False
if has_child_flag: if has_child_flag:
@ -332,6 +337,7 @@ def generate_excel(report,
ws['B' + str(current_row_number)] = name + ' 子空间数据' ws['B' + str(current_row_number)] = name + ' 子空间数据'
current_row_number += 1 current_row_number += 1
table_start_row_number = current_row_number
ws.row_dimensions[current_row_number].height = 60 ws.row_dimensions[current_row_number].height = 60
ws['B' + str(current_row_number)].fill = table_fill ws['B' + str(current_row_number)].fill = table_fill
@ -365,21 +371,22 @@ def generate_excel(report,
ws[col + row] = round(child['subtotals_array'][j][i], 2) ws[col + row] = round(child['subtotals_array'][j][i], 2)
ws[col + row].border = f_border ws[col + row].border = f_border
table_end_row_number = current_row_number
current_row_number += 1 current_row_number += 1
pie_start_row_number = current_row_number
# Pie # Pie
for i in range(0, ca_len): for i in range(0, ca_len):
pie = PieChart() pie = PieChart()
labels = Reference(ws, min_col=2, min_row=current_row_number - space_len, labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1,
max_row=current_row_number - 1) max_row=table_end_row_number)
pie_data = Reference(ws, min_col=3 + i, min_row=current_row_number - space_len - 1, pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number,
max_row=current_row_number - 1) max_row=table_end_row_number)
pie.add_data(pie_data, titles_from_data=True) pie.add_data(pie_data, titles_from_data=True)
pie.set_categories(labels) pie.set_categories(labels)
pie.height = 6.6 pie.height = 6.6
pie.width = 8 pie.width = 8
col = chr(ord('C') + i) pie.title = ws.cell(column=3 + i, row=table_start_row_number).value
pie.title = ws[col + str(current_row_number - space_len - 1)].value
s1 = pie.series[0] s1 = pie.series[0]
s1.dLbls = DataLabelList() s1.dLbls = DataLabelList()
s1.dLbls.showCatName = False s1.dLbls.showCatName = False
@ -387,12 +394,13 @@ def generate_excel(report,
s1.dLbls.showPercent = True s1.dLbls.showPercent = True
chart_cell = '' chart_cell = ''
if i % 2 == 0: if i % 2 == 0:
chart_cell = 'B' + str(current_row_number) chart_cell = 'B' + str(pie_start_row_number)
else: else:
chart_cell = 'E' + str(current_row_number) chart_cell = 'E' + str(pie_start_row_number)
current_row_number += 5 pie_start_row_number += 5
ws.add_chart(pie, chart_cell) ws.add_chart(pie, chart_cell)
current_row_number = pie_start_row_number
if ca_len % 2 == 1: if ca_len % 2 == 1:
current_row_number += 5 current_row_number += 5
@ -506,7 +514,7 @@ def generate_excel(report,
for i in range(0, ca_len): for i in range(0, ca_len):
line = LineChart() line = LineChart()
line.title = '报告期消耗 - ' + \ line.title = '报告期消耗 - ' + \
reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number)
line.add_data(line_data, titles_from_data=True) line.add_data(line_data, titles_from_data=True)
@ -541,4 +549,3 @@ def group_by_category(category_list):
category_dict[value] = list() category_dict[value] = list()
category_dict[value].append(i) category_dict[value].append(i)
return category_dict return category_dict

View File

@ -432,11 +432,12 @@ def generate_excel(report,
has_child_space_data_flag = True has_child_space_data_flag = True
if 'child_space' not in report.keys() or \ if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \
report['child_space'] is None or \ len(report['child_space']["energy_category_names"]) == 0 \
'energy_category_names' not in report['child_space'].keys() or \ or 'child_space_names_array' not in report['child_space'].keys() \
report['child_space']['energy_category_names'] is None or \ or report['child_space']['energy_category_names'] is None \
len(report['child_space']['energy_category_names']) == 0: or len(report['child_space']['child_space_names_array']) == 0 \
or len(report['child_space']['child_space_names_array'][0]) == 0:
has_child_space_data_flag = False has_child_space_data_flag = False
if has_child_space_data_flag: if has_child_space_data_flag: