Skip to content

An open source alternative to Tableau. Embeddable visual analytic

Added code for layout formatting in table (#286)

Vedant Jolly915781a02ede
diff --git a/packages/graphic-walker/src/components/pivotTable/index.tsx b/packages/graphic-walker/src/components/pivotTable/index.tsx
index c7025516..bb687665 100644
--- a/packages/graphic-walker/src/components/pivotTable/index.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/index.tsx
@@ -6,7 +6,7 @@ import { useAppRootContext } from '../../components/appRoot';
import LeftTree from './leftTree';
import TopTree from './topTree';
import { observer } from 'mobx-react-lite';
-import { DeepReadonly, DraggableFieldState, IDarkMode, IRow, IThemeKey, IViewField, IVisualConfigNew, IVisualLayout } from '../../interfaces';
+import { DeepReadonly, DraggableFieldState, IDarkMode, IRow, IThemeKey, IViewField, IVisualConfigNew, IVisualLayout, IVisualConfig } from '../../interfaces';
import { INestNode } from './inteface';
import { unstable_batchedUpdates } from 'react-dom';
import MetricTable from './metricTable';
@@ -35,7 +35,6 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
const [topTree, setTopTree] = useState<INestNode | null>(null);
const [metricTable, setMetricTable] = useState<any[][]>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
-
const vizStore = useVizStore();
const enableCollapse = !!vizStore;
const tableCollapsedHeaderMap = vizStore?.tableCollapsedHeaderMap ?? emptyMap;
@@ -249,7 +248,7 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
enableCollapse={enableCollapse}
/>
)}
- {metricTable && <MetricTable matrix={metricTable} meaInColumns={measInColumn} meaInRows={measInRow} />}
+ {metricTable && <MetricTable matrix={metricTable} meaInColumns={measInColumn} meaInRows={measInRow} numberFormat={layout.format.numberFormat || ''} />}
</table>
</div>
</div>
diff --git a/packages/graphic-walker/src/components/pivotTable/metricTable.tsx b/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
index 31e8de60..fb8c4d32 100644
--- a/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
@@ -1,22 +1,39 @@
import React from 'react';
-import { IField, IRow } from '../../interfaces';
+import { useMemo } from "react";
+import { IField, IRow, IVisualConfig } from '../../interfaces';
import { getMeaAggKey } from '../../utils';
+import { format } from "d3-format";
interface MetricTableProps {
matrix: any[][];
meaInRows: IField[];
meaInColumns: IField[];
+ numberFormat: string;
}
-function getCellData (cell: IRow, measure: IField) {
+function getCellData (cell: IRow, measure: IField, formatter: (value: unknown) => string) {
+
const meaKey = getMeaAggKey(measure.fid, measure.aggName);
if (cell[meaKey] === undefined) {
return '--';
}
- return cell[meaKey];
+ const formattedValue = formatter(cell[meaKey]);
+ return formattedValue;
}
+
const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
- const { matrix, meaInRows, meaInColumns } = props;
+ const { matrix, meaInRows, meaInColumns, numberFormat } = props;
+
+ const numberFormatter = useMemo<(value: unknown) => string>(() => {
+ const numberFormatter = numberFormat ? format(numberFormat) : (v: number) => v.toLocaleString();
+ return (value: unknown) => {
+ if (typeof value !== "number") {
+ return `${value}`;
+ }
+ return numberFormatter(value);
+ };
+ }, [numberFormat]);
+
return (
<tbody className="bg-white dark:bg-black text-gray-800 dark:text-gray-100 border-r border-b border-gray-300">
{matrix.map((row, rIndex) => {
@@ -33,7 +50,7 @@ const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
className="whitespace-nowrap p-2 text-xs"
key={`${rIndex}-${cIndex}-${rowMea.fid}-${rowMea.aggName}-${colMea.fid}-${colMea.aggName}`}
>
- {getCellData(cell, rowMea)} , {getCellData(cell, colMea)}
+ {getCellData(cell, rowMea, numberFormatter)} , {getCellData(cell, colMea, numberFormatter)}
</td>
));
}
@@ -42,7 +59,7 @@ const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
className="whitespace-nowrap p-2 text-xs"
key={`${rIndex}-${cIndex}-${rowMea.fid}-${rowMea.aggName}`}
>
- {getCellData(cell, rowMea)}
+ {getCellData(cell, rowMea, numberFormatter)}
</td>
);
})
@@ -62,7 +79,7 @@ const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
key={`${rIndex}-${cIndex}-${cmIndex}-${colMea.fid}-${colMea.aggName}`}
>
{
- getCellData(cell, colMea)
+ getCellData(cell, colMea, numberFormatter)
}
</td>
));
@@ -86,7 +103,7 @@ const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
className="whitespace-nowrap p-2 text-xs"
key={`${rIndex}-${cIndex}-${rmIndex}-${cmIndex}-${colMea.fid}-${colMea.aggName}`}
>
- { getCellData(cell, rowMea) } , { getCellData(cell, colMea) }
+ { getCellData(cell, rowMea, numberFormatter) } , { getCellData(cell, colMea, numberFormatter) }
</td>
))}
</td>
@@ -102,7 +119,7 @@ const MetricTable: React.FC<MetricTableProps> = React.memo((props) => {
</tbody>
);
}, function areEqual(prevProps, nextProps) {
- if (JSON.stringify(prevProps.matrix) === JSON.stringify(nextProps.matrix)) {
+ if (JSON.stringify(prevProps.matrix) === JSON.stringify(nextProps.matrix) && prevProps.numberFormat === nextProps.numberFormat) {
return true;
}