Beispiel #1
0
 def world_cloud2():
     """ 生成职位词云
     """
     counter = {}
     with open(os.path.join("data", "post_pre_counter.csv"),
               "r",
               encoding="utf-8") as f:
         f_csv = csv.reader(f)
         for row in f_csv:
             counter[row[0]] = counter.get(row[0], int(row[1]))
         # pprint(counter)
     x = [0] * 101
     y = [0] * 101
     i = 0
     # tuple(counter)
     for c in counter:
         x[i] = c
         y[i] = counter[c]
         i = i + 1
         if i > 100:
             break
     wordcloud = WordCloud(width=1300, height=620)
     wordcloud.add("", x, y, word_size_range=[30, 100], shape='diamond')
     wordcloud.render('wc.html')
     make_a_snapshot('wc.html', os.path.join("images", "wc2.png"))
        def drawTwoPie(self, fid, attrExprList, divideExprList, diseaseNameList, dbName,
                       year, isPercent, complete):
            try:
                innerLabel = diseaseNameList[0]
                outerLabel = diseaseNameList[1]

                dataList = []
                n = len(diseaseNameList)

                for i in range(0, n):
                    dataNow = self.dbInf.getDiffDistrictData(attrExprList[i], divideExprList[i], diseaseNameList[i],
                                                             dbName, "all", year, isPercent, complete)
                    dataNow = [x[0] for x in dataNow]
                    data = [x[1] for x in dataNow]
                    dataList.append(data)

                large = 1.3
                size = 0.8
                pie = Pie("", title_pos='center', width=800 * size, height=480 * size)
                pie.add("", outerLabel, dataList[1],
                        radius=[40 * large, 55 * large], is_label_show=True, legend_pos='left')
                pie.add("", innerLabel, dataList[0], radius=[0 * large, 30 * large], legend_orient='vertical',
                        legend_pos='left', is_label_show=True, label_formatter='{d}%', label_pos='inside')
                pie.render()
                make_a_snapshot('render.html', fid + '.png')
                print("图片生成完成:" + fid)

            except Exception as e:
                print(traceback.print_exc())
 def render_chart_to_file(self,
                          chart,
                          object_name="chart",
                          path="render.png",
                          template_name="simple_chart.html",
                          verbose=True,
                          delay=DEFAULT_DELAY,
                          pixel_ratio=DEFAULT_PIXEL_RATIO,
                          **kwargs):
     _, extension = os.path.splitext(path)
     tmp_file_handle, tmp_file_path = mkstemp(suffix=".html")
     super(SnapshotEnvironment,
           self).render_chart_to_file(chart=chart,
                                      object_name=object_name,
                                      path=tmp_file_path,
                                      template_name=template_name,
                                      **kwargs)
     make_a_snapshot(
         tmp_file_path,
         path,
         delay=delay,
         pixel_ratio=pixel_ratio,
         verbose=verbose,
     )
     os.close(tmp_file_handle)
     content = None
     if extension == ".svg":
         with codecs.open(path, "r", "utf-8") as f:
             content = f.read()
     else:
         with open(path, "rb") as f:
             content = f.read()
     return content
 def render_chart_to_file(
         self,
         chart,
         object_name='chart',
         path='render.png',
         template_name='simple_chart.html',
         verbose=True,
         delay=DEFAULT_DELAY,
         **kwargs):
     _, extension = os.path.splitext(path)
     tmp_file_handle, tmp_file_path = mkstemp(suffix='.html')
     super(SnapshotEnvironment, self).render_chart_to_file(
         chart=chart,
         object_name=object_name,
         path=tmp_file_path,
         template_name=template_name,
         **kwargs
     )
     make_a_snapshot(tmp_file_path, path, delay=delay, verbose=verbose)
     os.close(tmp_file_handle)
     content = None
     if extension == '.svg':
         with codecs.open(path, 'r', 'utf-8') as f:
             content = f.read()
     else:
         with open(path, 'rb') as f:
             content = f.read()
     return content
def create_charts(data_path, path_html, path_png, length):
    charts = GeoLines("TSP问题:全国34城市为例", '路径:%.3fkm' % (length / 1000),
                      **style.init_style)
    charts.add('', data_path, **style_geo)
    charts.render(path_html)
    make_a_snapshot(path_html, path_png)
    return print('绘图完毕')
def test_win32_shell_flag_is_false(fake_popen):
    fake_popen.side_effect = CustomTestException("Enough. Stop testing")
    try:
        make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                        'sample.png')
    except CustomTestException:
        eq_(fake_popen.call_args[1]['shell'], False)
Beispiel #7
0
def test_make_a_snapshot_real():
    # cannot produce a consistent binary matching file
    test_output = "real.png"
    make_a_snapshot(
        os.path.join("tests", "fixtures", "render.html"), test_output
    )
    assert os.path.exists(test_output)  # exists just fine
Beispiel #8
0
def test_make_jpeg_snapshot(fake_check, fake_popen):
    fake_popen.return_value.stdout = BytesIO(get_base64_image())
    test_output = "custom.jpeg"
    make_a_snapshot(
        os.path.join("tests", "fixtures", "render.html"), test_output
    )
    assert filecmp.cmp(test_output, get_fixture("sample.jpeg"))
def test_win32_shell_flag(fake_popen):
    fake_popen.side_effect = CustomTestException("Enough. Stop testing")
    try:
        with patch.object(sys, 'platform', 'win32'):
            make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                            'sample.png')
    except CustomTestException:
        eq_(fake_popen.call_args[1]['shell'], True)
Beispiel #10
0
 def get_mem_data():
     base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     performance_data = os.path.join(base_dir, 'data')
     image_path = performance_data + "\\result.png"
     html_path = "%s\\memory_report.html" % (performance_data)
     hfile = ('%s\\memory_data.txt' % (performance_data))
     fp = open(hfile, "r")
     # fp = open(r'D:\moxiu\youyang\data\xin.txt', 'r')
     meminfo_data = fp.readlines()
     case_name_list = []
     for i in range(len(meminfo_data)):
         j = meminfo_data[i].replace('\n', '').split(',')
         case_name_list.append(j[0:1][0])
     heap_size_list = []
     for i in range(len(meminfo_data)):
         j = meminfo_data[i].replace('\n', '').split(',')
         heap_size_list.append(float(j[1:2][0]))
     heap_alloc_list = []
     for i in range(len(meminfo_data)):
         j = meminfo_data[i].replace('\n', '').split(',')
         heap_alloc_list.append(float(j[2:3][0]))
     heap_free_list = []
     for i in range(len(meminfo_data)):
         j = meminfo_data[i].replace('\n', '').split(',')
         heap_free_list.append(float(j[3:4][0]))
     bar = Line("有样__内存数据图形报表", "图表纵轴为数据大小,横轴为case名称,直线为平均值")
     bar.add("heap_size",
             case_name_list,
             heap_size_list,
             label_color=['#B22222'],
             mark_line=["average"],
             mark_point=["max", "min"],
             xaxis_interval=0,
             xaxis_rotate=90)
     bar.add("heap_alloc",
             case_name_list,
             heap_alloc_list,
             label_color=['#008080'],
             mark_line=["average"],
             mark_point=["max", "min"],
             xaxis_interval=0,
             xaxis_rotate=90)
     bar.add("heap_free",
             case_name_list,
             heap_free_list,
             label_color=['#483D8B'],
             mark_line=["average"],
             mark_point=["max", "min"],
             xaxis_interval=0,
             xaxis_rotate=90)
     bar.use_theme("vintage")
     base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     performance_data = os.path.join(base_dir, 'data')
     bar.render('%s\\memory_report.html' % (performance_data))
     make_a_snapshot(html_path, image_path)
     im = Image.open(image_path)
     out = im.resize((800, 400), Image.ANTIALIAS)
     out.save(image_path)
Beispiel #11
0
 def show(self, class_key: dict, title: str, image_name):
     # print(self.show_coordinates)
     scatter = Scatter(title=title, background_color='#ffe')
     for i in range(0, self.show_coordinates.shape[0]):
         point = self.show_coordinates.iloc[i]['point']
         # print(point, class_key[point])
         coordinates = self.show_coordinates.iloc[i]['vector']
         # print(type(coordinates), type(class_key[point]))
         scatter.add(class_key[point], [coordinates[0]], [coordinates[1]])
     temp_path = 'html/render.html'
     scatter.render(path=temp_path)
     make_a_snapshot(temp_path, image_name)
Beispiel #12
0
def save_picture(name, data, p_path):
    if len(data) == 0 or p_path is None:
        return
    data_value = [x["value"] for x in data]
    line = Line(name, background_color='#EAEAEA')
    attr = list(range(0, len(data_value)))
    line.add("delta",
             attr,
             data_value,
             mark_point=["max", "min"],
             mark_line=["average"])
    render_file = "{}/{}.html".format(p_path, name)
    picture_file = "{}/{}.png".format(p_path, name)
    line.render(path=render_file, )
    make_a_snapshot(render_file, picture_file)
    print("save picture {}.png in {}".format(name, p_path))
Beispiel #13
0
 def post_salary_counter():
     """ 薪酬统计
     """
     with open(os.path.join("data", "post_salary.csv"),
               "r",
               encoding="utf-8") as f:
         f_csv = csv.reader(f)
         lst = [row[0] for row in f_csv]
     counter = Counter(lst).most_common()
     #pprint(counter)
     x = [0] * len(counter)
     y = [0] * len(counter)
     i = 0
     counter.sort()
     for c in counter:
         x[i] = float(c[0]) * 10000
         y[i] = c[1]
         i = i + 1
     func = interpolate.interp1d(x, y, kind='cubic')
     x = np.arange(x[0] + 50, x[-1] - 50, 10)  # 差值平均化,使线条平滑
     y = func(x)
     i = 0
     for xz in y:
         if xz < 0:
             y[i] = 0
             i = i + 1
         else:
             y[i] = y[i]
             i = i + 1
     line = Line("")
     line.add("",
              x,
              y,
              is_fill=True,
              line_opacity=0.2,
              area_opacity=0.4,
              symbol=None)
     line.render('xz.html')
     make_a_snapshot('xz.html', os.path.join("images", "money.png"))
     with open(os.path.join("data", "post_salary_counter1.csv"),
               "w+",
               encoding="utf-8",
               newline='') as f:
         f_csv = csv.writer(f)
         f_csv.writerows(counter)
Beispiel #14
0
 def post_salary_localcounter():
     """ 地点统计
     """
     with open(os.path.join("data", "post_salary_locate.csv"),
               "r",
               encoding="utf-8") as f:
         f_csv = csv.reader(f)
         lst = []
         for row in f_csv:
             row[2] = row[2].split("-")[0]
             lst.append(row[2])
     counter = Counter(lst).most_common()
     x = [0] * len(counter)
     y = [0] * len(counter)
     i = 0
     counter.sort()
     for c in counter:
         x[i] = c[0]
         y[i] = c[1]
         i = i + 1
     geo = Geo("",
               "",
               title_color="#fff",
               title_pos="center",
               width=800,
               height=500,
               background_color='#404a59')
     geo.add("",
             x,
             y,
             is_visualmap=True,
             visual_range=[0, max(y)],
             symbol_size=15,
             visual_text_color='#fff')
     geo.render('dd.html')  # 生成html
     make_a_snapshot('dd.html', os.path.join("images",
                                             "locate.png"))  # 利用快照生成png图片
     with open(os.path.join("data", "post_local.csv"),
               "w+",
               encoding="utf-8",
               newline='') as f:
         f_csv = csv.writer(f)
         f_csv.writerows(counter)
Beispiel #15
0
def get_chart_image():
    attr = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
        "Nov", "Dec"
    ]
    v1 = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
    v2 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
    bar = Bar("Bar chart", "precipitation and evaporation one year")
    bar.add("precipitation",
            attr,
            v1,
            mark_line=["average"],
            mark_point=["max", "min"])
    bar.add("evaporation",
            attr,
            v2,
            mark_line=["average"],
            mark_point=["max", "min"])
    bar.render('test.html')
    make_a_snapshot('test.html', 'test.png')
    return 'test.png'
from pyecharts import Bar
from pyecharts_snapshot.main import make_a_snapshot

bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])
bar.print_echarts_options()
bar.render()
make_a_snapshot('render.html', 'snapshot.png')
def test_unsupported_file_type():
    # cannot produce a consistent binary matching file
    test_output = 'real.shady'
    make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                    test_output)
def test_make_a_snapshot_real_pdf():
    test_output = 'real.pdf'
    make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                    test_output)
    assert(os.path.exists(test_output))  # exists just fine
Beispiel #19
0
def test_make_png_snapshot(fake_check, fake_popen):
    fake_popen.return_value.stdout = BytesIO(get_base64_image())
    test_output = "custom.png"
    make_a_snapshot(get_fixture("render.html"), test_output)
    assert filecmp.cmp(test_output, get_fixture("sample.png"))
Beispiel #20
0
 def mytr(self,year,month):
     make_a_snapshot(("db%d.html") % (year * 100 + month),("db%d.png") % (year * 100 + month))
def test_phantomjs_fails(fake_popen):
    fake_popen.return_value.stdout = BytesIO('abc')
    make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                    'custom.jpeg')
def test_make_png_snapshot(fake_popen):
    fake_popen.return_value.stdout = BytesIO(get_base64_image())
    test_output = 'custom.png'
    make_a_snapshot(os.path.join("tests", "fixtures", "render.html"),
                    test_output)
    assert(filecmp.cmp(test_output, get_fixture('sample.png')))
Beispiel #23
0
 def mytr(self):
     make_a_snapshot("2015_to_2018.html", "2015_to_2018.png")
Beispiel #24
0
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> # Part 2 Draw Pie Chart with Pyecharts
>>> from pyecharts import Pie
>>> from pyecharts_snapshot.main import make_a_snapshot #for creating output in forms of PNG and PDF
>>> attr=["V40","V60","V90","S90","S60","XC40","XC60","XC90","Polestar1"]
>>> v1=[500,200,360,1000,750,300,1200,1800,100]
>>> pie=Pie("MSales")
>>> pie.add("",attr,v1,is_label_show=True)
<pyecharts.charts.pie.Pie object at 0x03178F50>
>>> pie.render()
>>> make_a_snapshot('render.html', 'snapshot.png') # output png
phantomjs version: 2.1.1

Generating file ...
File saved in C:\Users\LylionCj\AppData\Local\Programs\Python\Python36-32\snapshot.png
>>> make_a_snapshot('render.html', 'snapshot.pdf') # output PDF
phantomjs version: 2.1.1

Generating file ...
File saved in C:\Users\LylionCj\AppData\Local\Programs\Python\Python36-32\snapshot.pdf
>>> 
from __future__ import unicode_literals
from pyecharts import Bar
from pyecharts_snapshot.main import make_a_snapshot
'''
如果想直接将图片保存为 png, pdf, gif 格式的文件,可以使用 pyecharts-snapshot

pip install pyecharts
保存为图片格式的插件

安装 phantomjs
npm install -g phantomjs-prebuilt

安装 pyecharts-snapshot
pip3 install pyecharts-snapshot
pip3 install pyecharts-snapshot==0.1.7


引入 pyecharts-snapshot
from pyecharts_snapshot.main import make_a_snapshot

调用方法
make_a_snapshot('render.html', 'snapshot.png')

'''

bar = Bar("我的第一个图表", "这里是副标题")
bar.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90])
# bar.print_echarts_options()
bar.render(path='snapshot.html')
bar.render(path='snapshot.png')
bar.render(path='snapshot.pdf')
Beispiel #26
0
            data_f.append(data[i - 1])
        for part in data_f:
            data_tmp = []
            for i in row:
                data_tmp.append(part[i - 1])
            data_f_f.append(data_tmp)

if chartid == 0:
    pie = Pie("", width=600, height=500)
    for part in range(0, len(categerise_f), +1):
        pie.add(categerise_f[part],
                legend_f,
                data_f_f[part],
                is_label_show=True)
        pie.render()
        make_a_snapshot('render.html', "pie.jpeg", 1)
        os.remove("render.html")
        shutil.copyfile("pie.jpeg", p_path)
        os.remove("pie.jpeg")

if chartid == 1:
    bar = Bar(title="", width=800, height=400)
    for part in range(0, len(categerise_f), +1):
        bar.add(categerise_f[part],
                legend_f,
                data_f_f[part],
                is_label_show=True,
                xaxis_name=xname,
                yaxis_name=yname,
                xaxis_name_pos="end",
                yaxis_name_pos="end",
Beispiel #27
0
line = Line("折线图示例", width = 1200)
attr_line = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
data1_line = [11, 11, 15, 13, 12, 13, 10]
data2_line = [1, -2, 2, 5, 3, 2, 0]
line.add("最高气温", 
		 attr_line, data1_line, 
		 mark_point = ["max", "min"], 
		 mark_line = ["average"])
line.add("最低气温", 
		 attr_line, data2_line,
		 mark_point = ["max", "min"], 
		 mark_line = ["avergae"], 
		 legend_pos = "20%")

# Pie
attr_pie = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
data_pie = [11, 12, 13, 10, 10, 10]
pie = Pie("饼图示例", title_pos = "45%")
pie.add("", 
		attr_pie, data_pie, 
		radius = [30, 55],
		legend_pos = "65%",
		legend_orient = "horizonial")

# Grid
grid = Grid()
grid.add(line, grid_right = "65%")
grid.add(pie, grid_left = "60%")
grid.render("/home/wangzhefeng/project/DataVisualiztion/pyecharts/grid_test.html")
make_a_snapshot("grid_test.html", "grid_test.pdf")
Beispiel #28
0
result.to_csv(path + 'output/sum.csv', encoding="gbk")

# In[13]:

from pyecharts import Bar, WordCloud
from pyecharts_snapshot.main import make_a_snapshot

# In[14]:

n = result[result['count'] > 4000]['n'].tolist()
count = result[result['count'] > 4000]['count'].tolist()
bar1 = Bar('名词排序')
bar1.add("", n, count, is_convert=True)
bar1.render('bar1.html')
make_a_snapshot('bar1.html', path + '/output/bar1.png')
bar1

# In[15]:

n = result[result['count'] > 30]['n'].tolist()
count = result[result['count'] > 30]['count'].tolist()

cloud = WordCloud("词云")
cloud = WordCloud(width=900, height=600)
cloud.add('名词词云', n, count, word_size_range=[20, 150], shape='star')
cloud.render('cloud.html')
make_a_snapshot('cloud.html', path + '/output/cloud.png')

cloud
Beispiel #29
0
 def mytr(self, year, top):
     make_a_snapshot("wc%d.html" % (year * 100 + top),
                     ("wc%d.png" % (year * 100 + top)))
Beispiel #30
0
 def chart(self):
     bar = Bar("ebooking")
     bar.add("60客户", self.percent, self.sortList, is_stack=True)
     bar.render()
     make_a_snapshot('render.html', './pic/%s.png' % self.name)