1
0

test_echart.py 3.9 KB

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