test_echart.py 3.8 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. @ui.page('/')
  56. def page():
  57. echart = ui.echart({
  58. 'xAxis': {'type': 'value'},
  59. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  60. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  61. }).classes('w-[600px]')
  62. async def get_width():
  63. ui.label(f'Width: {await echart.run_chart_method("getWidth")}px')
  64. ui.button('Get Width', on_click=get_width)
  65. screen.open('/')
  66. screen.click('Get Width')
  67. screen.should_contain('Width: 600px')
  68. def test_create_from_pyecharts(screen: Screen):
  69. X_AXIS_FORMATTER = r'(val, idx) => `x for ${val}`'
  70. Y_AXIS_FORMATTER = r'(val, idx) => `${val} kg`'
  71. ui.echart.from_pyecharts(
  72. Bar()
  73. .add_xaxis(['A', 'B', 'C'])
  74. .add_yaxis('series A', [0.1, 0.2, 0.3],)
  75. .set_global_opts(
  76. xaxis_opts=options.AxisOpts(axislabel_opts={':formatter': X_AXIS_FORMATTER}),
  77. yaxis_opts=options.AxisOpts(axislabel_opts={'formatter': utils.JsCode(Y_AXIS_FORMATTER)}),
  78. )
  79. )
  80. screen.open('/')
  81. assert screen.selenium.execute_script('''
  82. const chart = echarts.getInstanceByDom(document.querySelector(".nicegui-echart"));
  83. const x = chart.getOption().xAxis[0].axisLabel.formatter;
  84. const y = chart.getOption().yAxis[0].axisLabel.formatter;
  85. return [typeof x, x.toString(), typeof y, y.toString()];
  86. ''') == ['function', X_AXIS_FORMATTER, 'function', Y_AXIS_FORMATTER]
  87. def test_chart_events(screen: Screen):
  88. ui.echart({
  89. 'xAxis': {'type': 'category'},
  90. 'yAxis': {'type': 'value'},
  91. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  92. }).on('chart:rendered', lambda: ui.label('Chart rendered.'))
  93. screen.open('/')
  94. screen.should_contain('Chart rendered.')