Python读取Hbase静态数据并绘制多个Echarts图

Python读取Hbase静态数据并绘制多个Echarts图_第1张图片

from pyecharts import Bar, Line, Scatter, EffectScatter, Grid, Pie

import happybase
import pyecharts
#bytes.decode('utf-8')
#from pyecharts.charts import Bar
from pyecharts import Bar
conn=happybase.Connection("localhost",9090)
table=conn.table("gdp1")
row=table.row("AnHui_Province")
#print((row))
Anhui_x=[]
Anhui_y=[]
Anhui_x1=[]
#print (row[b'year:2015'].decode('utf-8'))
for i in range(2000,2015):
    Anhui_x.append(i)
    Anhui_x1.append(str(i))
    a='year:'+str(i)
    a=bytes(a,encoding='utf-8')
    Anhui_y.append(int(row[a]))
    #print(row[a])
# print (Anhui_x)
# print (Anhui_y)
row=table.row("JiangSu_Province")
#print((row))
JS_x=[]
JS_y=[]
JS_x1=[]
#print (row[b'year:2015'].decode('utf-8'))
for i in range(2000,2015):
    JS_x.append(i)
    JS_x1.append(str(i))
    a='year:'+str(i)
    a=bytes(a,encoding='utf-8')
    JS_y.append(int(row[a]))
# print(JS_y)
keys=[]
eco=[]
dic={
     }
scaner=table.scan()
for k,v in scaner:
    row = table.row(k)
    a = 'year:2017'
    a = bytes(a, encoding='utf-8')
    eco.append(int(row[a]))
    #k = bytes(k, encoding='utf-8')
    k=k.decode('utf-8')
    #bytes.decode('utf-8')
    keys.append(k)
    dic[k]=int(row[a])
    # print(k)
# print(keys)
# print(eco)
# print(dic)
key=[]
va=[]
A=sorted(dic.items(),key=lambda dic:dic[1],reverse=True)
j=0
for i in A:
    key.append(i[0])
    va.append(i[1])
    j=j+1
    if j>5:
        break
# print(key)
# print(va)
# single=[]
year=[]
year_str=[]
all=[]
#全国总经济走势

for i in range(1999, 2018):
    # if i==1997:
    #     continue
    year_str.append(str(i))
    a = 'year:' + str(i)
    year.append(i-1998)
    a = bytes(a, encoding='utf-8')
    sum=0
    scaner = table.scan()
    for k,v in scaner:
        # print(k)

        row = table.row(k)
        #print(int(row[a]))
        sum=sum+int(row[a])
        # single.append(int(row[a]))
    all.append(sum)
print(year)
print(all)
# print(table)
'''
    Grid类:并行显示多个图表 TODO 第一个图需为 有 x/y 轴的图,即不能为 Pie,其他位置顺序任意。
'''
attr=Anhui_x

v1=Anhui_y
bar = Bar("经济柱状图", title_pos="65%")
bar.add("安徽", attr, v1, is_stack=True)
bar.add("江苏", JS_x, JS_y, is_stack=True, legend_pos="80%")

line = Line("经济折线图")
attr=Anhui_x1
line.add(
    "安徽",
    attr,
    Anhui_y,
    mark_point=["max", "min"],
    mark_line=["average"],
)
line.add(
    "江苏",
    JS_x1,
    JS_y,
    mark_point=["max", "min"],
    mark_line=["average"],
    legend_pos="20%",
)

v1 = year
v2 = all
scatter = Scatter("全国总经济散点图", title_top="50%", title_pos="65%")
scatter.add("scatter", v1, v2, legend_top="50%", legend_pos="80%",
           # min_='dataMin',
            )


x = key
y = va
pie = Pie("2017年前六经济饼图",title_top="50%",title_pos="20")
pie.add(
    "2017",
    x,
    y,
    radius=[15,35],
    center=[25,80],
    legend_top="70%",
    legend_pos="5%",
    legend_orient="vertical",
)


# TODO 配置Grid类
grid = Grid(height=720, width=1200) # 初始化,参数可传page_title,width,height
grid.add(bar, grid_bottom="60%", grid_left="60%")  # 添加要展示的图表,并设置显示位置
grid.add(line, grid_bottom="60%", grid_right="60%") # 添加要展示的图表,并设置显示位置
grid.add(scatter, grid_top="60%", grid_left="60%") # 添加要展示的图表,并设置显示位置
grid.add(pie,grid_top="60%", grid_right="60%") # 添加要展示的图表,并设置显示位置

grid.render("./templates/index.html")

你可能感兴趣的:(HBase,Python,echarts,python,hbase,echarts)