polar-tick-texts.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright 2021-2024 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 datetime import datetime
  17. import numpy
  18. from taipy.gui import Gui
  19. def generate_hand_shapes():
  20. # Retrieve and store the current, local time
  21. time_now = datetime.now()
  22. hours = time_now.hour
  23. minutes = time_now.minute
  24. seconds = time_now.second
  25. # Compute the angle that represents the hours
  26. hours_angle = 360 * ((hours % 12) / 12 + (minutes % 60) / 60 / 60 + (seconds % 60) / 60 / 60 / 60)
  27. # Short and thick hand for the hours
  28. hours_hand = {"r": [0, 4, 5, 4, 0], "a": [0, hours_angle - 7, hours_angle, hours_angle + 7, 0]}
  29. # Compute the angle that represents the minutes
  30. minutes_angle = 360 * ((minutes % 60) / 60 + (seconds % 60) / 60 / 60)
  31. # Longer and slightly thinner hand for the minutes
  32. minutes_hand = {"r": [0, 6, 8, 6, 0], "a": [0, minutes_angle - 4, minutes_angle, minutes_angle + 4, 0]}
  33. # Compute the angle that represents the seconds
  34. seconds_angle = 360 * (seconds % 60) / 60
  35. # Even longer and thinner hand for the seconds
  36. seconds_hand = {"r": [0, 8, 10, 8, 0], "a": [0, seconds_angle - 2, seconds_angle, seconds_angle + 2, 0]}
  37. # Build and return the whole data set
  38. return [hours_hand, minutes_hand, seconds_hand]
  39. # Initialize the data set with the current time
  40. data = generate_hand_shapes()
  41. layout = {
  42. "polar": {
  43. "angularaxis": {
  44. "rotation": 90,
  45. "direction": "clockwise",
  46. # One tick every 30 degrees
  47. "tickvals": list(numpy.arange(0.0, 360.0, 30)),
  48. # Text value for every tick
  49. "ticktext": ["XII", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI"],
  50. },
  51. "radialaxis": {"angle": 90, "visible": False, "range": [0, 12]},
  52. },
  53. "showlegend": False,
  54. }
  55. # Options to be used for all three traces
  56. base_opts = {"fill": "toself"}
  57. # Specific for hours
  58. hours_opts = dict(base_opts)
  59. hours_opts["fillcolor"] = "#FF0000"
  60. # Specific for minutes
  61. minutes_opts = dict(base_opts)
  62. minutes_opts["fillcolor"] = "#00FF00"
  63. # Specific for seconds
  64. seconds_opts = dict(base_opts)
  65. seconds_opts["fillcolor"] = "#0000FF"
  66. # Store all the chart control properties in a single object
  67. properties = {
  68. # Don't show data point markers
  69. "mode": "lines",
  70. # Data for the hours
  71. "theta[1]": "0/a",
  72. "r[1]": "0/r",
  73. # Data for the minutes
  74. "theta[2]": "1/a",
  75. "r[2]": "1/r",
  76. # Data for the seconds
  77. "theta[3]": "2/a",
  78. "r[3]": "2/r",
  79. # Options for the three traces
  80. "options[1]": hours_opts,
  81. "options[2]": minutes_opts,
  82. "options[3]": seconds_opts,
  83. "line": {"color": "black"},
  84. "layout": layout,
  85. }
  86. # Update time on every refresh
  87. def on_navigate(state, page):
  88. state.data = generate_hand_shapes()
  89. return page
  90. page = """
  91. # Polar - Tick texts
  92. <|{data}|chart|type=scatterpolar|properties={properties}|>
  93. """
  94. Gui(page).run()