123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- # -*- coding: utf-8 -*-
- from odoo.http import request
- from odoo.utils.constant import LINE, BAR
- from odoo.utils.jinja_util import change_to_json
- from . import chart
- from .indicator import Indicator
- class ChartInfo(object):
- __slots__ = ['chart_type', 'unit_field', 'x_field', 'x_show', 'indicator_list', 'column_head_list', 'rows',
- '_column_length', 'unit_archive_model', 'chart_id']
- def __init__(self):
- self.chart_id = 0
- self.chart_type = ''
- self.unit_field = ''
- self.unit_archive_model = ''
- self.x_field = ''
- self.x_show = ''
- self.indicator_list = []
- self.column_head_list = [] # 查询结果:表头
- self.rows = [] # 查询结果
- self._column_length = 0 # 查询结果总列数
- return
- def set_chart_id(self, chart_id):
- self.chart_id = chart_id
- return
- def set_chart_type(self, chart_type):
- self.chart_type = chart_type
- return
- def set_unit(self, unit_field, unit_archive_model):
- self.unit_field = unit_field
- self.unit_archive_model = unit_archive_model
- return
- def set_x(self, x_field, x_show):
- self.x_field = x_field
- self.x_show = x_show
- return
- def add_indicator(self, field, show):
- indicator = Indicator(field, show)
- self.indicator_list.append(indicator)
- return
- def query_chart(self, where_expression, param, source):
- column_list = self._query_chart_column(where_expression, param, source)
- column_list = self._query_name(column_list)
- self._query_chart_data(where_expression, param, column_list, source)
- return
- def _query_name(self, column_list):
- domain = [('id', 'in', column_list)]
- data = request.env[self.unit_archive_model].sudo().search_read(domain, ['id', 'name'])
- return [[row['id'], row['name']] for row in data]
- def get(self, table_height, table_width):
- res = {
- 'chart_id': self.chart_id,
- 'chart': self._get_chart(),
- "table_height": table_height,
- "table_width": table_width,
- }
- # print 'chart res:', res
- return change_to_json(res)
- def _query_chart_column(self, where_expression, param, source):
- sql_format = """select {} from {} {}"""
- sql = sql_format.format('distinct ' + self.unit_field, source, where_expression)
- cr = request.env.cr
- if where_expression:
- cr.execute(sql, param)
- else:
- cr.execute(sql)
- result = cr.fetchall()
- # result = [list(row) for row in result]
- # self._change_none_(result)
- return [row[0] for row in result]
- def _query_chart_data(self, where_expression, param, column_list, source):
- sql_format = """select {} from {} {} group by {} order by {}"""
- field_expression_list = self._get_indicator_expression(column_list)
- field_expression_list = [self.x_field] + field_expression_list
- self._column_length = len(field_expression_list)
- field_expressions = ','.join(field_expression_list)
- sql = sql_format.format(field_expressions, source, where_expression, self.x_field, self.x_field)
- cr = request.env.cr
- if where_expression:
- cr.execute(sql, param)
- else:
- cr.execute(sql)
- result = cr.fetchall()
- result = [list(row) for row in result]
- self._change_none_(result, self._column_length)
- self.rows = result
- def _change_none_(self, rows, _length):
- for r in rows:
- for index in range(_length):
- if not r[index]:
- r[index] = ''
- return
- def _get_indicator_expression(self, column_value_list):
- res = []
- heads_list = [self.x_show]
- for _id, name in column_value_list:
- for indicator in self.indicator_list:
- res.append("sum(case when {}={} then {} end) as {}".format(self.unit_field, _id, indicator.field,
- indicator.show))
- heads_list.append("{}/{}".format(name, indicator.show))
- self.column_head_list = heads_list
- return res
- def _get_chart(self):
- data = self._get_chart_data()
- helper = chart.Chart(None)
- helper.set_data(data)
- helper.set_zoom(0, 100)
- helper.set_chart_type(self._get_chart_type())
- # helper.set_change_xy()
- return helper.get()
- def _get_chart_data(self):
- data = [self.column_head_list] + self.rows
- return list(map(list, zip(*data))) # 行列转换
- def _get_chart_type(self):
- if self.chart_type == LINE:
- return chart.ChartType().line
- if self.chart_type == BAR:
- return chart.ChartType().bar
- return chart.ChartType().line
|