1. from pyecharts import options as opts
    2. from pyecharts.charts import Bar, Grid, Line, Liquid, Page, Pie
    3. from pyecharts.commons.utils import JsCode
    4. from pyecharts.components import Table
    5. from pyecharts.faker import Faker
    6. def bar_datazoom_slider() -> Bar:
    7. c = (
    8. Bar()
    9. .add_xaxis(Faker.days_attrs)
    10. .add_yaxis("商家A", Faker.days_values)
    11. .set_global_opts(
    12. title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
    13. datazoom_opts=[opts.DataZoomOpts()],
    14. )
    15. )
    16. return c
    17. def line_markpoint() -> Line:
    18. c = (
    19. Line()
    20. .add_xaxis(Faker.choose())
    21. .add_yaxis(
    22. "商家A",
    23. Faker.values(),
    24. markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="min")]),
    25. )
    26. .add_yaxis(
    27. "商家B",
    28. Faker.values(),
    29. markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
    30. )
    31. .set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkPoint"))
    32. )
    33. return c
    34. def pie_rosetype() -> Pie:
    35. v = Faker.choose()
    36. c = (
    37. Pie()
    38. .add(
    39. "",
    40. [list(z) for z in zip(v, Faker.values())],
    41. radius=["30%", "75%"],
    42. center=["25%", "50%"],
    43. rosetype="radius",
    44. label_opts=opts.LabelOpts(is_show=False),
    45. )
    46. .add(
    47. "",
    48. [list(z) for z in zip(v, Faker.values())],
    49. radius=["30%", "75%"],
    50. center=["75%", "50%"],
    51. rosetype="area",
    52. )
    53. .set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
    54. )
    55. return c
    56. def grid_mutil_yaxis() -> Grid:
    57. x_data = ["{}月".format(i) for i in range(1, 13)]
    58. bar = (
    59. Bar()
    60. .add_xaxis(x_data)
    61. .add_yaxis(
    62. "蒸发量",
    63. [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
    64. yaxis_index=0,
    65. color="#d14a61",
    66. )
    67. .add_yaxis(
    68. "降水量",
    69. [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
    70. yaxis_index=1,
    71. color="#5793f3",
    72. )
    73. .extend_axis(
    74. yaxis=opts.AxisOpts(
    75. name="蒸发量",
    76. type_="value",
    77. min_=0,
    78. max_=250,
    79. position="right",
    80. axisline_opts=opts.AxisLineOpts(
    81. linestyle_opts=opts.LineStyleOpts(color="#d14a61")
    82. ),
    83. axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
    84. )
    85. )
    86. .extend_axis(
    87. yaxis=opts.AxisOpts(
    88. type_="value",
    89. name="温度",
    90. min_=0,
    91. max_=25,
    92. position="left",
    93. axisline_opts=opts.AxisLineOpts(
    94. linestyle_opts=opts.LineStyleOpts(color="#675bba")
    95. ),
    96. axislabel_opts=opts.LabelOpts(formatter="{value} °C"),
    97. splitline_opts=opts.SplitLineOpts(
    98. is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
    99. ),
    100. )
    101. )
    102. .set_global_opts(
    103. yaxis_opts=opts.AxisOpts(
    104. name="降水量",
    105. min_=0,
    106. max_=250,
    107. position="right",
    108. offset=80,
    109. axisline_opts=opts.AxisLineOpts(
    110. linestyle_opts=opts.LineStyleOpts(color="#5793f3")
    111. ),
    112. axislabel_opts=opts.LabelOpts(formatter="{value} ml"),
    113. ),
    114. title_opts=opts.TitleOpts(title="Grid-多 Y 轴示例"),
    115. tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
    116. )
    117. )
    118. line = (
    119. Line()
    120. .add_xaxis(x_data)
    121. .add_yaxis(
    122. "平均温度",
    123. [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
    124. yaxis_index=2,
    125. color="#675bba",
    126. label_opts=opts.LabelOpts(is_show=False),
    127. )
    128. )
    129. bar.overlap(line)
    130. return Grid().add(
    131. bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True
    132. )
    133. def liquid_data_precision() -> Liquid:
    134. c = (
    135. Liquid()
    136. .add(
    137. "lq",
    138. [0.3254],
    139. label_opts=opts.LabelOpts(
    140. font_size=50,
    141. formatter=JsCode(
    142. """function (param) {
    143. return (Math.floor(param.value * 10000) / 100) + '%';
    144. }"""
    145. ),
    146. position="inside",
    147. ),
    148. )
    149. .set_global_opts(title_opts=opts.TitleOpts(title="Liquid-数据精度"))
    150. )
    151. return c
    152. def table_base() -> Table:
    153. table = Table()
    154. headers = ["City name", "Area", "Population", "Annual Rainfall"]
    155. rows = [
    156. ["Brisbane", 5905, 1857594, 1146.4],
    157. ["Adelaide", 1295, 1158259, 600.5],
    158. ["Darwin", 112, 120900, 1714.7],
    159. ["Hobart", 1357, 205556, 619.5],
    160. ["Sydney", 2058, 4336374, 1214.8],
    161. ["Melbourne", 1566, 3806092, 646.9],
    162. ["Perth", 5386, 1554769, 869.4],
    163. ]
    164. table.add(headers, rows).set_global_opts(
    165. title_opts=opts.ComponentTitleOpts(title="Table")
    166. )
    167. return table
    168. def page_draggable_layout():
    169. page = Page(layout=Page.DraggablePageLayout)
    170. page.add(
    171. bar_datazoom_slider(),
    172. line_markpoint(),
    173. pie_rosetype(),
    174. grid_mutil_yaxis(),
    175. liquid_data_precision(),
    176. table_base(),
    177. )
    178. page.render("page_draggable_layout.html")
    179. if __name__ == "__main__":
    180. page_draggable_layout()

    生成的page_draggable_layout.html页面上各图是自上而下排列,可用鼠标拖动重置位置和大小。然后点第一幅图左上角的”Save Config”将配置文件保存为chart_config.json 文件,默认保存在当前用户的下载文件夹中。将其复制到page_draggable_layout.html文件相同文件夹下,然后注释掉渲染函数的调用(网页已经生成,不需要重新渲染,只需要改变位置及大小即可),加上以下语句:

    if __name__ == "__main__":
        # page_draggable_layout()
        Page.save_resize_html("page_draggable_layout.html", cfg_file="./chart_config.json", dest="my_new_charts.html")
    

    image.png