plottool.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding: utf-8
  2. import re
  3. import StringIO
  4. import uuid
  5. import logging
  6. import matplotlib as mpl
  7. mpl.use('Agg')
  8. mpl.rcParams['font.family'] = ['SimHei', 'sans-serif']
  9. import matplotlib.pyplot as plt
  10. import matplotlib.table as tb
  11. from utils.upload_to_oss import hnoss
  12. def plot_records_as_table_and_convert_to_png(
  13. (nrows, ncols),
  14. data,
  15. col_headers=None,
  16. title=None):
  17. """
  18. visual records
  19. """
  20. fig_width = ncols
  21. offset = 0
  22. if col_headers:
  23. offset += 1
  24. if title:
  25. offset += 1
  26. fig_height = offset * 2
  27. table_data = []
  28. row_heights_mul = []
  29. for i in data:
  30. t = [j for j in i]
  31. t[-1] = re.sub(',', '\n', t[-1])
  32. t[-2] = re.sub(',', '\n', t[-2])
  33. nlines = max(t[-1].count('\n'), t[-2].count('\n'))
  34. row_heights_mul.append(nlines + 2)
  35. fig_height += (nlines + 2)
  36. table_data.append(t)
  37. fig = plt.figure(figsize=(fig_width, float(fig_height)/5))
  38. ax = fig.add_axes([0, 0, 1, 1])
  39. ax.axis('off')
  40. table = tb.Table(ax, bbox=[0, 0, 1, 1])
  41. table.auto_set_font_size(False)
  42. widths = [0.05, 0.15, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2]
  43. height = table._approx_text_height()
  44. if title:
  45. for i in range(ncols):
  46. table.add_cell(0, i, width=widths[i], height=height * 2, edgecolor='red', facecolor='red', loc='right')
  47. table[0, 6].get_text().set_text(title)
  48. table[0, 6].set_fontsize(14)
  49. table[0, 6].get_text().set_color('yellow')
  50. if col_headers:
  51. for i in range(ncols):
  52. table.add_cell(1, i, width=widths[i], height=height * 2, facecolor='yellow', text=col_headers[i], loc='center')
  53. # table[1, i].set_fontsize(10)
  54. for i in range(nrows):
  55. for j in range(ncols):
  56. table.add_cell(i + offset, j, width=widths[j], height=height*row_heights_mul[i], text=table_data[i][j], loc='center')
  57. # table[i + offset, j].set_fontsize(10)
  58. if not table_data[i][5].startswith('-'):
  59. table[i + offset, j].get_text().set_color('red')
  60. ax.add_table(table)
  61. buf = StringIO.StringIO()
  62. plt.savefig(buf, format='png')
  63. return buf.getvalue()
  64. def plot_records(data, col_headers, title, prefix):
  65. nrows = len(data)
  66. if nrows == 0:
  67. return
  68. ncols = len(col_headers) if col_headers else len(data[0])
  69. ret = []
  70. start = 0
  71. step = 100
  72. while True:
  73. part = data[start: start + step]
  74. part_nrows = len(part)
  75. if start == 0:
  76. png_bytes = plot_records_as_table_and_convert_to_png(
  77. (part_nrows, ncols),
  78. part,
  79. col_headers,
  80. title)
  81. else:
  82. png_bytes = plot_records_as_table_and_convert_to_png(
  83. (part_nrows, ncols),
  84. part)
  85. filename = u'{}-第{}-{}名.png'.format(title, start + 1, start + part_nrows)
  86. logging.error(filename)
  87. image_url = hnoss.upload_from_str(
  88. png_bytes,
  89. '{}_{}.png'.format(prefix, uuid.uuid4().hex),
  90. False)
  91. ret.append({'filename': filename, 'image_url': image_url})
  92. start += step
  93. if start >= nrows:
  94. break
  95. return ret