advanced_animation.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. from math import ceil, cos
  17. from taipy.gui import Gui
  18. # Available waveforms to choose from
  19. waveforms = ["Sine", "Square"]
  20. # The initially selected waveform
  21. waveform = waveforms[0]
  22. # Values for the x axis
  23. x_range = range(100)
  24. # Data for the 'Sine' waveform
  25. cos_data = [cos(i / 6) for i in x_range]
  26. # Data for the 'Square' waveform
  27. square_data = [1 if ceil(i / 24) % 2 == 0 else -1 for i in x_range]
  28. # Dataset used by the chart
  29. data = {
  30. "x": x_range,
  31. "y": cos_data,
  32. }
  33. animation_data = None
  34. # Triggered when the selected waveform changes
  35. def change_data(state):
  36. # Animate by setting the 'y' values to the selected waveform's
  37. state.animation_data = {"y": cos_data if state.waveform == waveforms[0] else square_data}
  38. page = """
  39. <|{waveform}|toggle|lov={waveforms}|on_change=change_data|>
  40. <|{data}|chart|mode=lines+markers|x=x|y=y|animation_data={animation_data}|>
  41. """
  42. if __name__ == "__main__":
  43. Gui(page).run(title="Chart - Advanced - Animation")