Add switch for percentages in dash. widget #641

This commit is contained in:
Manuel
2023-01-31 20:12:03 +01:00
parent b459b0fe89
commit a533aad7b9
3 changed files with 26 additions and 4 deletions

View File

@@ -18,6 +18,9 @@
}, },
"url": { "url": {
"label": "Dash. URL" "label": "Dash. URL"
},
"usePercentages": {
"label": "Display percentages"
} }
} }
}, },

View File

@@ -5,9 +5,15 @@ interface DashDotGraphProps {
graph: GraphType; graph: GraphType;
isCompact: boolean; isCompact: boolean;
dashDotUrl: string; dashDotUrl: string;
usePercentages: boolean;
} }
export const DashDotGraph = ({ graph, isCompact, dashDotUrl }: DashDotGraphProps) => { export const DashDotGraph = ({
graph,
isCompact,
dashDotUrl,
usePercentages,
}: DashDotGraphProps) => {
const { classes } = useStyles(); const { classes } = useStyles();
return ( return (
<Stack <Stack
@@ -25,13 +31,18 @@ export const DashDotGraph = ({ graph, isCompact, dashDotUrl }: DashDotGraphProps
className={classes.iframe} className={classes.iframe}
key={graph.name} key={graph.name}
title={graph.name} title={graph.name}
src={useIframeSrc(dashDotUrl, graph, isCompact)} src={useIframeSrc(dashDotUrl, graph, isCompact, usePercentages)}
/> />
</Stack> </Stack>
); );
}; };
const useIframeSrc = (dashDotUrl: string, graph: GraphType, isCompact: boolean) => { const useIframeSrc = (
dashDotUrl: string,
graph: GraphType,
isCompact: boolean,
usePercentages: boolean
) => {
const { colorScheme, colors, radius } = useMantineTheme(); const { colorScheme, colors, radius } = useMantineTheme();
const surface = (colorScheme === 'dark' ? colors.dark[7] : colors.gray[0]).substring(1); // removes # from hex value const surface = (colorScheme === 'dark' ? colors.dark[7] : colors.gray[0]).substring(1); // removes # from hex value
@@ -45,7 +56,8 @@ const useIframeSrc = (dashDotUrl: string, graph: GraphType, isCompact: boolean)
`&surface=${surface}` + `&surface=${surface}` +
`&gap=${isCompact ? 10 : 5}` + `&gap=${isCompact ? 10 : 5}` +
`&innerRadius=${radius.lg}` + `&innerRadius=${radius.lg}` +
`&multiView=${graph.isMultiView}` `&multiView=${graph.isMultiView}` +
`&showPercentage=${usePercentages ? 'true' : 'false'}`
); );
}; };

View File

@@ -25,6 +25,10 @@ const definition = defineWidget({
type: 'switch', type: 'switch',
defaultValue: true, defaultValue: true,
}, },
usePercentages: {
type: 'switch',
defaultValue: false,
},
graphs: { graphs: {
type: 'multi-select', type: 'multi-select',
defaultValue: ['cpu', 'memory'], defaultValue: ['cpu', 'memory'],
@@ -88,6 +92,8 @@ function DashDotTile({ widget }: DashDotTileProps) {
const isCompactNetworkVisible = graphs?.some((g) => g.id === 'network' && isCompact); const isCompactNetworkVisible = graphs?.some((g) => g.id === 'network' && isCompact);
const usePercentages = widget?.properties.usePercentages ?? false;
const displayedGraphs = graphs?.filter( const displayedGraphs = graphs?.filter(
(g) => !isCompact || !['network', 'storage'].includes(g.id) (g) => !isCompact || !['network', 'storage'].includes(g.id)
); );
@@ -109,6 +115,7 @@ function DashDotTile({ widget }: DashDotTileProps) {
graph={graph} graph={graph}
dashDotUrl={dashDotUrl} dashDotUrl={dashDotUrl}
isCompact={isCompact} isCompact={isCompact}
usePercentages={usePercentages}
/> />
))} ))}
</Group> </Group>