-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-matrix): add matrix selection stats
- Loading branch information
Showing
5 changed files
with
250 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
webapp/src/components/common/Matrix/components/MatrixStats/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import { Box, Paper, Typography, Divider } from "@mui/material"; | ||
import { formatGridNumber } from "../../shared/utils"; | ||
|
||
interface MatrixStatsProps { | ||
stats: { | ||
count: number; | ||
sum: number; | ||
average: number; | ||
min: number; | ||
max: number; | ||
} | null; | ||
} | ||
|
||
function MatrixStats({ stats }: MatrixStatsProps) { | ||
if (!stats) { | ||
return null; | ||
} | ||
|
||
const statItems = [ | ||
{ label: "Nb", value: stats.count }, | ||
{ label: "Total", value: formatGridNumber({ value: stats.sum }) }, | ||
{ | ||
label: "Avg", | ||
value: formatGridNumber({ value: stats.average, maxDecimals: 2 }), | ||
}, | ||
{ label: "Min", value: formatGridNumber({ value: stats.min }) }, | ||
{ label: "Max", value: formatGridNumber({ value: stats.max }) }, | ||
]; | ||
|
||
return ( | ||
<Paper | ||
sx={{ | ||
display: "flex", | ||
flex: 1, | ||
alignItems: "center", | ||
justifyContent: "flex-end", | ||
width: 1, | ||
p: 1, | ||
maxHeight: 30, | ||
}} | ||
> | ||
{statItems.map((item, index) => ( | ||
<Box | ||
key={item.label} | ||
sx={{ | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "space-between", | ||
}} | ||
> | ||
<Typography | ||
sx={{ | ||
display: "flex", | ||
p: 0.5, | ||
alignItems: "center", | ||
color: "lightgray", | ||
fontSize: 10, | ||
fontWeight: 600, | ||
textTransform: "uppercase", | ||
letterSpacing: "0.1em", | ||
}} | ||
> | ||
{item.label} | ||
</Typography> | ||
<Typography | ||
sx={{ | ||
display: "flex", | ||
alignItems: "center", | ||
fontSize: 15, | ||
}} | ||
> | ||
{item.value} | ||
</Typography> | ||
{index < statItems.length - 1 && ( | ||
<Divider orientation="vertical" flexItem sx={{ mx: 1 }} /> | ||
)} | ||
</Box> | ||
))} | ||
</Paper> | ||
); | ||
} | ||
|
||
export default MatrixStats; |
99 changes: 99 additions & 0 deletions
99
webapp/src/components/common/Matrix/hooks/useSelectionStats/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import type { GridSelection, Item } from "@glideapps/glide-data-grid"; | ||
import { useState } from "react"; | ||
import { useUpdateEffect } from "react-use"; | ||
|
||
interface SelectionStats { | ||
sum: number; | ||
average: number; | ||
min: number; | ||
max: number; | ||
count: number; | ||
} | ||
|
||
interface UseSelectionStatsProps { | ||
data: number[][]; | ||
selection: GridSelection; | ||
gridToData: (coordinates: Item) => Item | null; | ||
} | ||
|
||
export function useSelectionStats({ data, selection, gridToData }: UseSelectionStatsProps) { | ||
const [stats, setStats] = useState<SelectionStats | null>(null); | ||
|
||
useUpdateEffect(() => { | ||
let sum = 0; | ||
let min = Infinity; | ||
let max = -Infinity; | ||
let count = 0; | ||
const numRows = data.length; | ||
const numCols = data[0]?.length ?? 0; | ||
|
||
const processValue = (value: number) => { | ||
sum += value; | ||
min = Math.min(min, value); | ||
max = Math.max(max, value); | ||
count++; | ||
}; | ||
|
||
if (selection.current?.range) { | ||
const { x, y, width, height } = selection.current.range; | ||
|
||
for (let col = x; col < x + width; ++col) { | ||
for (let row = y; row < y + height; ++row) { | ||
const coordinates = gridToData([col, row]); | ||
|
||
if (coordinates) { | ||
const [dataCol, dataRow] = coordinates; | ||
const value = data[dataRow]?.[dataCol]; | ||
|
||
if (value !== undefined) { | ||
processValue(value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const row of selection.rows) { | ||
for (let col = 0; col < numCols; ++col) { | ||
const value = data[row]?.[col]; | ||
|
||
if (value !== undefined) { | ||
processValue(value); | ||
} | ||
} | ||
} | ||
|
||
for (const col of selection.columns) { | ||
for (let row = 0; row < numRows; ++row) { | ||
const coordinates = gridToData([col, row]); | ||
|
||
if (coordinates) { | ||
const [dataCol, dataRow] = coordinates; | ||
const value = data[dataRow]?.[dataCol]; | ||
|
||
if (value !== undefined) { | ||
processValue(value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
setStats(count ? { sum, min, max, count, average: sum / count } : null); | ||
}, [data, selection]); | ||
|
||
return stats; | ||
} |