test_echart.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from typing import Generator
  2. import pytest
  3. from pyecharts import options
  4. from pyecharts.charts import Bar
  5. from pyecharts.commons import utils
  6. from nicegui import app, ui
  7. from nicegui.testing import Screen
  8. @pytest.fixture
  9. def test_route() -> Generator[str, None, None]:
  10. TEST_ROUTE = '/theme.json'
  11. yield TEST_ROUTE
  12. app.remove_route(TEST_ROUTE)
  13. def test_create_dynamically(screen: Screen):
  14. def create():
  15. ui.echart({
  16. 'xAxis': {'type': 'value'},
  17. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  18. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  19. })
  20. ui.button('Create', on_click=create)
  21. screen.open('/')
  22. screen.click('Create')
  23. assert screen.find_by_tag('canvas')
  24. def test_update(screen: Screen):
  25. def update():
  26. chart.options['xAxis'] = {'type': 'value'}
  27. chart.options['yAxis'] = {'type': 'category', 'data': ['A', 'B', 'C']}
  28. chart.options['series'] = [{'type': 'line', 'data': [0.1, 0.2, 0.3]}]
  29. chart.update()
  30. chart = ui.echart({})
  31. ui.button('Update', on_click=update)
  32. screen.open('/')
  33. assert not screen.find_all_by_tag('canvas')
  34. screen.click('Update')
  35. assert screen.find_by_tag('canvas')
  36. def test_nested_card(screen: Screen):
  37. with ui.card().style('height: 200px; width: 600px'):
  38. ui.echart({
  39. 'xAxis': {'type': 'value'},
  40. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  41. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  42. })
  43. screen.open('/')
  44. canvas = screen.find_by_tag('canvas')
  45. assert canvas.rect['height'] == 168
  46. assert canvas.rect['width'] == 568
  47. def test_nested_expansion(screen: Screen):
  48. with ui.expansion() as expansion:
  49. with ui.card().style('height: 200px; width: 600px'):
  50. ui.echart({
  51. 'xAxis': {'type': 'value'},
  52. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  53. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  54. 'animationDuration': 100,
  55. })
  56. ui.button('Open', on_click=expansion.open)
  57. screen.open('/')
  58. screen.click('Open')
  59. screen.wait(0.5)
  60. canvas = screen.find_by_tag('canvas')
  61. assert canvas.rect['height'] == 168
  62. assert canvas.rect['width'] == 568
  63. def test_run_method(screen: Screen):
  64. @ui.page('/')
  65. def page():
  66. echart = ui.echart({
  67. 'xAxis': {'type': 'value'},
  68. 'yAxis': {'type': 'category', 'data': ['A', 'B', 'C']},
  69. 'series': [{'type': 'line', 'data': [0.1, 0.2, 0.3]}],
  70. }).style('width: 600px')
  71. async def get_width():
  72. ui.label(f'Width: {await echart.run_chart_method("getWidth")}px')
  73. ui.button('Get Width', on_click=get_width)
  74. screen.open('/')
  75. screen.click('Get Width')
  76. screen.should_contain('Width: 600px')
  77. def test_create_from_pyecharts(screen: Screen):
  78. X_AXIS_FORMATTER = r'(val, idx) => `x for ${val}`'
  79. Y_AXIS_FORMATTER = r'(val, idx) => `${val} kg`'
  80. ui.echart.from_pyecharts(
  81. Bar()
  82. .add_xaxis(['A', 'B', 'C'])
  83. .add_yaxis('series A', [0.1, 0.2, 0.3],)
  84. .set_global_opts(
  85. xaxis_opts=options.AxisOpts(axislabel_opts={':formatter': X_AXIS_FORMATTER}),
  86. yaxis_opts=options.AxisOpts(axislabel_opts={'formatter': utils.JsCode(Y_AXIS_FORMATTER)}),
  87. )
  88. )
  89. screen.open('/')
  90. assert screen.selenium.execute_script('''
  91. const chart = echarts.getInstanceByDom(document.querySelector(".nicegui-echart"));
  92. const x = chart.getOption().xAxis[0].axisLabel.formatter;
  93. const y = chart.getOption().yAxis[0].axisLabel.formatter;
  94. return [typeof x, x.toString(), typeof y, y.toString()];
  95. ''') == ['function', X_AXIS_FORMATTER, 'function', Y_AXIS_FORMATTER]
  96. def test_chart_events(screen: Screen):
  97. ui.echart({
  98. 'xAxis': {'type': 'category'},
  99. 'yAxis': {'type': 'value'},
  100. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  101. }).on('chart:rendered', lambda: ui.label('Chart rendered.'))
  102. screen.open('/')
  103. screen.should_contain('Chart rendered.')
  104. def test_theme_dictionary(screen: Screen):
  105. ui.echart({
  106. 'xAxis': {'type': 'category'},
  107. 'yAxis': {'type': 'value'},
  108. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  109. }, theme={'backgroundColor': 'rgba(254,248,239,1)'}, renderer='svg')
  110. screen.open('/')
  111. assert screen.find_by_tag('rect').value_of_css_property('fill') == 'rgb(254, 248, 239)'
  112. def test_theme_url(screen: Screen, test_route: str): # pylint: disable=redefined-outer-name
  113. @app.get(test_route)
  114. def theme():
  115. return {'backgroundColor': 'rgba(254,248,239,1)'}
  116. ui.echart({
  117. 'xAxis': {'type': 'category'},
  118. 'yAxis': {'type': 'value'},
  119. 'series': [{'type': 'line', 'data': [1, 2, 3]}],
  120. }, theme=test_route, renderer='svg')
  121. screen.open('/')
  122. assert screen.find_by_tag('rect').value_of_css_property('fill') == 'rgb(254, 248, 239)'