polar_tick_texts.py 3.7 KB

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