echart.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from typing import Callable, Dict, Optional
  2. from ..awaitable_response import AwaitableResponse
  3. from ..element import Element
  4. from ..events import EChartPointClickEventArguments, GenericEventArguments, handle_event
  5. class EChart(Element, component='echart.js', libraries=['lib/echarts/echarts.min.js']):
  6. def __init__(self, options: Dict, on_point_click: Optional[Callable] = None) -> None:
  7. """Apache EChart
  8. An element to create a chart using `ECharts <https://echarts.apache.org/>`_.
  9. Updates can be pushed to the chart by changing the `options` property.
  10. After data has changed, call the `update` method to refresh the chart.
  11. :param options: dictionary of EChart options
  12. :param on_click_point: callback function that is called when a point is clicked
  13. """
  14. super().__init__()
  15. self._props['options'] = options
  16. self._classes.append('nicegui-echart')
  17. if on_point_click:
  18. def handle_point_click(e: GenericEventArguments) -> None:
  19. handle_event(on_point_click, EChartPointClickEventArguments(
  20. sender=self,
  21. client=self.client,
  22. component_type=e.args['componentType'],
  23. series_type=e.args['seriesType'],
  24. series_index=e.args['seriesIndex'],
  25. series_name=e.args['seriesName'],
  26. name=e.args['name'],
  27. data_index=e.args['dataIndex'],
  28. data=e.args['data'],
  29. data_type=e.args.get('dataType'),
  30. value=e.args['value'],
  31. ))
  32. self.on('pointClick', handle_point_click, [
  33. 'componentType',
  34. 'seriesType',
  35. 'seriesIndex',
  36. 'seriesName',
  37. 'name',
  38. 'dataIndex',
  39. 'data',
  40. 'dataType',
  41. 'value',
  42. ])
  43. @property
  44. def options(self) -> Dict:
  45. """The options dictionary."""
  46. return self._props['options']
  47. def update(self) -> None:
  48. super().update()
  49. self.run_method('update_chart')
  50. def run_chart_method(self, name: str, *args, timeout: float = 1,
  51. check_interval: float = 0.01) -> AwaitableResponse:
  52. """Run a method of the JSONEditor instance.
  53. See the `ECharts documentation <https://echarts.apache.org/en/api.html#echartsInstance>`_ for a list of methods.
  54. If the function is awaited, the result of the method call is returned.
  55. Otherwise, the method is executed without waiting for a response.
  56. :param name: name of the method (a prefix ":" indicates that the arguments are JavaScript expressions)
  57. :param args: arguments to pass to the method (Python objects or JavaScript expressions)
  58. :param timeout: timeout in seconds (default: 1 second)
  59. :param check_interval: interval in seconds to check for a response (default: 0.01 seconds)
  60. :return: AwaitableResponse that can be awaited to get the result of the method call
  61. """
  62. return self.run_method('run_chart_method', name, *args, timeout=timeout, check_interval=check_interval)