plotly_documentation.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class PlotlyDocumentation(UiElementDocumentation, element=ui.plotly):
  4. def main_demo(self) -> None:
  5. import plotly.graph_objects as go
  6. fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
  7. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  8. ui.plotly(fig).classes('w-full h-40')
  9. def more(self) -> None:
  10. @self.demo('Dictionary interface', '''
  11. This demo shows how to use the declarative dictionary interface to create a plot.
  12. For plots with many traces and data points, this is more efficient than the object-oriented interface.
  13. The definition corresponds to the [JavaScript Plotly API](https://plotly.com/javascript/).
  14. Due to different defaults, the resulting plot may look slightly different from the same plot created with the object-oriented interface,
  15. but the functionality is the same.
  16. ''')
  17. def plot_dict_interface():
  18. fig = {
  19. 'data': [
  20. {
  21. 'type': 'scatter',
  22. 'name': 'Trace 1',
  23. 'x': [1, 2, 3, 4],
  24. 'y': [1, 2, 3, 2.5],
  25. },
  26. {
  27. 'type': 'scatter',
  28. 'name': 'Trace 2',
  29. 'x': [1, 2, 3, 4],
  30. 'y': [1.4, 1.8, 3.8, 3.2],
  31. 'line': {'dash': 'dot', 'width': 3},
  32. },
  33. ],
  34. 'layout': {
  35. 'margin': {'l': 15, 'r': 0, 't': 0, 'b': 15},
  36. 'plot_bgcolor': '#E5ECF6',
  37. 'xaxis': {'gridcolor': 'white'},
  38. 'yaxis': {'gridcolor': 'white'},
  39. },
  40. }
  41. ui.plotly(fig).classes('w-full h-40')
  42. @self.demo('Plot updates', '''
  43. This demo shows how to update the plot in real time.
  44. Click the button to add a new trace to the plot.
  45. To send the new plot to the browser, make sure to explicitly call `plot.update()` or `ui.update(plot)`.
  46. ''')
  47. def plot_updates():
  48. from random import random
  49. import plotly.graph_objects as go
  50. fig = go.Figure()
  51. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  52. plot = ui.plotly(fig).classes('w-full h-40')
  53. def add_trace():
  54. fig.add_trace(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]))
  55. plot.update()
  56. ui.button('Add trace', on_click=add_trace)