anacode.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. @author: luojunhui
  3. """
  4. from pyecharts import options as opts
  5. from pyecharts.charts import Line, Page
  6. from pyecharts.globals import ThemeType
  7. import pandas as pd
  8. # Load the Excel file
  9. file_path = '/Users/luojunhui/Downloads/0708-ana数据.xlsx'
  10. df = pd.read_excel(file_path, sheet_name='Sheet1')
  11. df['日期'] = pd.to_datetime(df['日期']).dt.strftime('%Y-%m-%d')
  12. # Correct way to concatenate '模式类型' and '账号来源' with '-'
  13. df['账号来源'] = df.apply(lambda row: "{}-{}".format(row['模式类型'].replace('\n', ''), row['账号来源']), axis=1)
  14. print(df['账号来源'])
  15. # Pivot the data to get '小程序第一层人数/阅读量' for each '账号来源' over '日期'
  16. pivot_df = df.pivot(index='日期', columns='账号来源', values='小程序第一层人数/阅读量')
  17. # Create a line chart
  18. line = Line(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, width="100%", height="600px"))
  19. line.add_xaxis(pivot_df.index.tolist())
  20. # Add data for each product
  21. for product in pivot_df.columns:
  22. line.add_yaxis(
  23. product,
  24. pivot_df[product].tolist(),
  25. is_smooth=True,
  26. linestyle_opts=opts.LineStyleOpts(width=3),
  27. markline_opts=opts.MarkLineOpts(
  28. data=[
  29. opts.MarkLineItem(x="2024-07-02", name="分割")
  30. ],
  31. label_opts=opts.LabelOpts(position="insideEndTop"),
  32. linestyle_opts=opts.LineStyleOpts(width=2, color="grey")
  33. )
  34. )
  35. # Set global options
  36. line.set_global_opts(
  37. title_opts=opts.TitleOpts(title="小程序打开率趋势", pos_left='center'),
  38. xaxis_opts=opts.AxisOpts(type_="category"),
  39. yaxis_opts=opts.AxisOpts(type_="value"),
  40. tooltip_opts=opts.TooltipOpts(trigger="axis"),
  41. # toolbox_opts=opts.ToolboxOpts(is_show=True, feature=opts.ToolBoxFeatureOpts(save_as_image={"is_show": True})),
  42. legend_opts=opts.LegendOpts(
  43. pos_bottom='0%',
  44. pos_left='center',
  45. orient='horizontal',
  46. textstyle_opts=opts.TextStyleOpts(font_size=12),
  47. selected_mode='single',
  48. )
  49. )
  50. # Render the chart to an HTML file
  51. line.render("product_trends.html")
  52. # Display the location of the rendered file
  53. print("Charts are rendered in 'product_trends.html'")