echart_documentation.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 from pyecharts', '''
  39. You can create an EChart element from a pyecharts object using the `from_pyecharts` method.
  40. For defining dynamic options like a formatter function, you can use the `JsCode` class from `pyecharts.commons.utils`.
  41. Alternatively, you can use a colon ":" to prefix the property name to indicate that the value is a JavaScript expression.
  42. ''')
  43. def echart_from_pyecharts_demo():
  44. from pyecharts.charts import Bar
  45. from pyecharts.commons.utils import JsCode
  46. from pyecharts.options import AxisOpts
  47. ui.echart.from_pyecharts(
  48. Bar()
  49. .add_xaxis(['A', 'B', 'C'])
  50. .add_yaxis('ratio', [1, 2, 4])
  51. .set_global_opts(
  52. xaxis_opts=AxisOpts(axislabel_opts={
  53. ':formatter': r'(val, idx) => `group ${val}`',
  54. }),
  55. yaxis_opts=AxisOpts(axislabel_opts={
  56. 'formatter': JsCode(r'(val, idx) => `${val}%`'),
  57. }),
  58. )
  59. )
  60. @doc.demo('Run methods', '''
  61. You can run methods of the EChart instance using the `run_chart_method` method.
  62. This demo shows how to show and hide the loading animation, how to get the current width of the chart,
  63. and how to add tooltips with a custom formatter.
  64. The colon ":" in front of the method name "setOption" indicates that the argument is a JavaScript expression
  65. that is evaluated on the client before it is passed to the method.
  66. Note that requesting data from the client is only supported for page functions, not for the shared auto-index page.
  67. ''')
  68. def methods_demo() -> None:
  69. # @ui.page('/')
  70. def page():
  71. echart = ui.echart({
  72. 'xAxis': {'type': 'category', 'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']},
  73. 'yAxis': {'type': 'value'},
  74. 'series': [{'type': 'line', 'data': [150, 230, 224, 218, 135]}],
  75. })
  76. ui.button('Show Loading', on_click=lambda: echart.run_chart_method('showLoading'))
  77. ui.button('Hide Loading', on_click=lambda: echart.run_chart_method('hideLoading'))
  78. async def get_width():
  79. width = await echart.run_chart_method('getWidth')
  80. ui.notify(f'Width: {width}')
  81. ui.button('Get Width', on_click=get_width)
  82. ui.button('Set Tooltip', on_click=lambda: echart.run_chart_method(
  83. ':setOption', r'{tooltip: {formatter: params => "$" + params.value}}',
  84. ))
  85. page() # HIDE
  86. @doc.demo('Arbitrary chart events', '''
  87. You can register arbitrary event listeners for the chart using the `on` method and a "chart:" prefix.
  88. This demo shows how to register a callback for the "selectchanged" event which is triggered when the user selects a point.
  89. ''')
  90. def events_demo() -> None:
  91. ui.echart({
  92. 'toolbox': {'feature': {'brush': {'type': ['rect']}}},
  93. 'brush': {},
  94. 'xAxis': {'type': 'category'},
  95. 'yAxis': {'type': 'value'},
  96. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  97. }).on('chart:selectchanged', lambda e: label.set_text(
  98. f'Selected point {e.args["fromActionPayload"]["dataIndexInside"]}'
  99. ))
  100. label = ui.label()
  101. @doc.demo('3D Graphing', '''
  102. Charts will automatically be 3D enabled if the initial options contain the string "3D".
  103. If not, set the `enable_3d` argument to `True`.
  104. ''')
  105. def echarts_gl_demo() -> None:
  106. ui.echart({
  107. 'xAxis3D': {},
  108. 'yAxis3D': {},
  109. 'zAxis3D': {},
  110. 'grid3D': {},
  111. 'series': [{
  112. 'type': 'line3D',
  113. 'data': [[1, 1, 1], [3, 3, 3]],
  114. }],
  115. })
  116. doc.reference(ui.echart)