实现折线图
import pyecharts.options as opts
from pyecharts.charts import Line, Map
from pyecharts.commons.utils import JsCode
import pymysql
from pyecharts.globals import ThemeType
connection = pymysql.connect(host='localhost',
user='root',
passwd='root',
db='phicdata',
port=3306,
charset='utf8'
)
cur = connection.cursor() # 游标(指针)cursor的方式操作数据
sql = "select year1,year2,year3,year4,year5,year6,year7,year8,year9,year10,year11,year12,year13,year14,year15 from phic1 where id=1 or id=2 or id=3 or id=4 or id=5 or id=5 or id=6 or id=7 or id=8" # sql语句
cur.execute(sql) #execute(query, args):执行单条sql语句。
results=cur.fetchall() #使结果全部可看
print(results)
#转换成列表形式,以便于填入图表的数据
data1=results[0]
h=list(data1)
data2=results[1]
i=list(data2)
data3=results[2]
j=list(data3)
data4=results[3]
k=list(data4)
data5=results[4]
l=list(data5)
data6=results[5]
m=list(data6)
data7=results[6]
n=list(data7)
data8=results[7]
o=list(data8)
print(h)
def line_scatter() -> Line:
js_formatter = """function (params) {
console.log(params);
return '人口数量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
}"""
line = (
# Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))
Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(
xaxis_data=[
"2004",
"2005",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2017",
"2018",
]
)
.extend_axis(
xaxis_data=[
"2004",
"2005",
"2007",
"2008",
"2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2017",
"2018",
],
xaxis=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),