1
0

test_chart.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. 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. gui._set_frame(inspect.currentframe())
  58. helpers.test_control_builder(gui, page, expected_list)
  59. def test_chart_builder_2(gui: Gui, helpers, csvdata):
  60. selected_indices = [14258] # noqa: F841
  61. with tgb.Page(frame=None) as page:
  62. tgb.chart( # type: ignore[attr-defined]
  63. data="{csvdata}",
  64. x="Day",
  65. selected_color="green",
  66. y=["Daily hospital occupancy", "Daily hospital occupancy"],
  67. label=["Entity", "Code"],
  68. mode=[None, "markers"],
  69. color=[None, "red"],
  70. type=(None, "scatter"),
  71. xaxis=(None, "x2"),
  72. layout={"grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}},
  73. on_range_change="range_change",
  74. width="100%",
  75. height="100%",
  76. selected="{selected_indices}",
  77. )
  78. expected_list = [
  79. "<Chart",
  80. "selected0={tpec_TpExPr_selected_indices_TPMDL_0}",
  81. "selected1={tpec_TpExPr_selected_indices_TPMDL_0}",
  82. 'height="100%"',
  83. '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
  84. 'onRangeChange="range_change"',
  85. 'updateVars="selected0=tpec_TpExPr_selected_indices_TPMDL_0;selected1=tpec_TpExPr_selected_indices_TPMDL_0"',
  86. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  87. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  88. 'width="100%"',
  89. ]
  90. gui._set_frame(inspect.currentframe())
  91. helpers.test_control_builder(gui, page, expected_list)
  92. def test_map_builder(gui: Gui, helpers):
  93. mapData = { # noqa: F841
  94. "Lat": [
  95. 48.4113,
  96. 18.0057,
  97. 48.6163,
  98. 48.5379,
  99. 48.5843,
  100. 48.612,
  101. 48.6286,
  102. 48.6068,
  103. 48.4489,
  104. 48.6548,
  105. 18.5721,
  106. 48.3734,
  107. 17.6398,
  108. 48.5765,
  109. 48.4407,
  110. 48.2286,
  111. ],
  112. "Lon": [
  113. -112.8352,
  114. -65.804,
  115. -113.4784,
  116. -114.0702,
  117. -111.0188,
  118. -110.7939,
  119. -109.4629,
  120. -114.9123,
  121. -112.9705,
  122. -113.965,
  123. -66.5401,
  124. -111.5245,
  125. -64.7246,
  126. -112.1932,
  127. -113.3159,
  128. -104.5863,
  129. ],
  130. "Globvalue": [
  131. 0.0875,
  132. 0.0892,
  133. 0.0908,
  134. 0.0933,
  135. 0.0942,
  136. 0.095,
  137. 0.095,
  138. 0.095,
  139. 0.0958,
  140. 0.0958,
  141. 0.0958,
  142. 0.0958,
  143. 0.0958,
  144. 0.0975,
  145. 0.0983,
  146. 0.0992,
  147. ],
  148. }
  149. marker = {"color": "fuchsia", "size": 4} # noqa: F841
  150. layout = { # noqa: F841
  151. "dragmode": "zoom",
  152. "map": {"style": "open-street-map", "center": {"lat": 38, "lon": -90}, "zoom": 3},
  153. "margin": {"r": 0, "t": 0, "b": 0, "l": 0},
  154. }
  155. with tgb.Page(frame=None) as page:
  156. tgb.chart( # type: ignore[attr-defined]
  157. data="{mapData}",
  158. type="scattermap",
  159. marker="{marker}",
  160. layout="{layout}",
  161. lat="Lat",
  162. lon="Lon",
  163. text="Globvalue",
  164. mode="markers",
  165. )
  166. gui._set_frame(inspect.currentframe())
  167. expected_list = [
  168. "<Chart",
  169. "&quot;Lat&quot;: &#x7B;&quot;",
  170. "&quot;Lon&quot;: &#x7B;&quot;",
  171. "data={_TpD_tpec_TpExPr_mapData_TPMDL_0}",
  172. 'defaultLayout="{&quot;dragmode&quot;: &quot;zoom&quot;, &quot;map&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
  173. 'updateVarName="_TpD_tpec_TpExPr_mapData_TPMDL_0"',
  174. ]
  175. helpers.test_control_builder(gui, page, expected_list)
  176. def test_chart_indexed_properties_builder(gui: Gui, helpers):
  177. data: t.Dict[str, t.Any] = {}
  178. data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
  179. data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
  180. data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
  181. data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
  182. data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
  183. data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
  184. data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
  185. data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
  186. with tgb.Page(frame=None) as page:
  187. tgb.chart( # type: ignore[attr-defined]
  188. data="{data}",
  189. x="Date",
  190. mode="lines",
  191. y=["La Rochelle", "La Rochelle 1", "La Rochelle 2", "Montpellier", "Montpellier 1", "Montpellier 2"],
  192. line=[None, "dashdot", "dash", None, "dashdot", "dash"],
  193. color=[None, "blue", "blue", None, "red", "red"],
  194. )
  195. gui._set_frame(inspect.currentframe())
  196. expected_list = [
  197. "<Chart",
  198. "&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
  199. "&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
  200. ]
  201. helpers.test_control_builder(gui, page, expected_list)
  202. def test_chart_indexed_properties_with_arrays_builder(gui: Gui, helpers):
  203. data: t.Dict[str, t.Any] = {}
  204. data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
  205. data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
  206. data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
  207. data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
  208. data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
  209. data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
  210. data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
  211. data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
  212. ys = [ # noqa: F841
  213. "La Rochelle",
  214. "La Rochelle 1",
  215. "La Rochelle 2",
  216. "Montpellier",
  217. "Montpellier 1",
  218. "Montpellier 2",
  219. ]
  220. lines = [None, "dashdot", "dash", None, "dashdot", "dash"] # noqa: F841
  221. colors = [None, "blue", "blue", None, "red", "red"] # noqa: F841
  222. with tgb.Page(frame=None) as page:
  223. tgb.chart( # type: ignore[attr-defined]
  224. data="{data}",
  225. x="Date",
  226. mode="lines",
  227. y="{ys}",
  228. line="{lines}",
  229. color="{colors}",
  230. )
  231. gui._set_frame(inspect.currentframe())
  232. expected_list = [
  233. "<Chart",
  234. "&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
  235. "&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
  236. ]
  237. helpers.test_control_builder(gui, page, expected_list)
  238. def test_chart_multi_data(gui: Gui, helpers, csvdata):
  239. with tgb.Page(frame=None) as page:
  240. tgb.chart( # type: ignore[attr-defined]
  241. data="{csvdata}",
  242. x="Day",
  243. y="Daily hospital occupancy",
  244. data__1="{csvdata}",
  245. )
  246. expected_list = [
  247. "<Chart",
  248. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  249. 'dataVarNames="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  250. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  251. "data1={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  252. ]
  253. gui._set_frame(inspect.currentframe())
  254. helpers.test_control_builder(gui, page, expected_list)
  255. def test_chart_none_data(gui: Gui, helpers):
  256. data=None # noqa: F841
  257. with tgb.Page(frame=None) as page:
  258. tgb.chart( # type: ignore[attr-defined]
  259. data="{data}",
  260. )
  261. expected_list = [
  262. "<Chart",
  263. "data={_TpD_tpec_TpExPr_data_TPMDL_0}",
  264. 'dataVarNames=""',
  265. 'defaultConfig="{&quot;traces&quot;: []}"',
  266. 'libClassName="taipy-chart"',
  267. 'updateVarName="_TpD_tpec_TpExPr_data_TPMDL_0"',
  268. ]
  269. gui._set_frame(inspect.currentframe())
  270. helpers.test_control_builder(gui, page, expected_list)