You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
6.5 KiB
145 lines
6.5 KiB
from pyecharts import options as opts
|
|
from pyecharts.charts import Bar,Page,Line,Timeline
|
|
from pyecharts.commons.utils import JsCode
|
|
from pyecharts.globals import ThemeType
|
|
from pyecharts.charts import Pie
|
|
import sql
|
|
from pyecharts.faker import Faker
|
|
# from main import book_list_data
|
|
from collections import Counter
|
|
class Book:
|
|
def __init__(self, name, url, star, star_people, author, translater, publisher, pub_year, price, comment):
|
|
self.name = name
|
|
self.url = url
|
|
self.star = star
|
|
self.star_people = star_people
|
|
self.author = author
|
|
self.translater = translater
|
|
self.publisher = publisher
|
|
self.pub_year = pub_year
|
|
self.price = price
|
|
self.comment = comment
|
|
db = sql.BookDatabase(host='localhost', user='root', password='123456', database='xiaosuo', table_name='books')
|
|
db.initialize_table()
|
|
def show():
|
|
data = db.get_book_list()
|
|
book_list_data = []
|
|
for book in data:
|
|
book_list_data.append(
|
|
Book(book["name"], book["url"], book["star"], book["star_people"], book["author"], book["translater"],
|
|
book["publisher"], book["pub_year"], book["price"], book["comment"]))
|
|
|
|
book_list_data_sortBystart = sorted(book_list_data, key=lambda x: (x.star, x.star_people), reverse=True)
|
|
book_list_data_sortBystart = book_list_data_sortBystart[:10]
|
|
book_list_data_sortBystart = book_list_data_sortBystart[::-1]
|
|
# 从book_list_data中提取书籍名称和评分数据
|
|
book_names = [book.name for book in book_list_data_sortBystart]
|
|
book_stars = [book.star for book in book_list_data_sortBystart]
|
|
x_data = list(range(1, len(book_names) + 1))
|
|
# 创建柱状图
|
|
bar = (
|
|
Bar(init_opts=opts.InitOpts(theme="shine", width="850px", height='400px'))
|
|
.add_xaxis(book_names)
|
|
.add_yaxis("评分", book_stars, color="red")
|
|
.reversal_axis() # 实现旋转
|
|
.set_global_opts(
|
|
title_opts=opts.TitleOpts(title="评分前10榜", pos_bottom="bottom", pos_left="center"),
|
|
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=0)),
|
|
yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45))
|
|
)
|
|
# .render("bar_datazoom_slider.html")
|
|
)
|
|
|
|
publisher_list = [book.publisher for book in book_list_data]
|
|
# 使用Counter来计算每个出版商的数量
|
|
publisher_counter = Counter(publisher_list)
|
|
publisher_names = list(publisher_counter.keys())
|
|
publisher_counts = list(publisher_counter.values())
|
|
colors = [
|
|
"#5470c6", "#91cc75", "#fac858", "#ee6666", "#73c0de",
|
|
"#3ba272", "#fc8452", "#9a60b4", "#ea7ccc", "#bb60b4",
|
|
"#8B008B", "#FF1493", "#1E90FF", "#20B2AA", "#2E8B57",
|
|
"#B22222", "#FF4500", "#4682B4", "#DAA520", "#32CD32"
|
|
]
|
|
|
|
# 使用Pyecharts创建饼图
|
|
pie = (
|
|
Pie()
|
|
.add("", [list(z) for z in zip(publisher_names, publisher_counts)])
|
|
.set_colors(colors) # 设置颜色
|
|
.set_global_opts(title_opts=opts.TitleOpts(title="出版商分布饼图", pos_left="center", pos_bottom="bottom"))
|
|
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
|
|
)
|
|
|
|
prices = [float(book.price) for book in book_list_data]
|
|
print(prices)
|
|
# 将价格分组到不同的区间
|
|
price_intervals = [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, float('inf')] # 设置价格区间
|
|
price_counts = [0] * (len(price_intervals) - 1) # 初始化每个区间的计数为0
|
|
prices_intervalsStr = ["0-50", "50-100", "100-150", "150-200", "200-250", "250-300", "300-350", "350-400",
|
|
"400-450", "450-500", "500以上"]
|
|
# 统计每个价格区间的数量
|
|
for price in prices:
|
|
for i in range(len(price_intervals) - 1):
|
|
if price >= 500:
|
|
price_counts[len(price_intervals) - 1] += 1
|
|
break
|
|
if price_intervals[i] <= price and price < price_intervals[i + 1]:
|
|
price_counts[i] += 1
|
|
break
|
|
# print(price_counts)
|
|
# 生成价格区间与书籍数量折线图
|
|
line = (
|
|
Line()
|
|
.add_xaxis([str(interval) for interval in prices_intervalsStr[:-1]]) # X轴标签为价格区间
|
|
.add_yaxis("书籍数量", price_counts, symbol="circle", is_smooth=True,
|
|
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))
|
|
.set_global_opts(
|
|
title_opts=opts.TitleOpts(title="价格区间与书籍数量折线图", pos_left="center", pos_bottom="bottom"),
|
|
xaxis_opts=opts.AxisOpts(name="价格区间"),
|
|
yaxis_opts=opts.AxisOpts(name="书籍数量"),
|
|
datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100), )
|
|
)
|
|
|
|
# 生成时间与书籍数量折线图# 假设您的书籍数据保存在book_list_data中
|
|
# 提取每本书的出版年份
|
|
publish_dates = [int(book.pub_year.replace('-', '.').split('.')[0]) for book in book_list_data]
|
|
print(publish_dates)
|
|
# 使用Counter来计算每年出版的书的数量
|
|
publish_year_counter = Counter(publish_dates)
|
|
print(publish_year_counter)
|
|
# 确保结果包含连续的年份范围,并且将缺失的年份对应的数量设为 0
|
|
full_year_range = range(min(publish_dates), max(publish_dates) + 1)
|
|
print(full_year_range)
|
|
pub_year_counts = [(year, publish_year_counter[year]) for year in full_year_range]
|
|
|
|
# 提取年份和对应的书籍数量
|
|
years = [str(year) for year, count in pub_year_counts]
|
|
counts = [count for year, count in pub_year_counts]
|
|
print(pub_year_counts)
|
|
|
|
line_year_count = (
|
|
Line()
|
|
.add_xaxis(years)
|
|
.add_yaxis(
|
|
series_name="出版书籍数量",
|
|
y_axis=counts,
|
|
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),
|
|
opts.MarkPointItem(type_="min", name="最小值")]),
|
|
markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
|
|
)
|
|
.set_global_opts(title_opts=opts.TitleOpts(title="年份与出版书籍数量变化"),
|
|
xaxis_opts=opts.AxisOpts(name="年份"),
|
|
yaxis_opts=opts.AxisOpts(name="书籍数量"),
|
|
)
|
|
)
|
|
|
|
# 创建一个页面
|
|
page = Page()
|
|
# 将柱状图和饼图添加到页面中
|
|
page.add(line_year_count)
|
|
page.add(bar)
|
|
page.add(pie)
|
|
page.add(line)
|
|
# 生成HTML文件
|
|
page.render("book_analysis.html") |