polar-tick-texts.py 3.9 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. # Update time on every refresh
  40. def on_navigate(state, page):
  41. state.data = generate_hand_shapes()
  42. return page
  43. if __name__ == "__main__":
  44. # Initialize the data set with the current time
  45. data = generate_hand_shapes()
  46. layout = {
  47. "polar": {
  48. "angularaxis": {
  49. "rotation": 90,
  50. "direction": "clockwise",
  51. # One tick every 30 degrees
  52. "tickvals": list(numpy.arange(0.0, 360.0, 30)),
  53. # Text value for every tick
  54. "ticktext": ["XII", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI"],
  55. },
  56. "radialaxis": {"angle": 90, "visible": False, "range": [0, 12]},
  57. },
  58. "showlegend": False,
  59. }
  60. # Options to be used for all three traces
  61. base_opts = {"fill": "toself"}
  62. # Specific for hours
  63. hours_opts = dict(base_opts)
  64. hours_opts["fillcolor"] = "#FF0000"
  65. # Specific for minutes
  66. minutes_opts = dict(base_opts)
  67. minutes_opts["fillcolor"] = "#00FF00"
  68. # Specific for seconds
  69. seconds_opts = dict(base_opts)
  70. seconds_opts["fillcolor"] = "#0000FF"
  71. # Store all the chart control properties in a single object
  72. properties = {
  73. # Don't show data point markers
  74. "mode": "lines",
  75. # Data for the hours
  76. "theta[1]": "0/a",
  77. "r[1]": "0/r",
  78. # Data for the minutes
  79. "theta[2]": "1/a",
  80. "r[2]": "1/r",
  81. # Data for the seconds
  82. "theta[3]": "2/a",
  83. "r[3]": "2/r",
  84. # Options for the three traces
  85. "options[1]": hours_opts,
  86. "options[2]": minutes_opts,
  87. "options[3]": seconds_opts,
  88. "line": {"color": "black"},
  89. "layout": layout,
  90. }
  91. page = """
  92. # Polar - Tick texts
  93. <|{data}|chart|type=scatterpolar|properties={properties}|>
  94. """
  95. Gui(page).run()