echart_documentation.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. from random import random
  5. echart = ui.echart({
  6. 'xAxis': {'type': 'value'},
  7. 'yAxis': {'type': 'category', 'data': ['A', 'B'], 'inverse': True},
  8. 'legend': {'textStyle': {'color': 'gray'}},
  9. 'series': [
  10. {'type': 'bar', 'name': 'Alpha', 'data': [0.1, 0.2]},
  11. {'type': 'bar', 'name': 'Beta', 'data': [0.3, 0.4]},
  12. ],
  13. })
  14. def update():
  15. echart.options['series'][0]['data'][0] = random()
  16. echart.update()
  17. ui.button('Update', on_click=update)
  18. def more() -> None:
  19. @text_demo('Chart with clickable points', '''
  20. You can register a callback for an event when a series point is clicked.
  21. ''')
  22. def click() -> None:
  23. echart = ui.echart({
  24. 'xAxis': {'type': 'category'},
  25. 'yAxis': {'type': 'value', 'data': 'A', 'inverse': True},
  26. 'legend': {'textStyle': {'color': 'gray'}},
  27. 'series': [
  28. {'type': 'line', 'name': 'Alpha', 'data': [20, 10, 30, 50, 40, 30]},
  29. ],
  30. },
  31. on_point_click=lambda e: ui.notify(f'Click {e}')
  32. ).classes('w-full h-64')
  33. @text_demo('Chart with dynamic properties', '''
  34. Dynamic properties can be passed to chart elements to customize them such as apply an axis label format.
  35. To make a property dynamic prefix `:` to the property name.
  36. ''')
  37. def dynamic() -> None:
  38. echart = ui.echart({
  39. 'xAxis': {'type': 'category'},
  40. 'yAxis': {'type': 'value', 'data': 'A', 'axisLabel' : {':formatter': f'value => "$" + value.toFixed(2)'}},
  41. 'legend': {'textStyle': {'color': 'gray'}},
  42. 'series': [
  43. {'type': 'line', 'name': 'Alpha', 'data': [5, 8, 13, 21, 34, 55]},
  44. ],
  45. },
  46. ).classes('w-full h-64')