echart.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from typing import Callable, Dict, Optional
  2. from ..element import Element
  3. from ..events import EChartPointClickEventArguments, GenericEventArguments, handle_event
  4. class EChart(Element, component='echart.js', libraries=['lib/echarts/echarts.min.js']):
  5. def __init__(self, options: Dict, on_point_click: Optional[Callable] = None) -> None:
  6. """Apache EChart
  7. An element to create a chart using `ECharts <https://echarts.apache.org/>`_.
  8. Updates can be pushed to the chart by changing the `options` property.
  9. After data has changed, call the `update` method to refresh the chart.
  10. :param options: dictionary of EChart options
  11. :param on_click_point: callback function that is called when a point is clicked
  12. """
  13. super().__init__()
  14. self._props['options'] = options
  15. self._classes = ['nicegui-echart']
  16. if on_point_click:
  17. def handle_point_click(e: GenericEventArguments) -> None:
  18. handle_event(on_point_click, EChartPointClickEventArguments(
  19. sender=self,
  20. client=self.client,
  21. component_type=e.args['componentType'],
  22. series_type=e.args['seriesType'],
  23. series_index=e.args['seriesIndex'],
  24. series_name=e.args['seriesName'],
  25. name=e.args['name'],
  26. data_index=e.args['dataIndex'],
  27. data=e.args['data'],
  28. data_type=e.args.get('dataType'),
  29. value=e.args['value'],
  30. ))
  31. self.on('pointClick', handle_point_click, [
  32. 'componentType',
  33. 'seriesType',
  34. 'seriesIndex',
  35. 'seriesName',
  36. 'name',
  37. 'dataIndex',
  38. 'data',
  39. 'dataType',
  40. 'value',
  41. ])
  42. @property
  43. def options(self) -> Dict:
  44. return self._props['options']
  45. def update(self) -> None:
  46. super().update()
  47. self.run_method('update_chart')