123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- # Copyright 2021-2025 Avaiga Private Limited
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- # the License. You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- # specific language governing permissions and limitations under the License.
- import datetime
- import inspect
- import random
- import typing as t
- import taipy.gui.builder as tgb
- from taipy.gui import Gui
- def test_chart_builder_1(gui: Gui, helpers, csvdata):
- selected_indices = [14258] # noqa: F841
- subplot_layout = { # noqa: F841
- "grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}
- }
- y = ["Daily hospital occupancy", "Daily hospital occupancy"] # noqa: F841
- label = ["Entity", "Code"] # noqa: F841
- mode = [None, "markers"] # noqa: F841
- color = [None, "red"] # noqa: F841
- chart_type = [None, "scatter"] # noqa: F841
- xaxis = [None, "x2"] # noqa: F841
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{csvdata}",
- x="Day",
- selected_color="green",
- y="{y}",
- label="{label}",
- mode="{mode}",
- color="{color}",
- type="{chart_type}",
- xaxis="{xaxis}",
- layout="{subplot_layout}",
- on_range_change="range_change",
- width="100%",
- height="100%",
- selected="{selected_indices}",
- )
- expected_list = [
- "<Chart",
- "selected0={tpec_TpExPr_selected_indices_TPMDL_0}",
- "selected1={tpec_TpExPr_selected_indices_TPMDL_0}",
- 'height="100%"',
- 'defaultLayout="{"grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}}"', # noqa: E501
- 'onRangeChange="range_change"',
- 'updateVars="layout=_TpDi_tpec_TpExPr_subplot_layout_TPMDL_0;selected0=tpec_TpExPr_selected_indices_TPMDL_0;selected1=tpec_TpExPr_selected_indices_TPMDL_0"',
- 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
- "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
- 'width="100%"',
- ]
- gui._set_frame(inspect.currentframe())
- helpers.test_control_builder(gui, page, expected_list)
- def test_chart_builder_2(gui: Gui, helpers, csvdata):
- selected_indices = [14258] # noqa: F841
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{csvdata}",
- x="Day",
- selected_color="green",
- y=["Daily hospital occupancy", "Daily hospital occupancy"],
- label=["Entity", "Code"],
- mode=[None, "markers"],
- color=[None, "red"],
- type=(None, "scatter"),
- xaxis=(None, "x2"),
- layout={"grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}},
- on_range_change="range_change",
- width="100%",
- height="100%",
- selected="{selected_indices}",
- )
- expected_list = [
- "<Chart",
- "selected0={tpec_TpExPr_selected_indices_TPMDL_0}",
- "selected1={tpec_TpExPr_selected_indices_TPMDL_0}",
- 'height="100%"',
- 'defaultLayout="{"grid": {"rows": 1, "columns": 2, "subplots": [["xy", "x2y"]], "roworder": "bottom to top"}}"', # noqa: E501
- 'onRangeChange="range_change"',
- 'updateVars="selected0=tpec_TpExPr_selected_indices_TPMDL_0;selected1=tpec_TpExPr_selected_indices_TPMDL_0"',
- 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
- "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
- 'width="100%"',
- ]
- gui._set_frame(inspect.currentframe())
- helpers.test_control_builder(gui, page, expected_list)
- def test_map_builder(gui: Gui, helpers):
- mapData = { # noqa: F841
- "Lat": [
- 48.4113,
- 18.0057,
- 48.6163,
- 48.5379,
- 48.5843,
- 48.612,
- 48.6286,
- 48.6068,
- 48.4489,
- 48.6548,
- 18.5721,
- 48.3734,
- 17.6398,
- 48.5765,
- 48.4407,
- 48.2286,
- ],
- "Lon": [
- -112.8352,
- -65.804,
- -113.4784,
- -114.0702,
- -111.0188,
- -110.7939,
- -109.4629,
- -114.9123,
- -112.9705,
- -113.965,
- -66.5401,
- -111.5245,
- -64.7246,
- -112.1932,
- -113.3159,
- -104.5863,
- ],
- "Globvalue": [
- 0.0875,
- 0.0892,
- 0.0908,
- 0.0933,
- 0.0942,
- 0.095,
- 0.095,
- 0.095,
- 0.0958,
- 0.0958,
- 0.0958,
- 0.0958,
- 0.0958,
- 0.0975,
- 0.0983,
- 0.0992,
- ],
- }
- marker = {"color": "fuchsia", "size": 4} # noqa: F841
- layout = { # noqa: F841
- "dragmode": "zoom",
- "map": {"style": "open-street-map", "center": {"lat": 38, "lon": -90}, "zoom": 3},
- "margin": {"r": 0, "t": 0, "b": 0, "l": 0},
- }
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{mapData}",
- type="scattermap",
- marker="{marker}",
- layout="{layout}",
- lat="Lat",
- lon="Lon",
- text="Globvalue",
- mode="markers",
- )
- gui._set_frame(inspect.currentframe())
- expected_list = [
- "<Chart",
- ""Lat": {"",
- ""Lon": {"",
- "data={_TpD_tpec_TpExPr_mapData_TPMDL_0}",
- 'defaultLayout="{"dragmode": "zoom", "map": {"style": "open-street-map", "center": {"lat": 38, "lon": -90}, "zoom": 3}, "margin": {"r": 0, "t": 0, "b": 0, "l": 0}}"', # noqa: E501
- 'updateVarName="_TpD_tpec_TpExPr_mapData_TPMDL_0"',
- ]
- helpers.test_control_builder(gui, page, expected_list)
- def test_chart_indexed_properties_builder(gui: Gui, helpers):
- data: t.Dict[str, t.Any] = {}
- data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
- data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
- data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
- data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
- data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
- data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
- data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
- data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{data}",
- x="Date",
- mode="lines",
- y=["La Rochelle", "La Rochelle 1", "La Rochelle 2", "Montpellier", "Montpellier 1", "Montpellier 2"],
- line=[None, "dashdot", "dash", None, "dashdot", "dash"],
- color=[None, "blue", "blue", None, "red", "red"],
- )
- gui._set_frame(inspect.currentframe())
- expected_list = [
- "<Chart",
- ""traces": [["Date_str", "La Rochelle"], ["Date_str", "La Rochelle 1"], ["Date_str", "La Rochelle 2"], ["Date_str", "Montpellier"], ["Date_str", "Montpellier 1"], ["Date_str", "Montpellier 2"]]", # noqa: E501
- ""lines": [null, {"dash": "dashdot"}, {"dash": "dash"}, null, {"dash": "dashdot"}, {"dash": "dash"}]", # noqa: E501
- ]
- helpers.test_control_builder(gui, page, expected_list)
- def test_chart_indexed_properties_with_arrays_builder(gui: Gui, helpers):
- data: t.Dict[str, t.Any] = {}
- data["Date"] = [datetime.datetime(2021, 12, i) for i in range(1, 31)]
- data["La Rochelle"] = [10 + 6 * random.random() for _ in range(1, 31)]
- data["Montpellier"] = [16 + 6 * random.random() for _ in range(1, 31)]
- data["Paris"] = [6 + 6 * random.random() for _ in range(1, 31)]
- data["La Rochelle 1"] = [x * (1 + (random.random() / 10)) for x in data["La Rochelle"]]
- data["La Rochelle 2"] = [x * (1 - (random.random() / 10)) for x in data["La Rochelle"]]
- data["Montpellier 1"] = [x * (1 + (random.random() / 10)) for x in data["Montpellier"]]
- data["Montpellier 2"] = [x * (1 - (random.random() / 10)) for x in data["Montpellier"]]
- ys = [ # noqa: F841
- "La Rochelle",
- "La Rochelle 1",
- "La Rochelle 2",
- "Montpellier",
- "Montpellier 1",
- "Montpellier 2",
- ]
- lines = [None, "dashdot", "dash", None, "dashdot", "dash"] # noqa: F841
- colors = [None, "blue", "blue", None, "red", "red"] # noqa: F841
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{data}",
- x="Date",
- mode="lines",
- y="{ys}",
- line="{lines}",
- color="{colors}",
- )
- gui._set_frame(inspect.currentframe())
- expected_list = [
- "<Chart",
- ""traces": [["Date_str", "La Rochelle"], ["Date_str", "La Rochelle 1"], ["Date_str", "La Rochelle 2"], ["Date_str", "Montpellier"], ["Date_str", "Montpellier 1"], ["Date_str", "Montpellier 2"]]", # noqa: E501
- ""lines": [null, {"dash": "dashdot"}, {"dash": "dash"}, null, {"dash": "dashdot"}, {"dash": "dash"}]", # noqa: E501
- ]
- helpers.test_control_builder(gui, page, expected_list)
- def test_chart_multi_data(gui: Gui, helpers, csvdata):
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{csvdata}",
- x="Day",
- y="Daily hospital occupancy",
- data__1="{csvdata}",
- )
- expected_list = [
- "<Chart",
- 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
- 'dataVarNames="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
- "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
- "data1={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
- ]
- gui._set_frame(inspect.currentframe())
- helpers.test_control_builder(gui, page, expected_list)
- def test_chart_none_data(gui: Gui, helpers):
- data=None # noqa: F841
- with tgb.Page(frame=None) as page:
- tgb.chart( # type: ignore[attr-defined]
- data="{data}",
- )
- expected_list = [
- "<Chart",
- "data={_TpD_tpec_TpExPr_data_TPMDL_0}",
- 'dataVarNames=""',
- 'defaultConfig="{"traces": []}"',
- 'libClassName="taipy-chart"',
- 'updateVarName="_TpD_tpec_TpExPr_data_TPMDL_0"',
- ]
- gui._set_frame(inspect.currentframe())
- helpers.test_control_builder(gui, page, expected_list)
|