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

Metric format converter #1321

Merged
merged 23 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6c12783
Adding custom converter to convert sprintf-js to d3format for plotly
namnguyen20999 May 26, 2024
9b4b1a8
create a re-usable file for format converter
namnguyen20999 May 28, 2024
2a7e04b
adding unit test
namnguyen20999 May 29, 2024
7643c5a
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 29, 2024
2a9876b
fix test case name
namnguyen20999 May 29, 2024
cdcd954
Merge branch '1297-value-format' of github.com:Avaiga/taipy into 1297…
namnguyen20999 May 29, 2024
95c19d1
refactoring the format & delta_format
namnguyen20999 May 29, 2024
d03df5d
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 29, 2024
f762e9d
adding edge test case
namnguyen20999 May 30, 2024
32b0604
Merge branch '1297-value-format' of github.com:Avaiga/taipy into 1297…
namnguyen20999 May 30, 2024
68ca3e2
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 30, 2024
9fe190a
Merge branch 'develop' into 1297-value-format
namnguyen20999 May 31, 2024
a9d3dd8
refactor code base on reviews
namnguyen20999 Jun 2, 2024
aaa2323
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 2, 2024
6bfe97d
resolve comments
namnguyen20999 Jun 3, 2024
762cae3
resolve comments
namnguyen20999 Jun 3, 2024
c2798cc
Delete frontend/taipy-gui/public/stylekit/controls/metric.css
namnguyen20999 Jun 3, 2024
181c0be
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
3693ce6
update to camelCase
namnguyen20999 Jun 4, 2024
678fa7f
update to camelCase
namnguyen20999 Jun 4, 2024
cdc0834
refactor code base on comments
namnguyen20999 Jun 4, 2024
32ade6d
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
4436b1a
Merge branch 'develop' into 1297-value-format
namnguyen20999 Jun 4, 2024
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
87 changes: 45 additions & 42 deletions frontend/taipy-gui/src/components/Taipy/Metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,11 @@
* specific language governing permissions and limitations under the License.
*/

import React, {
CSSProperties,
lazy,
Suspense,
useMemo
} from 'react';
import React, {CSSProperties, lazy, Suspense, useMemo} from 'react';
import {Data} from "plotly.js";
import {
useClassNames,
useDynamicJsonProperty,
useDynamicProperty
} from "../../utils/hooks";
import {
TaipyBaseProps,
TaipyHoverProps
} from "./utils";
import {useClassNames, useDynamicJsonProperty, useDynamicProperty} from "../../utils/hooks";
import {extractPrefix, extractSuffix, extractFormatSpecifier} from "../../utils/formatConversion";
import {TaipyBaseProps, TaipyHoverProps} from "./utils";
import Box from "@mui/material/Box";
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
import Skeleton from "@mui/material/Skeleton";

Expand All @@ -50,6 +39,8 @@ interface MetricProps extends TaipyBaseProps, TaipyHoverProps {
width?: string | number;
height?: string | number;
showValue?: boolean;
format?: string;
formatDelta?: string;
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
}

const emptyLayout = {} as Record<string, Record<string, unknown>>;
Expand All @@ -68,37 +59,49 @@ const Metric = (props: MetricProps) => {
const baseLayout = useDynamicJsonProperty(props.layout, props.defaultLayout || "", emptyLayout);
const baseStyle = useDynamicJsonProperty(props.style, props.defaultStyle || "", defaultStyle);
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved

const data = useMemo(() => ([
{
domain: {x: [0, 1], y: [0, 1]},
value: value,
type: "indicator",
mode: "gauge" + (showValue ? "+number" : "") + (delta !== undefined ? "+delta" : ""),
delta: {
reference: typeof value === 'number' && typeof delta === 'number' ? value - delta : undefined,
},
gauge: {
axis: {
range: [
typeof props.min === 'number' ? props.min : 0,
typeof props.max === 'number' ? props.max : 100
]
const data = useMemo(() => {
return [
{
domain: {x: [0, 1], y: [0, 1]},
value: value,
type: "indicator",
mode: "gauge" + (showValue ? "+number" : "") + (delta !== undefined ? "+delta" : ""),
number: {
prefix: extractPrefix(props.format),
suffix: extractSuffix(props.format),
valueformat: extractFormatSpecifier(props.format),
},
shape: props.type === "linear" ? "bullet" : "angular",
threshold: {
line: {color: "red", width: 4},
thickness: 0.75,
value: threshold
}
},
}
]), [
value,
showValue,
delta,
delta: {
reference: typeof value === 'number' && typeof delta === 'number' ? value - delta : undefined,
prefix: extractPrefix(props.formatDelta),
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
suffix: extractSuffix(props.formatDelta),
valueformat: extractFormatSpecifier(props.formatDelta)
},
gauge: {
axis: {
range: [
props.min ?? 0,
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
props.max ?? 100
]
},
shape: props.type === "linear" ? "bullet" : "angular",
threshold: {
line: {color: "red", width: 4},
thickness: 0.75,
value: threshold
}
},
}
];
}, [
props.format,
props.formatDelta,
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
props.min,
props.max,
props.type,
value,
showValue,
delta,
threshold
]);

Expand Down
119 changes: 119 additions & 0 deletions frontend/taipy-gui/src/utils/formatConversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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.
*/

const FORMAT_REPLACE_REGEX = /%([0-9]*)([.][0-9]+)?([bdieufgosxX])/g;

/**
* Converts a sprintf format string to a D3 format string.
*
* This function takes a sprintf format string as input and returns a D3 format string.
* The conversion is done by replacing each sprintf format specifier with the corresponding D3 format specifier.
* If the input format string is undefined, the function returns undefined.
* If an error occurs during the conversion, the function logs the error to the console and returns undefined.
*
* @param format - The sprintf format string to convert.
* @returns The converted D3 format string, or undefined if the input is undefined or if an error occurs.
*/
const sprintfToD3Converter = (format: string) => {
try {
return format?.replace(FORMAT_REPLACE_REGEX, (match, width, precision, type) => {

Check warning on line 29 in frontend/taipy-gui/src/utils/formatConversion.ts

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 29 in frontend/taipy-gui/src/utils/formatConversion.ts

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 29 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
switch (type) {
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
case "b":
return "b";

Check warning on line 32 in frontend/taipy-gui/src/utils/formatConversion.ts

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 32 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "d":
return "d";

Check warning on line 34 in frontend/taipy-gui/src/utils/formatConversion.ts

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 34 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "i":
return "d";

Check warning on line 36 in frontend/taipy-gui/src/utils/formatConversion.ts

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 36 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "e":
return "e";

Check warning on line 38 in frontend/taipy-gui/src/utils/formatConversion.ts

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 38 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "f":
return "." + (precision?.slice(1) ?? "2") + "f";

Check warning on line 40 in frontend/taipy-gui/src/utils/formatConversion.ts

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 40 in frontend/taipy-gui/src/utils/formatConversion.ts

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 40 in frontend/taipy-gui/src/utils/formatConversion.ts

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 40 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "g":
return "." + (precision?.slice(1) ?? "2") + "g";

Check warning on line 42 in frontend/taipy-gui/src/utils/formatConversion.ts

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 42 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "o":
return "o";

Check warning on line 44 in frontend/taipy-gui/src/utils/formatConversion.ts

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 44 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "s":
return "";

Check warning on line 46 in frontend/taipy-gui/src/utils/formatConversion.ts

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 46 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "x":
return "x";

Check warning on line 48 in frontend/taipy-gui/src/utils/formatConversion.ts

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 48 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
case "X":
return "X";

Check warning on line 50 in frontend/taipy-gui/src/utils/formatConversion.ts

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 50 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
default:
return "";

Check warning on line 52 in frontend/taipy-gui/src/utils/formatConversion.ts

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 52 in frontend/taipy-gui/src/utils/formatConversion.ts

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 53 in frontend/taipy-gui/src/utils/formatConversion.ts

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 54 in frontend/taipy-gui/src/utils/formatConversion.ts

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 54 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🌿 Branch is not covered

Warning! Not covered branch
} catch (error) {
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
console.error(`Failed to convert format "${format}" to D3 format: ${error}`);

Check warning on line 56 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 57 in frontend/taipy-gui/src/utils/formatConversion.ts

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 58 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Extracts the prefix from the format string.
* @param format - The format string.
* @returns The extracted prefix, or undefined if the format is undefined.
*/
export const extractPrefix = (format: string | undefined): string | undefined => {
if (format === undefined) {
return undefined;
}

try {
const PREFIX_MATCH_REGEX: RegExp = /.*?(?=%)/;

Check warning on line 72 in frontend/taipy-gui/src/utils/formatConversion.ts

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
return format.match(PREFIX_MATCH_REGEX)?.[0] ?? "";

Check warning on line 73 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
} catch (error) {
console.error(`Failed to extract prefix from format "${format}": ${error}`);

Check warning on line 75 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 76 in frontend/taipy-gui/src/utils/formatConversion.ts

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 77 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Extracts the suffix from the format string.
* @param format - The format string.
* @returns The extracted suffix, or undefined if the format is undefined.
*/
export const extractSuffix = (format: string | undefined): string | undefined => {
if (format === undefined) {
return undefined;
}

try {
const SURFIX_MATCH_REGEX: RegExp = /(?<=[bdieufgosxX])./;

Check warning on line 91 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return format.match(SURFIX_MATCH_REGEX)?.[0] ?? "";

Check warning on line 92 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
} catch (error) {
console.error(`Failed to extract suffix from format "${format}": ${error}`);

Check warning on line 94 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 95 in frontend/taipy-gui/src/utils/formatConversion.ts

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 96 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}

/**
* Extracts the format specifier from the input string.
* The input string is expected to be in sprintf format.
* The function returns the format specifier (one of 'b', 'd', 'i', 'e', 'f', 'g', 'o', 's', 'x', 'X') if it exists, or undefined otherwise.
* @param input - The input string in sprintf format.
* @returns The extracted format specifier, or undefined if no specifier is found or if the input is undefined.
*/
export const extractFormatSpecifier = (input: string | undefined): string | undefined => {
if (input === undefined) {
return undefined;
}

try {
const regex: RegExp = /.*%?([bdieufgosxX]).*/g;

Check warning on line 112 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
const convertedInput = sprintfToD3Converter(input);

Check warning on line 113 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return convertedInput ? convertedInput.replace(regex, '$1') : undefined;

Check warning on line 114 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
} catch (error) {
console.error(`Failed to extract format specifier from input "${input}": ${error}`);

Check warning on line 116 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 117 in frontend/taipy-gui/src/utils/formatConversion.ts

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 118 in frontend/taipy-gui/src/utils/formatConversion.ts

View workflow job for this annotation

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

🧾 Statement is not covered

Warning! Not covered statement
}
2 changes: 2 additions & 0 deletions taipy/gui/_renderers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ class _Factory:
("width", PropertyType.string_or_number),
("height", PropertyType.string_or_number),
("show_value", PropertyType.boolean, True),
("format", PropertyType.string),
("format_delta", PropertyType.string),
namnguyen20999 marked this conversation as resolved.
Show resolved Hide resolved
]
),
"navbar": lambda gui, control_type, attrs: _Builder(
Expand Down
Loading