1
0

plotly_documentation.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.plotly)
  4. def main_demo() -> 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. @doc.demo('Dictionary interface', '''
  10. This demo shows how to use the declarative dictionary interface to create a plot.
  11. For plots with many traces and data points, this is more efficient than the object-oriented interface.
  12. The definition corresponds to the [JavaScript Plotly API](https://plotly.com/javascript/).
  13. Due to different defaults, the resulting plot may look slightly different from the same plot created with the object-oriented interface,
  14. but the functionality is the same.
  15. ''')
  16. def plot_dict_interface():
  17. fig = {
  18. 'data': [
  19. {
  20. 'type': 'scatter',
  21. 'name': 'Trace 1',
  22. 'x': [1, 2, 3, 4],
  23. 'y': [1, 2, 3, 2.5],
  24. },
  25. {
  26. 'type': 'scatter',
  27. 'name': 'Trace 2',
  28. 'x': [1, 2, 3, 4],
  29. 'y': [1.4, 1.8, 3.8, 3.2],
  30. 'line': {'dash': 'dot', 'width': 3},
  31. },
  32. ],
  33. 'layout': {
  34. 'margin': {'l': 15, 'r': 0, 't': 0, 'b': 15},
  35. 'plot_bgcolor': '#E5ECF6',
  36. 'xaxis': {'gridcolor': 'white'},
  37. 'yaxis': {'gridcolor': 'white'},
  38. },
  39. }
  40. ui.plotly(fig).classes('w-full h-40')
  41. @doc.demo('Plot updates', '''
  42. This demo shows how to update the plot in real time.
  43. Click the button to add a new trace to the plot.
  44. To send the new plot to the browser, make sure to explicitly call `plot.update()` or `ui.update(plot)`.
  45. ''')
  46. def plot_updates():
  47. from random import random
  48. import plotly.graph_objects as go
  49. fig = go.Figure()
  50. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  51. plot = ui.plotly(fig).classes('w-full h-40')
  52. def add_trace():
  53. fig.add_trace(go.Scatter(x=[1, 2, 3], y=[random(), random(), random()]))
  54. plot.update()
  55. ui.button('Add trace', on_click=add_trace)
  56. @doc.demo('Plot events', r'''
  57. This demo shows how to handle Plotly events.
  58. Try clicking on a data point to see the event data.
  59. Currently, the following events are supported:
  60. "plotly\_click",
  61. "plotly\_legendclick",
  62. "plotly\_selecting",
  63. "plotly\_selected",
  64. "plotly\_hover",
  65. "plotly\_unhover",
  66. "plotly\_legenddoubleclick",
  67. "plotly\_restyle",
  68. "plotly\_relayout",
  69. "plotly\_webglcontextlost",
  70. "plotly\_afterplot",
  71. "plotly\_autosize",
  72. "plotly\_deselect",
  73. "plotly\_doubleclick",
  74. "plotly\_redraw",
  75. "plotly\_animated".
  76. For more information, see the [Plotly documentation](https://plotly.com/javascript/plotlyjs-events/).
  77. ''')
  78. def plot_events():
  79. import plotly.graph_objects as go
  80. fig = go.Figure(go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 2.5]))
  81. fig.update_layout(margin=dict(l=0, r=0, t=0, b=0))
  82. plot = ui.plotly(fig).classes('w-full h-40')
  83. plot.on('plotly_click', ui.notify)
  84. doc.reference(ui.plotly)