echart_documentation.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.echart)
  4. def main_demo() -> None:
  5. from random import random
  6. echart = ui.echart({
  7. 'xAxis': {'type': 'value'},
  8. 'yAxis': {'type': 'category', 'data': ['A', 'B'], 'inverse': True},
  9. 'legend': {'textStyle': {'color': 'gray'}},
  10. 'series': [
  11. {'type': 'bar', 'name': 'Alpha', 'data': [0.1, 0.2]},
  12. {'type': 'bar', 'name': 'Beta', 'data': [0.3, 0.4]},
  13. ],
  14. })
  15. def update():
  16. echart.options['series'][0]['data'][0] = random()
  17. echart.update()
  18. ui.button('Update', on_click=update)
  19. @doc.demo('EChart with clickable points', '''
  20. You can register a callback for an event when a series point is clicked.
  21. ''')
  22. def clickable_points() -> None:
  23. ui.echart({
  24. 'xAxis': {'type': 'category'},
  25. 'yAxis': {'type': 'value'},
  26. 'series': [{'type': 'line', 'data': [20, 10, 30, 50, 40, 30]}],
  27. }, on_point_click=ui.notify)
  28. @doc.demo('EChart with dynamic properties', '''
  29. Dynamic properties can be passed to chart elements to customize them such as apply an axis label format.
  30. To make a property dynamic, prefix a colon ":" to the property name.
  31. ''')
  32. def dynamic_properties() -> None:
  33. ui.echart({
  34. 'xAxis': {'type': 'category'},
  35. 'yAxis': {'axisLabel': {':formatter': 'value => "$" + value'}},
  36. 'series': [{'type': 'line', 'data': [5, 8, 13, 21, 34, 55]}],
  37. })
  38. @doc.demo('EChart with custom theme', '''
  39. You can apply custom themes created with the [Theme Builder](https://echarts.apache.org/en/theme-builder.html).
  40. Instead of passing the theme as a dictionary, you can pass a URL to a JSON file.
  41. This allows the browser to cache the theme and load it faster when the same theme is used multiple times.
  42. *Added in version 2.15.0*
  43. ''')
  44. def custom_theme() -> None:
  45. ui.echart({
  46. 'xAxis': {'type': 'category'},
  47. 'yAxis': {'type': 'value'},
  48. 'series': [{'type': 'bar', 'data': [20, 10, 30, 50, 40, 30]}],
  49. }, theme={
  50. 'color': ['#b687ac', '#28738a', '#a78f8f'],
  51. 'backgroundColor': 'rgba(254,248,239,1)',
  52. })
  53. @doc.demo('EChart from pyecharts', '''
  54. You can create an EChart element from a pyecharts object using the `from_pyecharts` method.
  55. For defining dynamic options like a formatter function, you can use the `JsCode` class from `pyecharts.commons.utils`.
  56. Alternatively, you can use a colon ":" to prefix the property name to indicate that the value is a JavaScript expression.
  57. ''')
  58. def echart_from_pyecharts_demo():
  59. from pyecharts.charts import Bar
  60. from pyecharts.commons.utils import JsCode
  61. from pyecharts.options import AxisOpts
  62. ui.echart.from_pyecharts(
  63. Bar()
  64. .add_xaxis(['A', 'B', 'C'])
  65. .add_yaxis('ratio', [1, 2, 4])
  66. .set_global_opts(
  67. xaxis_opts=AxisOpts(axislabel_opts={
  68. ':formatter': r'(val, idx) => `group ${val}`',
  69. }),
  70. yaxis_opts=AxisOpts(axislabel_opts={
  71. 'formatter': JsCode(r'(val, idx) => `${val}%`'),
  72. }),
  73. )
  74. )
  75. @doc.demo('Run methods', '''
  76. You can run methods of the EChart instance using the `run_chart_method` method.
  77. This demo shows how to show and hide the loading animation, how to get the current width of the chart,
  78. and how to add tooltips with a custom formatter.
  79. The colon ":" in front of the method name "setOption" indicates that the argument is a JavaScript expression
  80. that is evaluated on the client before it is passed to the method.
  81. Note that requesting data from the client is only supported for page functions, not for the shared auto-index page.
  82. ''')
  83. def methods_demo() -> None:
  84. # @ui.page('/')
  85. def page():
  86. echart = ui.echart({
  87. 'xAxis': {'type': 'category', 'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']},
  88. 'yAxis': {'type': 'value'},
  89. 'series': [{'type': 'line', 'data': [150, 230, 224, 218, 135]}],
  90. })
  91. ui.button('Show Loading', on_click=lambda: echart.run_chart_method('showLoading'))
  92. ui.button('Hide Loading', on_click=lambda: echart.run_chart_method('hideLoading'))
  93. async def get_width():
  94. width = await echart.run_chart_method('getWidth')
  95. ui.notify(f'Width: {width}')
  96. ui.button('Get Width', on_click=get_width)
  97. ui.button('Set Tooltip', on_click=lambda: echart.run_chart_method(
  98. ':setOption', r'{tooltip: {formatter: params => "$" + params.value}}',
  99. ))
  100. page() # HIDE
  101. @doc.demo('Arbitrary chart events', '''
  102. You can register arbitrary event listeners for the chart using the `on` method and a "chart:" prefix.
  103. This demo shows how to register a callback for the "selectchanged" event which is triggered when the user selects a point.
  104. ''')
  105. def events_demo() -> None:
  106. ui.echart({
  107. 'toolbox': {'feature': {'brush': {'type': ['rect']}}},
  108. 'brush': {},
  109. 'xAxis': {'type': 'category'},
  110. 'yAxis': {'type': 'value'},
  111. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  112. }).on('chart:selectchanged', lambda e: label.set_text(
  113. f'Selected point {e.args["fromActionPayload"]["dataIndexInside"]}'
  114. ))
  115. label = ui.label()
  116. @doc.demo('3D Graphing', '''
  117. Charts will automatically be 3D enabled if the initial options contain the string "3D".
  118. If not, set the `enable_3d` argument to `True`.
  119. ''')
  120. def echarts_gl_demo() -> None:
  121. ui.echart({
  122. 'xAxis3D': {},
  123. 'yAxis3D': {},
  124. 'zAxis3D': {},
  125. 'grid3D': {},
  126. 'series': [{
  127. 'type': 'line3D',
  128. 'data': [[1, 1, 1], [3, 3, 3]],
  129. }],
  130. })
  131. doc.reference(ui.echart)