test_chart.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Copyright 2023 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. import datetime
  12. import inspect
  13. import random
  14. import typing as t
  15. import taipy.gui.builder as tgb
  16. from taipy.gui import Gui
  17. def test_chart_builder_1(gui: Gui, helpers, csvdata):
  18. selected_indices = [14258] # noqa: F841
  19. subplot_layout = { # noqa: F841
  20. "grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}
  21. }
  22. y = ["Daily hospital occupancy", "Daily hospital occupancy"] # noqa: F841
  23. label = ["Entity", "Code"] # noqa: F841
  24. mode = [None, "markers"] # noqa: F841
  25. color = [None, "red"] # noqa: F841
  26. chart_type = [None, "scatter"] # noqa: F841
  27. xaxis = [None, "x2"] # noqa: F841
  28. with tgb.Page(frame=None) as page:
  29. tgb.chart( # type: ignore[attr-defined]
  30. data="{csvdata}",
  31. x="Day",
  32. selected_color="green",
  33. y="{y}",
  34. label="{label}",
  35. mode="{mode}",
  36. color="{color}",
  37. type="{chart_type}",
  38. xaxis="{xaxis}",
  39. layout="{subplot_layout}",
  40. on_range_change="range_change",
  41. width="100%",
  42. height="100%",
  43. selected="{selected_indices}",
  44. )
  45. expected_list = [
  46. "<Chart",
  47. "selected0={tpec_TpExPr_selected_indices_TPMDL_0}",
  48. "selected1={tpec_TpExPr_selected_indices_TPMDL_0}",
  49. 'height="100%"',
  50. 'defaultLayout="{&quot;grid&quot;: &#x7B;&quot;rows&quot;: 1, &quot;columns&quot;: 2, &quot;subplots&quot;: [[&quot;xy&quot;, &quot;x2y&quot;]], &quot;roworder&quot;: &quot;bottom to top&quot;&#x7D;}"', # noqa: E501
  51. 'onRangeChange="range_change"',
  52. 'updateVars="layout=_TpDi_tpec_TpExPr_subplot_layout_TPMDL_0;selected0=tpec_TpExPr_selected_indices_TPMDL_0;selected1=tpec_TpExPr_selected_indices_TPMDL_0"',
  53. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  54. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  55. 'width="100%"',
  56. ]
  57. if frame := inspect.currentframe():
  58. gui._set_frame(frame)
  59. helpers.test_control_builder(gui, page, expected_list)
  60. def test_chart_builder_2(gui: Gui, helpers, csvdata):
  61. selected_indices = [14258] # noqa: F841
  62. with tgb.Page(frame=None) as page:
  63. tgb.chart( # type: ignore[attr-defined]
  64. data="{csvdata}",
  65. x="Day",
  66. selected_color="green",
  67. y=["Daily hospital occupancy", "Daily hospital occupancy"],
  68. label=["Entity", "Code"],
  69. mode=[None, "markers"],
  70. color=[None, "red"],
  71. type=(None, "scatter"),
  72. xaxis=(None, "x2"),
  73. layout={"grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}},
  74. on_range_change="range_change",
  75. width="100%",
  76. height="100%",
  77. selected="{selected_indices}",
  78. )
  79. expected_list = [
  80. "<Chart",
  81. "selected0={tpec_TpExPr_selected_indices_TPMDL_0}",
  82. "selected1={tpec_TpExPr_selected_indices_TPMDL_0}",
  83. 'height="100%"',
  84. 'defaultLayout="{&quot;grid&quot;: &#x7B;&quot;rows&quot;: 1, &quot;columns&quot;: 2, &quot;subplots&quot;: [[&quot;xy&quot;, &quot;x2y&quot;]], &quot;roworder&quot;: &quot;bottom to top&quot;&#x7D;}"', # noqa: E501
  85. 'onRangeChange="range_change"',
  86. 'updateVars="selected0=tpec_TpExPr_selected_indices_TPMDL_0;selected1=tpec_TpExPr_selected_indices_TPMDL_0"',
  87. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  88. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  89. 'width="100%"',
  90. ]
  91. if frame := inspect.currentframe():
  92. gui._set_frame(frame)
  93. helpers.test_control_builder(gui, page, expected_list)
  94. def test_map_builder(gui: Gui, helpers):
  95. mapData = { # noqa: F841
  96. "Lat": [
  97. 48.4113,
  98. 18.0057,
  99. 48.6163,
  100. 48.5379,
  101. 48.5843,
  102. 48.612,
  103. 48.6286,
  104. 48.6068,
  105. 48.4489,
  106. 48.6548,
  107. 18.5721,
  108. 48.3734,
  109. 17.6398,
  110. 48.5765,
  111. 48.4407,
  112. 48.2286,
  113. ],
  114. "Lon": [
  115. -112.8352,
  116. -65.804,
  117. -113.4784,
  118. -114.0702,
  119. -111.0188,
  120. -110.7939,
  121. -109.4629,
  122. -114.9123,
  123. -112.9705,
  124. -113.965,
  125. -66.5401,
  126. -111.5245,
  127. -64.7246,
  128. -112.1932,
  129. -113.3159,
  130. -104.5863,
  131. ],
  132. "Globvalue": [
  133. 0.0875,
  134. 0.0892,
  135. 0.0908,
  136. 0.0933,
  137. 0.0942,
  138. 0.095,
  139. 0.095,
  140. 0.095,
  141. 0.0958,
  142. 0.0958,
  143. 0.0958,
  144. 0.0958,
  145. 0.0958,
  146. 0.0975,
  147. 0.0983,
  148. 0.0992,
  149. ],
  150. }
  151. marker = {"color": "fuchsia", "size": 4} # noqa: F841
  152. layout = { # noqa: F841
  153. "dragmode": "zoom",
  154. "mapbox": {"style": "open-street-map", "center": {"lat": 38, "lon": -90}, "zoom": 3},
  155. "margin": {"r": 0, "t": 0, "b": 0, "l": 0},
  156. }
  157. with tgb.Page(frame=None) as page:
  158. tgb.chart( # type: ignore[attr-defined]
  159. data="{mapData}",
  160. type="scattermapbox",
  161. marker="{marker}",
  162. layout="{layout}",
  163. lat="Lat",
  164. lon="Lon",
  165. text="Globvalue",
  166. mode="markers",
  167. )
  168. if frame := inspect.currentframe():
  169. gui._set_frame(frame)
  170. expected_list = [
  171. "<Chart",
  172. "&quot;Lat&quot;: &#x7B;&quot;index&quot;:",
  173. "&quot;Lon&quot;: &#x7B;&quot;index&quot;:",
  174. "data={_TpD_tpec_TpExPr_mapData_TPMDL_0}",
  175. 'defaultLayout="{&quot;dragmode&quot;: &quot;zoom&quot;, &quot;mapbox&quot;: &#x7B;&quot;style&quot;: &quot;open-street-map&quot;, &quot;center&quot;: &#x7B;&quot;lat&quot;: 38, &quot;lon&quot;: -90&#x7D;, &quot;zoom&quot;: 3&#x7D;, &quot;margin&quot;: &#x7B;&quot;r&quot;: 0, &quot;t&quot;: 0, &quot;b&quot;: 0, &quot;l&quot;: 0&#x7D;}"', # noqa: E501
  176. 'updateVarName="_TpD_tpec_TpExPr_mapData_TPMDL_0"',
  177. ]
  178. helpers.test_control_builder(gui, page, expected_list)
  179. def test_chart_indexed_properties_builder(gui: Gui, helpers):
  180. data: t.Dict[str, t.Any] = {}
  181. data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
  182. data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
  183. data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
  184. data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
  185. data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
  186. data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
  187. data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
  188. data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
  189. with tgb.Page(frame=None) as page:
  190. tgb.chart( # type: ignore[attr-defined]
  191. data="{data}",
  192. x="Date",
  193. mode="lines",
  194. y=["La Rochelle", "La Rochelle 1", "La Rochelle 2", "Montpellier", "Montpellier 1", "Montpellier 2"],
  195. line=[None, "dashdot", "dash", None, "dashdot", "dash"],
  196. color=[None, "blue", "blue", None, "red", "red"],
  197. )
  198. if frame := inspect.currentframe():
  199. gui._set_frame(frame)
  200. expected_list = [
  201. "<Chart",
  202. "&quot;traces&quot;: [[&quot;Date_str&quot;, &quot;La Rochelle&quot;], [&quot;Date_str&quot;, &quot;La Rochelle 1&quot;], [&quot;Date_str&quot;, &quot;La Rochelle 2&quot;], [&quot;Date_str&quot;, &quot;Montpellier&quot;], [&quot;Date_str&quot;, &quot;Montpellier 1&quot;], [&quot;Date_str&quot;, &quot;Montpellier 2&quot;]]", # noqa: E501
  203. "&quot;lines&quot;: [null, &#x7B;&quot;dash&quot;: &quot;dashdot&quot;&#x7D;, &#x7B;&quot;dash&quot;: &quot;dash&quot;&#x7D;, null, &#x7B;&quot;dash&quot;: &quot;dashdot&quot;&#x7D;, &#x7B;&quot;dash&quot;: &quot;dash&quot;&#x7D;]", # noqa: E501
  204. ]
  205. helpers.test_control_builder(gui, page, expected_list)
  206. def test_chart_indexed_properties_with_arrays_builder(gui: Gui, helpers):
  207. data: t.Dict[str, t.Any] = {}
  208. data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
  209. data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
  210. data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
  211. data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
  212. data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
  213. data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
  214. data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
  215. data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
  216. ys = [ # noqa: F841
  217. "La Rochelle",
  218. "La Rochelle 1",
  219. "La Rochelle 2",
  220. "Montpellier",
  221. "Montpellier 1",
  222. "Montpellier 2",
  223. ]
  224. lines = [None, "dashdot", "dash", None, "dashdot", "dash"] # noqa: F841
  225. colors = [None, "blue", "blue", None, "red", "red"] # noqa: F841
  226. with tgb.Page(frame=None) as page:
  227. tgb.chart( # type: ignore[attr-defined]
  228. data="{data}",
  229. x="Date",
  230. mode="lines",
  231. y="{ys}",
  232. line="{lines}",
  233. color="{colors}",
  234. )
  235. if frame := inspect.currentframe():
  236. gui._set_frame(frame)
  237. expected_list = [
  238. "<Chart",
  239. "&quot;traces&quot;: [[&quot;Date_str&quot;, &quot;La Rochelle&quot;], [&quot;Date_str&quot;, &quot;La Rochelle 1&quot;], [&quot;Date_str&quot;, &quot;La Rochelle 2&quot;], [&quot;Date_str&quot;, &quot;Montpellier&quot;], [&quot;Date_str&quot;, &quot;Montpellier 1&quot;], [&quot;Date_str&quot;, &quot;Montpellier 2&quot;]]", # noqa: E501
  240. "&quot;lines&quot;: [null, &#x7B;&quot;dash&quot;: &quot;dashdot&quot;&#x7D;, &#x7B;&quot;dash&quot;: &quot;dash&quot;&#x7D;, null, &#x7B;&quot;dash&quot;: &quot;dashdot&quot;&#x7D;, &#x7B;&quot;dash&quot;: &quot;dash&quot;&#x7D;]", # noqa: E501
  241. ]
  242. helpers.test_control_builder(gui, page, expected_list)