continuous-error-multiple.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright 2021-2024 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. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. import datetime
  17. from typing import Dict, List
  18. import dateutil.relativedelta
  19. from taipy.gui import Gui
  20. # Data is collected from January 1st, 2010, every month
  21. start_date = datetime.datetime(year=2010, month=1, day=1)
  22. period = dateutil.relativedelta.relativedelta(months=1)
  23. # Data
  24. # All arrays have the same size (the number of months to track)
  25. prices: Dict[str, List] = {
  26. # Data for apples
  27. "apples": [2.48, 2.47, 2.5, 2.47, 2.46, 2.38, 2.31, 2.25, 2.39, 2.41, 2.59, 2.61],
  28. "apples_low": [1.58, 1.58, 1.59, 1.64, 1.79, 1.54, 1.53, 1.61, 1.65, 2.02, 1.92, 1.54],
  29. "apples_high": [3.38, 3.32, 2.63, 2.82, 2.58, 2.53, 3.27, 3.15, 3.44, 3.42, 3.08, 2.86],
  30. "bananas": [2.94, 2.50, 2.39, 2.77, 2.43, 2.32, 2.37, 1.90, 2.31, 2.71, 3.38, 1.92],
  31. "bananas_low": [2.12, 1.90, 1.69, 2.44, 1.58, 1.81, 1.44, 1.00, 1.59, 1.74, 2.78, 0.96],
  32. "bananas_high": [3.32, 2.70, 3.12, 3.25, 3.00, 2.63, 2.54, 2.37, 2.97, 3.69, 4.36, 2.95],
  33. "cherries": [6.18, None, None, None, 3.69, 2.46, 2.31, 2.57, None, None, 6.50, 4.38],
  34. "cherries_high": [7.00, None, None, None, 8.50, 6.27, 5.61, 4.36, None, None, 8.00, 7.23],
  35. "cherries_low": [3.55, None, None, None, 1.20, 0.87, 1.08, 1.50, None, None, 5.00, 4.20],
  36. }
  37. # Create monthly time series
  38. months = [start_date + n * period for n in range(0, len(prices["apples"]))]
  39. data = [
  40. # Raw data
  41. {"Months": months, "apples": prices["apples"], "bananas": prices["bananas"], "cherries": prices["cherries"]},
  42. # Range data (twice as many values)
  43. {
  44. "Months2": months + list(reversed(months)),
  45. "apples": prices["apples_high"] + list(reversed(prices["apples_low"])),
  46. "bananas": prices["bananas_high"] + list(reversed(prices["bananas_low"])),
  47. "cherries": prices["cherries_high"] + list(reversed(prices["cherries_low"])),
  48. },
  49. ]
  50. properties = {
  51. # First trace: reference for Apples
  52. "x[1]": "0/Months",
  53. "y[1]": "0/apples",
  54. "color[1]": "rgb(0,200,80)",
  55. # Hide line
  56. "mode[1]": "markers",
  57. # Show in the legend
  58. "name[1]": "Apples",
  59. # Second trace: reference for Bananas
  60. "x[2]": "0/Months",
  61. "y[2]": "0/bananas",
  62. "color[2]": "rgb(0,100,240)",
  63. # Hide line
  64. "mode[2]": "markers",
  65. # Show in the legend
  66. "name[2]": "Bananas",
  67. # Third trace: reference for Cherries
  68. "x[3]": "0/Months",
  69. "y[3]": "0/cherries",
  70. "color[3]": "rgb(240,60,60)",
  71. # Hide line
  72. "mode[3]": "markers",
  73. # Show in the legend
  74. "name[3]": "Cherries",
  75. # Fourth trace: range for Apples
  76. "x[4]": "1/Months2",
  77. "y[4]": "1/apples",
  78. "options[4]": {
  79. "fill": "tozerox",
  80. "showlegend": False,
  81. "fillcolor": "rgba(0,100,80,0.4)",
  82. },
  83. # No surrounding stroke
  84. "color[4]": "transparent",
  85. # Fifth trace: range for Bananas
  86. "x[5]": "1/Months2",
  87. "y[5]": "1/bananas",
  88. "options[5]": {"fill": "tozerox", "showlegend": False, "fillcolor": "rgba(0,180,250,0.4)"},
  89. # No surrounding stroke
  90. "color[5]": "transparent",
  91. # Sixth trace: range for Cherries
  92. "x[6]": "1/Months2",
  93. "y[6]": "1/cherries",
  94. "options[6]": {
  95. "fill": "tozerox",
  96. "showlegend": False,
  97. "fillcolor": "rgba(230,100,120,0.4)",
  98. },
  99. # No surrounding stroke
  100. "color[6]": "transparent",
  101. }
  102. page = """
  103. <|{data}|chart|properties={properties}|>
  104. """
  105. if __name__ == "__main__":
  106. Gui(page).run(title="Chart - Continuous Error - Multiple traces")