test_echart.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from pyecharts import options
  2. from pyecharts.charts import Bar
  3. from pyecharts.commons import utils
  4. from nicegui import ui
  5. from nicegui.testing import Screen
  6. def test_create_dynamically(screen: Screen):
  7. def create():
  8. ui.echart({
  9. 'xAxis': {'type': 'value'},
  10. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  11. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  12. })
  13. ui.button('Create', on_click=create)
  14. screen.open('/')
  15. screen.click('Create')
  16. assert screen.find_by_tag('canvas')
  17. def test_update(screen: Screen):
  18. def update():
  19. chart.options['xAxis'] = {'type': 'value'}
  20. chart.options['yAxis'] = {'type': 'category', 'data': ['A', 'B', 'C']}
  21. chart.options['series'] = [{'type': 'line', 'data': [0.1, 0.2, 0.3]}]
  22. chart.update()
  23. chart = ui.echart({})
  24. ui.button('Update', on_click=update)
  25. screen.open('/')
  26. assert not screen.find_all_by_tag('canvas')
  27. screen.click('Update')
  28. assert screen.find_by_tag('canvas')
  29. def test_nested_card(screen: Screen):
  30. with ui.card().style('height: 200px; width: 600px'):
  31. ui.echart({
  32. 'xAxis': {'type': 'value'},
  33. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  34. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  35. })
  36. screen.open('/')
  37. canvas = screen.find_by_tag('canvas')
  38. assert canvas.rect['height'] == 168
  39. assert canvas.rect['width'] == 568
  40. def test_nested_expansion(screen: Screen):
  41. with ui.expansion() as expansion:
  42. with ui.card().style('height: 200px; width: 600px'):
  43. ui.echart({
  44. 'xAxis': {'type': 'value'},
  45. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  46. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  47. })
  48. ui.button('Open', on_click=expansion.open)
  49. screen.open('/')
  50. screen.click('Open')
  51. canvas = screen.find_by_tag('canvas')
  52. assert canvas.rect['height'] == 168
  53. assert canvas.rect['width'] == 568
  54. def test_run_method(screen: Screen):
  55. echart = ui.echart({
  56. 'xAxis': {'type': 'value'},
  57. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  58. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  59. }).classes('w-[600px]')
  60. async def get_width():
  61. ui.label(f'Width: {await echart.run_chart_method("getWidth")}px')
  62. ui.button('Get Width', on_click=get_width)
  63. screen.open('/')
  64. screen.click('Get Width')
  65. screen.should_contain('Width: 600px')
  66. def test_create_from_pyecharts(screen: Screen):
  67. xaxis_formatter = r'(val, idx) => `x for ${val}`'
  68. yaxis_formatter = r'(val, idx) => `${val} kg`'
  69. get_options_js = "echarts.getInstanceByDom(document.querySelector('.nicegui-echart')).getOption()"
  70. ui.echart.from_pyecharts(
  71. Bar()
  72. .add_xaxis(['A', 'B', 'C'])
  73. .add_yaxis('series A', [0.1,0.2,0.3],)
  74. .set_global_opts(
  75. xaxis_opts=options.AxisOpts(axislabel_opts={':formatter': xaxis_formatter}),
  76. yaxis_opts=options.AxisOpts(axislabel_opts={'formatter': utils.JsCode(yaxis_formatter)}),
  77. )
  78. )
  79. label = ui.label('')
  80. async def get_xAxis_formatter():
  81. type_str,formatter= await ui.run_javascript(f"const fm={get_options_js}.xAxis[0].axisLabel.formatter;[typeof fm,fm.toString()]")
  82. label.set_text(f"xAxis formatter is {type_str}: {formatter}")
  83. ui.button('Get xAxis formatter', on_click=get_xAxis_formatter)
  84. async def get_yAxis_formatter():
  85. type_str,formatter= await ui.run_javascript(f"const fm={get_options_js}.yAxis[0].axisLabel.formatter;[typeof fm,fm.toString()]")
  86. label.set_text(f"yAxis formatter is {type_str}: {formatter}")
  87. ui.button('Get yAxis formatter', on_click=get_yAxis_formatter)
  88. screen.open('/')
  89. screen.click('Get xAxis formatter')
  90. screen.should_contain(f"formatter is function: {xaxis_formatter}")
  91. screen.click('Get yAxis formatter')
  92. screen.should_contain(f"formatter is function: {yaxis_formatter}")