echart_documentation.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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('Run methods', '''
  39. You can run methods of the EChart instance using the `run_chart_method` method.
  40. This demo shows how to show and hide the loading animation, how to get the current width of the chart,
  41. and how to add tooltips with a custom formatter.
  42. The colon ":" in front of the method name "setOption" indicates that the argument is a JavaScript expression
  43. that is evaluated on the client before it is passed to the method.
  44. ''')
  45. def methods_demo() -> None:
  46. echart = ui.echart({
  47. 'xAxis': {'type': 'category', 'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']},
  48. 'yAxis': {'type': 'value'},
  49. 'series': [{'type': 'line', 'data': [150, 230, 224, 218, 135]}],
  50. })
  51. ui.button('Show Loading', on_click=lambda: echart.run_chart_method('showLoading'))
  52. ui.button('Hide Loading', on_click=lambda: echart.run_chart_method('hideLoading'))
  53. async def get_width():
  54. width = await echart.run_chart_method('getWidth')
  55. ui.notify(f'Width: {width}')
  56. ui.button('Get Width', on_click=get_width)
  57. ui.button('Set Tooltip', on_click=lambda: echart.run_chart_method(
  58. ':setOption', r'{tooltip: {formatter: params => "$" + params.value}}',
  59. ))
  60. doc.reference(ui.echart)