treemap-hierarchical-values.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 taipy.gui import Gui
  17. if __name__ == "__main__":
  18. # Major countries and their surface (in km2), for every continent
  19. # Source: https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_area
  20. continents = {
  21. "Africa": [
  22. {"name": "Algeria", "surface": 2381741},
  23. {"name": "Dem. Rep. Congo", "surface": 2344858},
  24. {"name": "Sudan", "surface": 1886068},
  25. {"name": "Libya", "surface": 1759540},
  26. {"name": "Chad", "surface": 1284000},
  27. ],
  28. "Asia": [
  29. {"name": "Russia-Asia", "surface": 17098246},
  30. {"name": "China", "surface": 9596961},
  31. {"name": "India", "surface": 3287263},
  32. {"name": "Kazakhstan", "surface": 2724900},
  33. {"name": "Saudi Arabia", "surface": 2149690},
  34. ],
  35. "Europe": [
  36. {"name": "Russia-Eur", "surface": 3972400},
  37. {"name": "Ukraine", "surface": 603628},
  38. {"name": "France", "surface": 551695},
  39. {"name": "Spain", "surface": 498980},
  40. {"name": "Sweden", "surface": 450295},
  41. ],
  42. "Americas": [
  43. {"name": "Canada", "surface": 9984670},
  44. {"name": "U.S.A.", "surface": 9833517},
  45. {"name": "Brazil", "surface": 8515767},
  46. {"name": "Argentina", "surface": 2780400},
  47. {"name": "Mexico", "surface": 1964375},
  48. ],
  49. "Oceania": [
  50. {"name": "Australia", "surface": 7692024},
  51. {"name": "Papua New Guinea", "surface": 462840},
  52. {"name": "New Zealand", "surface": 270467},
  53. {"name": "Solomon Islands", "surface": 28896},
  54. {"name": "Fiji", "surface": 18274},
  55. ],
  56. "Antarctica": [{"name": "Whole", "surface": 14200000}],
  57. }
  58. name: list = []
  59. surface: list = []
  60. continent: list = []
  61. for continent_name, countries in continents.items():
  62. # Create continent in root rectangle
  63. name.append(continent_name)
  64. surface.append(0)
  65. continent.append("")
  66. # Create countries in that continent rectangle
  67. for country in countries:
  68. name.append(country["name"])
  69. surface.append(country["surface"])
  70. continent.append(continent_name)
  71. data = {"names": name, "surfaces": surface, "continent": continent}
  72. page = """
  73. # TreeMap - Hierarchical values
  74. <|{data}|chart|type=treemap|labels=names|values=surfaces|parents=continent|>
  75. """
  76. Gui(page).run()