def render(self, path="render.html", template_name="simple_chart.html", object_name="chart", **kwargs): """ 将图表保存成html文件,默认占满屏幕 :param path: :param template_name: :param object_name: :param kwargs: :return: """ # 调用父类的方法生成html文件 super(Bubble4D, self).render(path, "simple_chart.html", "chart", **kwargs) with open(path, 'r', encoding="utf-8") as file: cont = file.read() # 修改html文件使tooltip可以正常显示 pos1 = re.search("myChart\w*.setOption(.*?);", cont).span()[1] +1 cont = cont[: pos1] + "var columns_list = {0};\n".format(self.my_option['columns_list']) + cont[pos1:] # 修改html文件使图表全屏显示 try: pos2 = re.search("<html>", cont).span()[1] - 1 cont = cont[: pos2] + ' style="height: 100%"' + cont[pos2: ] pos2 = re.search("<body>", cont).span()[1] - 1 cont = cont[: pos2] + ' style="height: 100%; margin: 0"' + cont[pos2:] pos3, pos4 = re.search('style="width:.*?;height:.*?;"', cont).span() cont = cont[: pos3] + ' style="width: 100%; height: 100%;"' + cont[pos4:] except: pass # 保存修改后的html文件 utils.write_utf8_html_file(path, cont)
def test_write_utf8_html_file(): content = "柱状图数据堆叠示例" file_name = 'test.html' write_utf8_html_file(file_name, content) with codecs.open(file_name, 'r', 'utf-8') as f: actual_content = f.read() eq_(content, actual_content)
def test_write_utf8_html_file(): content = "柱状图数据堆叠示例" file_name = "test.html" write_utf8_html_file(file_name, content) with codecs.open(file_name, "r", "utf-8") as f: actual_content = f.read() eq_(content, actual_content)
def render(self, path="render.html", template_name="simple_chart.html", object_name="chart", **kwargs): """ 将图表保存成html文件,默认占满屏幕 :param path: :param template_name: :param object_name: :param kwargs: :return: """ super(MapBin, self).render(path, "simple_chart.html", "chart", **kwargs) with open(path, 'r', encoding="utf-8") as file: cont = file.read() # 查找myChart的id temp_start, temp_end = re.search("myChart_(.*?).setOption", cont).span() myChart_id = cont[temp_start: temp_end].replace('myChart_', '').replace('.setOption', '') # 导入百度接口和bmap.js try: pos1 = re.search("<body>\n\s*<div id=", cont).span()[0] + 7 cont = cont[: pos1] + """<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ZUONbpqGBsYGXNIYHicvbAbM"></script>\n<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"></script>\n""" + cont[pos1:] except: pass # 使tooltip可以正常显示 pos1 = re.search("myChart\w*.setOption(.*?);", cont).span()[1] + 1 cont = cont[: pos1] + "var binmap_columns_list = {0};\n".format(self.my_option['binmap_columns_list']) + cont[pos1:] # 使图表全屏显示 try: pos2 = re.search("<html>", cont).span()[1] - 1 cont = cont[: pos2] + ' style="height: 100%"' + cont[pos2:] pos2 = re.search("<body>", cont).span()[1] - 1 cont = cont[: pos2] + ' style="height: 100%; margin: 0"' + cont[pos2:] pos3, pos4 = re.search('style="width:.*?;height:.*?;"', cont).span() cont = cont[: pos3] + ' style="width: 100%; height: 100%;"' + cont[pos4:] except: pass if self.my_option['search_box']: # 添加搜索框和按钮 pos5 = re.search('<div id="{0}"(.*?)></div>'.format(myChart_id), cont).span()[1] + 1 cont = cont[: pos5] + '<div id="left-panel" style="height: 654px;position: absolute;left: 20px;top: 20px;overflow: hidden;pointer-events: none;">\n <div id="searchbox" class="clearfix" style="border-radius: 2px;width: 425px;position: relative;z-index: 5;zoom: 1;">\n <div id="searchbox-container" style="position: relative;z-index: 2;pointer-events: auto;width: 200px;float: left;box-sizing: border-box;box-shadow: 1px 2px 1px rgba(0, 0, 0, .15);">\n <div id="sole-searchbox-content" class="searchbox-content" style="position: relative;width: 200px;border-radius: 2px 0 0 2px;background: #fff;">\n <input id="sole-input" class="searchbox-content-common" placeholder="以名称定位方格、点" style="position: relative;height: 38px;box-sizing: border-box;border: 0;padding: 9px 0;border-left: 10px solid transparent;border-right: 27px solid transparent;border-radius: 2px 0 0 2px;line-height: 20px;font-size: 16px;color: #333;box-sizing: border-box;float: left; width: 200px;height: 38px;"\n type="text" name="word" autocomplete="off" maxlength="256">\n </div>\n </div>\n </div>\n <button id="search-button" onclick="search()" style="width: 50px;height: 38px;float: left;pointer-events: auto;background: url(http://webmap1.bdimg.com/wolfman/static/common/images/new/searchbox_f175577.png) no-repeat -3px -76px ' + self.my_option['lightness_color'][3] + ';border: 0;padding: 0;cursor: pointer;border-radius: 0 2px 2px 0;box-shadow: 1px 2px 1px rgba(0, 0, 0, .15);"></button>\n</div>\n' + cont[pos5:] if self.my_option['time_line_col'] is None: search_function_str = 'function search() {\n let search_text = document.getElementById("sole-input").value;\n let data = option_%s["series"][0]["data"];\n let flag = false;\n for (var i = 0; i < data.length; i++) {\n if (search_text == data[i][0].toString()) {\n console.log(data[i]);\n var pt = new BMap.Point((data[i][1] + data[i][3] / 2).toFixed(6), (data[i][2] + data[i][4] / 2).toFixed(6));\n infoContent = "麻烦你用力<br>去吻碎<br>这相反故事";\n var infoWindow = new BMap.InfoWindow(infoContent, infoOpts); // 创建信息窗口对象\n bmap.openInfoWindow(infoWindow, pt); //开启信息窗口\n flag = true;\n break;\n }\n }\n if (!flag) {\n console.log("can\'t find");\n alert("而事到如今\\n终于明白\\n我图里没你");\n }\n}\n' % myChart_id else: search_function_str = 'function search() {\n let search_text = document.getElementById("sole-input").value;\n let current_index = myChart_%s.getOption()["timeline"][0]["currentIndex"];\n let data = option_%s["options"][current_index]["series"][0]["data"];\n let flag = false;\n for (var i = 0; i < data.length; i++) {\n if (search_text == data[i][0].toString()) {\n console.log(data[i]);\n var pt = new BMap.Point((data[i][1] + data[i][3] / 2).toFixed(6), (data[i][2] + data[i][4] / 2).toFixed(6));\n infoContent = "麻烦你用力<br>去吻碎<br>这相反故事";\n var infoWindow = new BMap.InfoWindow(infoContent, infoOpts); // 创建信息窗口对象\n bmap.openInfoWindow(infoWindow, pt); //开启信息窗口\n flag = true;\n break;\n }\n }\n if (!flag) {\n console.log("can\'t find");\n alert("而事到如今\\n终于明白\\n我图里没你");\n }\n}\n' % (myChart_id, myChart_id) # 获取地图实例、添加信息窗模板 pos6 = re.search("myChart\w*.setOption(.*?);", cont).span()[1] + 1 temp_str = "var bmap = myChart_{0}.getModel().getComponent('bmap').getBMap();\n".format(myChart_id) temp_str += 'var infoOpts = {\n width: 50, // 信息窗口宽度\n height: 50, // 信息窗口高度\n // title: "信息", // 信息窗口标题\n enableMessage: true //设置允许信息窗发送短息\n};\n' temp_str += search_function_str cont = cont[: pos6] + temp_str + cont[pos6: ] # 保存修改后的html文件 utils.write_utf8_html_file(path, cont)
def render(self, path='render.html', template_name='simple_chart.html', object_name='chart', extra_context=None): context = {object_name: self} context.update(extra_context or {}) html = engine.render(template_name, **context) utils.write_utf8_html_file(path, html)
def test_echarts_js_dependencies(): env = EchartsEnvironment(pyecharts_config=PyEchartsConfig() # pyecharts_config=PyEchartsConfig() ) tpl = env.get_template('tpl_demo.html') bar = create_demo_bar() html = tpl.render(bar=bar) # flake8: noqa write_utf8_html_file('my_tpl_demo4.html', html)
def dataframe_line_plot(df, title='', path=None): option = { 'title': { 'text': title, 'left': 'center' }, 'legend': { 'show': True, 'orient': 'vertical', 'right': '9%', 'top': '3%' }, 'xAxis': { 'type': 'time', 'axisLine': { 'onZero': False } }, 'yAxis': { 'axisLine': { 'onZero': False } }, 'dataZoom': [{ 'type': 'slider', 'xAxisIndex': 0, 'start': 0, 'end': 50 }] } time = [ts.timestamp() / 0.001 for ts in df.index] series = [] for channel in df.columns: values = df[channel].values.tolist() data = [(t, x) for t, x in zip(time, values)] series.append({ 'type': 'line', 'showSymbol': False, 'name': channel, 'data': data }) option.update({'series': series}) line_plot = Line() line_plot._option.update(option) conf = PyEchartsConfig(echarts_template_dir='html', jshost='js', force_js_embed=False) env = EchartsEnvironment(pyecharts_config=conf) tpl = env.get_template('template.html') html = tpl.render(chart=line_plot) write_utf8_html_file(path, html)
def render(self, path="render.html"): """ Produce rendered charts in a html file :param path: :return: """ template_name = "multicharts.html" chart_content = self.render_embed() dependencies = self._merge_dependencies() script_list = produce_html_script_list(dependencies) tmp = template.JINJA2_ENV.get_template(template_name) html = tmp.render(multi_chart_content=chart_content, script_list=script_list) html = utils.freeze_js(html) utils.write_utf8_html_file(path, html)
def render(self, path="render.html"): """ Render the options dict, generate the html file :param path: path of render html file """ _tmp = "local.html" my_option = json_dumps(self._option, indent=4) tmp = template.JINJA2_ENV.get_template(_tmp) script_list = template.produce_html_script_list(self._js_dependencies) html = tmp.render(myOption=my_option, chart_id=self._chart_id, script_list=script_list, myWidth=self._width, myHeight=self._height) html = utils.freeze_js(html) utils.write_utf8_html_file(path, html)
def render(self, path="render.html"): """ 渲染配置项并生成 html 文件 :param path: 文件保存路径 """ _tmp = "local.html" my_option = json_dumps(self._option, indent=4) tmp = template.JINJA2_ENV.get_template(_tmp) script_list = template.produce_html_script_list(self._js_dependencies) html = tmp.render(my_option=my_option, chart_id=self._chart_id, script_list=script_list, page_title=self._page_title, my_width=self.width, my_height=self.height) html = utils.freeze_js(html) utils.write_utf8_html_file(path, html)
def genReport(self, filetype="html", timeout=60): attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图数据堆叠示例") bar.add("商家A", attr, v1, is_stack=True, is_toolbox_show=False) bar.add("商家B", attr, v2, is_stack=True) config = PyEchartsConfig(echarts_template_dir=Report.TEMPLATE_DIR) env = EchartsEnvironment(pyecharts_config=config) tpl = env.get_template(Report.REPORT_HTML) html = tpl.render(bar=bar) name = 'report_%s'%self.getUID() write_utf8_html_file("%s/%s.html"%(Report.OUT_DIR, name), html) if "pdf"==filetype: cmd = "phantomjs make_pdf.js %s"%name self.PopenWithTimeout(cmd, cwd=Report.OUT_DIR, timeout=timeout)
def render_chart_to_file(self, chart, object_name="chart", path="render.html", template_name="simple_chart.html", **kwargs): """ Render a chart or page to local html files. :param chart: A Chart or Page object :param object_name: Variable name for chart/page used in template :param path: The destination file which the html code write to :param template_name: The name of template file. :return: None """ kwargs[object_name] = chart tpl = self.get_template(template_name) html = tpl.render(**kwargs) utils.write_utf8_html_file(path, html)
def render(self, path="render.html", template_name="simple_chart.html", object_name="chart", **kwargs): """ 将图表保存成html文件,默认占满屏幕 :param path: :param template_name: :param object_name: :param kwargs: :return: """ super(GeoArcs, self).render(path, "simple_chart.html", "chart", **kwargs) with open(path, 'r', encoding="utf-8") as file: cont = file.read() # 导入地图js pos1 = re.search("<body>\n\s*<div id=", cont).span()[0] + 7 cont = cont[: pos1] + \ """<script type="text/javascript" src="http://echarts.baidu.com/gallery/""" + \ """vendors/echarts/map/js/china.js"></script>\n""" + cont[pos1:] # 使图表全屏显示 try: pos2 = re.search("<html>", cont).span()[1] - 1 cont = cont[:pos2] + ' style="height: 100%"' + cont[pos2:] pos2 = re.search("<body>", cont).span()[1] - 1 cont = cont[:pos2] + ' style="height: 100%; margin: 0"' + cont[ pos2:] pos3, pos4 = re.search('style="width:.*?;height:.*?;"', cont).span() cont = cont[:pos3] + ' style="width: 100%; height: 100%;"' + cont[ pos4:] except: pass # 保存修改后的html文件 utils.write_utf8_html_file(path, cont)
def render_chart_to_file(self, chart, object_name='chart', path='render.html', template_name='simple_chart.html', extra_context=None): """ Render a chart or page to local html files. :param chart: A Chart or Page object :param object_name: Variable name for chart/page used in template :param path: The destination file which the html code write to :param template_name: The name of template file. :param extra_context: A dictionary containing extra data. :return: None """ context = {object_name: chart} context.update(extra_context or {}) tpl = self.get_template(template_name) html = tpl.render(**context) utils.write_utf8_html_file(path, html)
def render(self, path="render.html", template_name="simple_chart.html", object_name="chart", **kwargs): """ 重写了父类的该函数 :param path: :param template_name: :param object_name: :param kwargs: :return: """ super(ColorMap, self).render(path, "simple_chart.html", "chart", **kwargs) with open(path, 'r', encoding="utf-8") as file: cont = file.read() # 导入自定义地图的geoJson,并注册该地图 pos1 = re.search("<body>\n\s*<div id=", cont).span()[0] + 7 cont = cont[: pos1] + """<script type="text/javascript">\n var geoJson = {0};\n""".format(self.geoJson) + \ """ echarts.registerMap('chinacity', geoJson);\n</script>\n""" + cont[pos1:] # 使图表全屏显示 try: pos2 = re.search("<html>", cont).span()[1] - 1 cont = cont[:pos2] + ' style="height: 100%"' + cont[pos2:] pos2 = re.search("<body>", cont).span()[1] - 1 cont = cont[:pos2] + ' style="height: 100%; margin: 0"' + cont[ pos2:] pos3, pos4 = re.search('style="width:.*?;height:.*?;"', cont).span() cont = cont[:pos3] + ' style="width: 100%; height: 100%;"' + cont[ pos4:] except: pass # 保存修改后的html文件 utils.write_utf8_html_file(path, cont)
def render_chart_to_file( self, chart, object_name="chart", path="render.html", template_name="simple_chart.html", **kwargs ): """ Render a chart or page to local html files. :param chart: A Chart or Page object :param object_name: Variable name for chart/page used in template :param path: The destination file which the html code write to :param template_name: The name of template file. :param extra_context: A dictionary containing extra data. :return: None """ kwargs[object_name] = chart tpl = self.get_template(template_name) html = tpl.render(**kwargs) utils.write_utf8_html_file(path, html)
xiaoqu_bar = Bar("{0}小区均价".format(city), width=1200) xiaoqu_bar.add( "小区均价排名", xqs, prices, is_label_show=True, is_more_utils=True, xaxis_interval=0, xaxis_rotate=30, is_datazoom_show=True, datazoom_type="both", datazoom_range=[10, 10.3], ) tpl_render = tpl.render(bar=xiaoqu_bar) path = "html/{0}_xiaoqu_bar.html".format(ct) write_utf8_html_file(path, tpl_render) print("save to ", path) #################################################### # 区县均价排名 #################################################### district_df = df.groupby('district_full').mean() district_df = district_df.round(0) district_df.sort_values("price", ascending=False, inplace=True) print(district_df) districts = district_df.index prices = district_df["price"] district_bar = Bar("{0}区县均价".format(city), width=1200) district_bar.add("区县均价排名", districts, prices,
from pyecharts import Line from pyecharts.conf import PyEchartsConfig from pyecharts.engine import EchartsEnvironment from pyecharts.utils import write_utf8_html_file from pyecharts import ThemeRiver attr = ['a', 'b', 'c', 'd'] v1 = [40000, 50000, 60000, 70000] pie = Pie("饼图-星级玫瑰图示例", title_pos='center', width=900) pie.add("7-17", attr, v1, center=[75, 50], is_random=True, radius=[30, 75], rosetype='area', is_legend_show=False, is_label_show=True) #pie.render() config = PyEchartsConfig(echarts_template_dir='./') env = EchartsEnvironment(pyecharts_config=config) #tpl = env.get_template('chart_template.html') #html = tpl.render(pie=pie) write_utf8_html_file('chart_out.tif') ## from pyecharts import Gauge gauge = Gauge("仪表盘示例") gauge.add("业务指标", "完成率", 66.66) gauge.show_config() gauge.render()
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ from __future__ import unicode_literals from pyecharts import Bar from pyecharts.conf import PyEchartsConfig from pyecharts.engine import EchartsEnvironment from pyecharts.utils import write_utf8_html_file attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图数据堆叠示例") bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True) config = PyEchartsConfig(echarts_template_dir='./', jshost='https://cdn.bootcss.com/echarts/3.6.2') env = EchartsEnvironment(pyecharts_config=config) tpl = env.get_template('tpl.html') html = tpl.render(bar=bar) write_utf8_html_file('my_tpl_demo2.html', html)
# coding=utf8 from __future__ import unicode_literals from pyecharts import Bar from pyecharts.conf import PyEchartsConfig from pyecharts.engine import EchartsEnvironment from pyecharts.utils import write_utf8_html_file attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图数据堆叠示例") bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True) config = PyEchartsConfig(echarts_template_dir='my_tpl', jshost='https://cdn.bootcss.com/echarts/3.6.2') env = EchartsEnvironment(pyecharts_config=config) tpl = env.get_template('tpl_demo.html') html = tpl.render(bar=bar) write_utf8_html_file('my_tpl_demo2.html', html)
from __future__ import unicode_literals from pyecharts import Bar from pyecharts.conf import PyEchartsConfig from pyecharts.engine import EchartsEnvironment from pyecharts.utils import write_utf8_html_file attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图数据堆叠示例") bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True) config = PyEchartsConfig(echarts_template_dir='template', jshost='https://cdn.bootcss.com/echarts/3.6.2') env = EchartsEnvironment(pyecharts_config=config) tpl = env.get_template('demo.html') html = tpl.render(bar=bar) write_utf8_html_file("my_demo.html", html)
#coding=utf-8 from __future__ import unicode_literals from pyecharts import Line from pyecharts.conf import PyEchartsConfig from pyecharts.engine import EchartsEnvironment from pyecharts.utils import write_utf8_html_file attr = ["2018/03/11", "2018/03/18", "2018/03/25", "2018/04/01", "2018/04/08"] selectedColumn = "Visitor Count" column = 'Column: {}'.format(selectedColumn) line = Line(column) v1 = [661.0, 359.0, 358.0, 536.0, 391.0] v2 = [102.0, 906.0, 84.0, 878.0, 115.0] line.add("VP1", attr, v1, mark_point=["average", "max", "min"]) line.add("VP2", attr, v2, mark_point=["average", "max", "min"]) line.add("Filter", attr, [430] * len(v1), is_fill=True, area_opacity=0.3, is_smooth=True) #,is_toolbox_show=False config = PyEchartsConfig(echarts_template_dir='./') env = EchartsEnvironment(pyecharts_config=config) tpl = env.get_template('chart_template.html') html = tpl.render(line=line) write_utf8_html_file('chart_out.html', html)