用数据库建表保存某公司在郑州和洛阳两地2021和2022年的5种不同品牌手机的累计销售额,利用pyecharts选择合适可视化图进行展示,要求能够利用工具框切换为其他不同类型图表,画布上方居中有本人学号姓名等信息。
时间: 2023-06-15 15:05:18 浏览: 84
数据库建表
建表:
```sql
CREATE TABLE phone_sales (
id INT PRIMARY KEY AUTO_INCREMENT,
city VARCHAR(10),
brand VARCHAR(20),
year INT,
sales BIGINT
);
```
插入数据:
```sql
INSERT INTO phone_sales(city, brand, year, sales) VALUES
('郑州', '品牌A', 2021, 1000000),
('郑州', '品牌A', 2022, 1500000),
('郑州', '品牌B', 2021, 800000),
('郑州', '品牌B', 2022, 1200000),
('郑州', '品牌C', 2021, 500000),
('郑州', '品牌C', 2022, 800000),
('郑州', '品牌D', 2021, 400000),
('郑州', '品牌D', 2022, 600000),
('郑州', '品牌E', 2021, 200000),
('郑州', '品牌E', 2022, 300000),
('洛阳', '品牌A', 2021, 500000),
('洛阳', '品牌A', 2022, 700000),
('洛阳', '品牌B', 2021, 400000),
('洛阳', '品牌B', 2022, 600000),
('洛阳', '品牌C', 2021, 300000),
('洛阳', '品牌C', 2022, 500000),
('洛阳', '品牌D', 2021, 200000),
('洛阳', '品牌D', 2022, 300000),
('洛阳', '品牌E', 2021, 100000),
('洛阳', '品牌E', 2022, 150000);
```
利用 pyecharts 可视化,代码如下:
```python
import pymysql
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Grid, Tab
from pyecharts.commons.utils import JsCode
# 连接数据库
db = pymysql.connect("localhost", "root", "password", "testdb")
cursor = db.cursor()
# 查询数据
sql = "SELECT city, brand, year, sales FROM phone_sales"
cursor.execute(sql)
results = cursor.fetchall()
# 处理数据
data = {}
for row in results:
city, brand, year, sales = row
if year not in data:
data[year] = {}
if city not in data[year]:
data[year][city] = {}
data[year][city][brand] = sales
# 生成图表
def generate_chart(data, chart_type):
x_data = ['品牌A', '品牌B', '品牌C', '品牌D', '品牌E']
y_data_2021 = []
y_data_2022 = []
for brand in x_data:
y_data_2021.append(data.get(2021, {}).get(brand, 0))
y_data_2022.append(data.get(2022, {}).get(brand, 0))
if chart_type == 'bar':
chart = Bar()
chart.add_xaxis(x_data)
chart.add_yaxis("2021年", y_data_2021)
chart.add_yaxis("2022年", y_data_2022)
chart.set_global_opts(
title_opts=opts.TitleOpts(title="手机销售额"),
legend_opts=opts.LegendOpts(pos_left="center"),
toolbox_opts=opts.ToolboxOpts(
feature={
"dataView": {},
"saveAsImage": {},
"restore": {},
"magicType": {"type": ["line", "bar"]},
}
),
)
elif chart_type == 'line':
chart = Line()
chart.add_xaxis(x_data)
chart.add_yaxis("2021年", y_data_2021, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
chart.add_yaxis("2022年", y_data_2022, areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
chart.set_global_opts(
title_opts=opts.TitleOpts(title="手机销售额"),
legend_opts=opts.LegendOpts(pos_left="center"),
toolbox_opts=opts.ToolboxOpts(
feature={
"dataView": {},
"saveAsImage": {},
"restore": {},
"magicType": {"type": ["line", "bar"]},
}
),
)
else:
chart = None
return chart
# 生成网格布局
tab = Tab()
tab.add(generate_chart(data, 'bar'), "柱状图")
tab.add(generate_chart(data, 'line'), "折线图")
grid = Grid()
grid.add(
tab,
grid_opts=opts.GridOpts(
pos_left="10%",
pos_right="10%",
pos_top="15%",
pos_bottom="15%",
),
)
# 生成 HTML 文件
grid.render("phone_sales.html")
```
其中,这里使用了 Grid 和 Tab 来实现切换不同类型图表的功能。生成的 HTML 文件中,可以在工具框中选择柱状图或折线图。
阅读全文