Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding color-map property to Metric #1407

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions doc/gui/examples/controls/metric-color-map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2021-2024 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.
# -----------------------------------------------------------------------------------------
# To execute this script, make sure that the taipy-gui package is installed in your
# Python environment and run:
# python <script>
# -----------------------------------------------------------------------------------------
from taipy.gui import Gui

# Layout reference can be found in the documentation: https://plotly.com/python/reference/layout/

value = 50
color_map = {
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
20: "#fd2020",
40: None,
60: "#f3ff26",
80: None
}

page = """
<|{value}|metric|color_map={color_map}|>
"""

Gui(page).run()

14 changes: 12 additions & 2 deletions frontend/taipy-gui/src/components/Taipy/Metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import {useClassNames, useDynamicJsonProperty, useDynamicProperty} from "../../utils/hooks";
import {extractPrefix, extractSuffix, sprintfToD3Converter} from "../../utils/formatConversion";
import {TaipyBaseProps, TaipyHoverProps} from "./utils";
import { darkThemeTemplate } from "../../themes/darkThemeTemplate";
import {darkThemeTemplate} from "../../themes/darkThemeTemplate";

const Plot = lazy(() => import("react-plotly.js"));

Expand All @@ -44,6 +44,7 @@
showValue?: boolean;
format?: string;
deltaFormat?: string;
colorMap?: string;
template?: string;
template_Dark_?: string;
template_Light_?: string;
Expand Down Expand Up @@ -91,6 +92,14 @@
props.max || 100
]
},
steps: props.colorMap ? (() => {

Check warning on line 95 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const obj = JSON.parse(props.colorMap);

Check warning on line 96 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
const keys = Object.keys(obj);

Check warning on line 97 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return keys.sort((a, b) => parseFloat(a) - parseFloat(b)).map((key, index) => {

Check warning on line 98 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 98 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function

Check warning on line 98 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const nextKey = keys[index + 1] !== undefined ? parseFloat(keys[index + 1]) : props.max;

Check warning on line 99 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 99 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 99 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return {range: [parseFloat(key), nextKey], color: obj[key]};

Check warning on line 100 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}).filter(item => item.color !== null);

Check warning on line 101 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 101 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 101 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
})() : undefined,

Check warning on line 102 in frontend/taipy-gui/src/components/Taipy/Metric.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
shape: props.type === "linear" ? "bullet" : "angular",
threshold: {
line: {color: "red", width: 4},
Expand All @@ -102,6 +111,7 @@
];
}, [
props.format,
props.colorMap,
props.deltaFormat,
props.min,
props.max,
Expand Down Expand Up @@ -168,7 +178,7 @@

export default Metric;

const { colorscale, colorway, font} = darkThemeTemplate.layout;
const {colorscale, colorway, font} = darkThemeTemplate.layout;
const darkTemplate = {
layout: {
colorscale,
Expand Down
1 change: 1 addition & 0 deletions taipy/gui/_renderers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ class _Factory:
("show_value", PropertyType.boolean, True),
("format", PropertyType.string),
("delta_format", PropertyType.string),
("color_map", PropertyType.dict),
("template", PropertyType.dict),
("template[dark]", PropertyType.dict),
("template[light]", PropertyType.dict),
Expand Down
5 changes: 5 additions & 0 deletions taipy/gui/viselements.json
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,11 @@
"type": "str",
"doc": "The format to use when displaying the delta value.<br/>This uses the <code>printf</code> syntax."
},
{
"name": "color_map",
"type": "dict",
"doc": "The color_map is used to display different colors for different ranges of the metric. The color_map's keys represent the starting point of each range, which is a number, while the values represent the corresponding color for that range. If the value associated with a key is set to None, it implies that the corresponding range will not be assigned any color."
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
},
{
"name": "width",
"type": "str|number",
Expand Down
Loading