1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- """
- @author: luojunhui
- """
- from pyecharts import options as opts
- from pyecharts.charts import Line, Page
- from pyecharts.globals import ThemeType
- import pandas as pd
- # Load the Excel file
- file_path = '/Users/luojunhui/Downloads/0708-ana数据.xlsx'
- df = pd.read_excel(file_path, sheet_name='Sheet1')
- df['日期'] = pd.to_datetime(df['日期']).dt.strftime('%Y-%m-%d')
- # Correct way to concatenate '模式类型' and '账号来源' with '-'
- df['账号来源'] = df.apply(lambda row: "{}-{}".format(row['模式类型'].replace('\n', ''), row['账号来源']), axis=1)
- print(df['账号来源'])
- # Pivot the data to get '小程序第一层人数/阅读量' for each '账号来源' over '日期'
- pivot_df = df.pivot(index='日期', columns='账号来源', values='小程序第一层人数/阅读量')
- # Create a line chart
- line = Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width="100%", height="600px"))
- line.add_xaxis(pivot_df.index.tolist())
- # Add data for each product
- for product in pivot_df.columns:
- line.add_yaxis(
- product,
- pivot_df[product].tolist(),
- is_smooth=True,
- linestyle_opts=opts.LineStyleOpts(width=3),
- markline_opts=opts.MarkLineOpts(
- data=[
- opts.MarkLineItem(x="2024-07-02", name="分割")
- ],
- label_opts=opts.LabelOpts(position="insideEndTop"),
- linestyle_opts=opts.LineStyleOpts(width=2, color="grey")
- )
- )
- # Set global options
- line.set_global_opts(
- title_opts=opts.TitleOpts(title="小程序打开率趋势", pos_left='center'),
- xaxis_opts=opts.AxisOpts(type_="category"),
- yaxis_opts=opts.AxisOpts(type_="value"),
- tooltip_opts=opts.TooltipOpts(trigger="axis"),
- # toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts(save_as_image={"is_show": True})),
- legend_opts=opts.LegendOpts(
- pos_bottom='0%',
- pos_left='center',
- orient='horizontal',
- textstyle_opts=opts.TextStyleOpts(font_size=12),
- selected_mode='single',
- )
- )
- # Render the chart to an HTML file
- line.render("product_trends.html")
- # Display the location of the rendered file
- print("Charts are rendered in 'product_trends.html'")
|