test_chart.py 11 KB

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