xjc 1 tahun lalu
induk
melakukan
3d4dd561ec
3 mengubah file dengan 75 tambahan dan 56 penghapusan
  1. 6 17
      src/manage/controls.py
  2. 1 3
      src/manage/views.py
  3. 68 36
      src/utils/plottool.py

+ 6 - 17
src/manage/controls.py

@@ -29,7 +29,7 @@ from django.db import connection
 from utils.wxSubscribeMessage import send_consult_reply_message
 from utils.aliyunpush import aliyunpush
 from tools.article_spider import startSpider
-from utils.plottool import plot_table
+from utils.plottool import plot_records
 
 import xlrd
 import xlwt
@@ -710,7 +710,7 @@ def download_records_image(request):
     if kwargs.get("stock_date"):
         qset = qset.filter(stock_date=kwargs.get("stock_date"))
         if match_group_name:
-            title = u'{}-{}{}'.format(match_name, match_group_name, kwargs.get("stock_date")[5:])
+            title = u'{}-{}-{}'.format(match_name, match_group_name, kwargs.get("stock_date")[5:])
         else:
             title = u'{}-{}'.format(match_name, kwargs.get("stock_date")[5:])
     else:
@@ -718,12 +718,8 @@ def download_records_image(request):
             title = u'{}-{}'.format(match_name, match_group_name)
         else:
             title = match_name
-    filename = u'{}.png'.format(title)
 
-    if match_group_name in (u'百万组', u'十万组', u'菜鸟组') or len(match_group_name) > 3 or match_group_name == '':
-        qdata = list(qset.order_by("-total_income").values())
-    else:
-        qdata = list(qset.order_by("-total_income").values())
+    qdata = list(qset.order_by("-total_income").values())
 
     if kwargs.get("groupId"):
         qdata = sorted(qdata,key=lambda x:x["group_rank"])
@@ -781,18 +777,11 @@ def download_records_image(request):
         except Exception as e:
             print(e)
 
-    nrows = len(data)
     col_headers = [u"NO.", u"选手", u"初始资产", u"今日资产",
                    u"今日盈亏", u"总盈亏", u"昨日持股\n(万元)", u"今日持股\n(万元)"]
-    ncols = len(col_headers)
-
-    image_url = plot_table(
-        (nrows, ncols),
-        data,
-        title,
-        col_headers,
-        'player_record')
-    return filename, image_url
+
+    ret = plot_records(data, col_headers, title, 'player_records')
+    return ret 
 
 
 def fast_save_player(**kwargs):

+ 1 - 3
src/manage/views.py

@@ -619,9 +619,7 @@ class PlayerRecordDownloadImageView(cv.AdminView):
             r = ctl.download_records_image(request)
             if r is None:
                 return cv.to_fail('ret is None')
-            filename, image_url = r
-            return cv.to_suc({'filename': filename,
-                              'image_url': image_url})
+            return cv.to_suc(r)
         except Exception as e:
             cv.tracefail()
             return cv.to_fail(e)

+ 68 - 36
src/utils/plottool.py

@@ -14,23 +14,31 @@ import matplotlib.table as tb
 from utils.upload_to_oss import hnoss
 
 
-def plot_data_as_table_and_convert_to_png((nrows, ncols), data, title, col_headers):
-    table_data = [[title], col_headers]
-    row_heights_mul = [2, 2]
-
+def plot_records_as_table_and_convert_to_png(
+    (nrows, ncols),
+    data,
+    col_headers=None,
+    title=None):
+    """
+    visual records
+    """
     fig_width = ncols
-    fig_height = 4
-    positive_nrows = 0
-
+    offset = 0
+    if col_headers:
+        offset += 1
+    if title:
+        offset += 1
+    fig_height = offset * 2
+
+    table_data = []
+    row_heights_mul = []
     for i in data:
         t = [j for j in i]
-        if not t[5].startswith('-'):
-            positive_nrows += 1
         t[-1] = re.sub(',', '\n', t[-1])
         t[-2] = re.sub(',', '\n', t[-2])
         nlines = max(t[-1].count('\n'), t[-2].count('\n'))
         row_heights_mul.append(nlines + 2)
-        fig_height += (nlines + 1.8)
+        fig_height += (nlines + 2)
         table_data.append(t)
 
     fig = plt.figure(figsize=(fig_width, float(fig_height)/5))
@@ -42,24 +50,24 @@ def plot_data_as_table_and_convert_to_png((nrows, ncols), data, title, col_heade
     widths = [0.05, 0.15, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2]
     height = table._approx_text_height()
 
-    for i in range(ncols):
-        table.add_cell(0, i, width=widths[i], height=height * row_heights_mul[0], edgecolor='red', facecolor='red', loc='right')
-    table[0, 6].get_text().set_text(table_data[0][0])
-    table[0, 6].set_fontsize(14)
-    table[0, 6].get_text().set_color('yellow')
+    if title:
+        for i in range(ncols):
+            table.add_cell(0, i, width=widths[i], height=height * 2, edgecolor='red', facecolor='red', loc='right')
+        table[0, 6].get_text().set_text(title)
+        table[0, 6].set_fontsize(14)
+        table[0, 6].get_text().set_color('yellow')
 
-    for i in range(ncols):
-        table.add_cell(1, i, width=widths[i], height=height * row_heights_mul[1], facecolor='yellow', text=table_data[1][i], loc='center')
-        #table[1, i].set_fontsize(10)
+    if col_headers:
+        for i in range(ncols):
+            table.add_cell(1, i, width=widths[i], height=height * 2, facecolor='yellow', text=col_headers[i], loc='center')
+            # table[1, i].set_fontsize(10)
 
     for i in range(nrows):
         for j in range(ncols):
-            table.add_cell(i + 2, j, width=widths[j], height=height*row_heights_mul[i + 2], text=table_data[i + 2][j], loc='center')
-            #table[i+2, j].set_fontsize(10)
-
-    for row in range(positive_nrows):
-        for col in range(ncols):
-            table[row + 2, col].get_text().set_color('red')
+            table.add_cell(i + offset, j, width=widths[j], height=height*row_heights_mul[i], text=table_data[i][j], loc='center')
+            # table[i + offset, j].set_fontsize(10)
+            if not table_data[i][5].startswith('-'):
+                table[i + offset, j].get_text().set_color('red')
 
     ax.add_table(table)
     buf = StringIO.StringIO()
@@ -67,16 +75,40 @@ def plot_data_as_table_and_convert_to_png((nrows, ncols), data, title, col_heade
     return buf.getvalue()
 
 
-def plot_table((nrows, ncols), data, title, col_headers, prefix):
-    png_bytes = plot_data_as_table_and_convert_to_png(
-        (nrows, ncols),
-        data,
-        title,
-        col_headers)
-
-    url = hnoss.upload_from_str(
-        png_bytes,
-        '{}_{}.png'.format(prefix, uuid.uuid4().hex),
-        False)
-    return url
+def plot_records(data, col_headers, title, prefix):
+    nrows = len(data)
+    if nrows == 0:
+        return
+    ncols = len(col_headers) if col_headers else len(data[0])
+
+    ret = []
+    start = 0
+    step = 100
+    while True:
+        part = data[start: start + step]
+        part_nrows = len(part)
+
+        if start == 0:
+            png_bytes = plot_records_as_table_and_convert_to_png(
+                (part_nrows, ncols),
+                part,
+                col_headers,
+                title)
+        else:
+            png_bytes = plot_records_as_table_and_convert_to_png(
+                (part_nrows, ncols),
+                part)
+        filename = u'{}-第{}-{}名.png'.format(title, start + 1, start + part_nrows)
+        logging.error(filename)
+        image_url = hnoss.upload_from_str(
+            png_bytes,
+            '{}_{}.png'.format(prefix, uuid.uuid4().hex),
+            False)
+        ret.append({'filename': filename, 'image_url': image_url})
+
+        start += step
+        if start >= nrows:
+            break
+
+    return ret