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 theme for metric #1364

Merged
merged 6 commits into from
Jun 8, 2024
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
95 changes: 94 additions & 1 deletion frontend/taipy-gui/src/components/Taipy/Metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import {useClassNames, useDynamicJsonProperty, useDynamicProperty} from "../../utils/hooks";
import {extractPrefix, extractSuffix, sprintfToD3Converter} from "../../utils/formatConversion";
import {TaipyBaseProps, TaipyHoverProps} from "./utils";
import {useTheme} from "@mui/material";

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

Expand All @@ -42,6 +43,9 @@
showValue?: boolean;
format?: string;
deltaFormat?: string;
template?: string;
template_Dark_?: string;
template_Light_?: string;
}

const emptyLayout = {} as Record<string, Record<string, unknown>>;
Expand All @@ -59,6 +63,7 @@
const className = useClassNames(props.libClassName, props.dynamicClassName, props.className);
const baseLayout = useDynamicJsonProperty(props.layout, props.defaultLayout || "", emptyLayout);
const hover = useDynamicProperty(props.hoverText, props.defaultHoverText, undefined);
const theme = useTheme();

const data = useMemo(() => {
return [
Expand Down Expand Up @@ -116,13 +121,41 @@

const skelStyle = useMemo(() => ({...style, minHeight: "7em"}), [style]);

const layout = useMemo(() => {
const layout = {...baseLayout};
let template = undefined;
try {
const tpl = props.template && JSON.parse(props.template);

Check warning on line 128 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
const tplTheme =
theme.palette.mode === "dark"
? props.template_Dark_
? JSON.parse(props.template_Dark_)

Check warning on line 132 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
: darkTemplate

Check warning on line 133 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 133 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
: props.template_Light_ && JSON.parse(props.template_Light_);

Check warning on line 134 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
template = tpl ? (tplTheme ? {...tpl, ...tplTheme} : tpl) : tplTheme ? tplTheme : undefined;

Check warning on line 135 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 135 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 135 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 135 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
} catch (e) {
console.info(`Error while parsing Metric.template\n${(e as Error).message || e}`);

Check warning on line 137 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 137 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 137 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
}
if (template) {
layout.template = template;

Check warning on line 140 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 141 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 layout
}, [
props.template,
props.template_Dark_,
props.template_Light_,
theme.palette.mode,
baseLayout
])

return (
<Box data-testid={props.testId} className={className}>
<Tooltip title={hover || ""}>
<Suspense fallback={<Skeleton key="skeleton" sx={skelStyle}/>}>
<Plot
data={data as Data[]}
layout={baseLayout}
layout={layout}
style={style}
useResizeHandler
/>
Expand All @@ -133,3 +166,63 @@
}

export default Metric;

const darkTemplate = {
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
layout: {
colorscale: {
diverging: [
[0, "#8e0152"],
[0.1, "#c51b7d"],
[0.2, "#de77ae"],
[0.3, "#f1b6da"],
[0.4, "#fde0ef"],
[0.5, "#f7f7f7"],
[0.6, "#e6f5d0"],
[0.7, "#b8e186"],
[0.8, "#7fbc41"],
[0.9, "#4d9221"],
[1, "#276419"],
],
sequential: [
[0.0, "#0d0887"],
[0.1111111111111111, "#46039f"],
[0.2222222222222222, "#7201a8"],
[0.3333333333333333, "#9c179e"],
[0.4444444444444444, "#bd3786"],
[0.5555555555555556, "#d8576b"],
[0.6666666666666666, "#ed7953"],
[0.7777777777777778, "#fb9f3a"],
[0.8888888888888888, "#fdca26"],
[1.0, "#f0f921"],
],
sequentialminus: [
[0.0, "#0d0887"],
[0.1111111111111111, "#46039f"],
[0.2222222222222222, "#7201a8"],
[0.3333333333333333, "#9c179e"],
[0.4444444444444444, "#bd3786"],
[0.5555555555555556, "#d8576b"],
[0.6666666666666666, "#ed7953"],
[0.7777777777777778, "#fb9f3a"],
[0.8888888888888888, "#fdca26"],
[1.0, "#f0f921"],
],
},
colorway: [
"#636efa",
"#EF553B",
"#00cc96",
"#ab63fa",
"#FFA15A",
"#19d3f3",
"#FF6692",
"#B6E880",
"#FF97FF",
"#FECB52",
],
font: {
color: "#f2f5fa",
},
paper_bgcolor: "rgb(31,47,68)",
},
};
3 changes: 3 additions & 0 deletions taipy/gui/_renderers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ class _Factory:
("show_value", PropertyType.boolean, True),
("format", PropertyType.string),
("delta_format", PropertyType.string),
("template", PropertyType.dict),
("template[dark]", PropertyType.dict),
("template[light]", PropertyType.dict),
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
]
),
"navbar": lambda gui, control_type, attrs: _Builder(
Expand Down
15 changes: 15 additions & 0 deletions taipy/gui/viselements.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,21 @@
"type": "str|number",
"default_value": "None",
"doc": "The height, in CSS units, of the metric."
},
{
"name": "template",
"type": "dict",
"doc": "The Plotly layout <a href=\"https://plotly.com/javascript/layout-template/\">template</a>."
},
{
"name": "template[dark]",
"type": "dict",
"doc": "The Plotly layout <a href=\"https://plotly.com/javascript/layout-template/\">template</a> applied over the base template when theme is dark."
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
},
{
"name": "template[light]",
"type": "dict",
"doc": "The Plotly layout <a href=\"https://plotly.com/javascript/layout-template/\">template</a> applied over the base template when theme is not dark."
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Expand Down
Loading