test_chart.py 11 KB

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