Skip to content

Commit

Permalink
fix: charts and get delete endpoint init
Browse files Browse the repository at this point in the history
  • Loading branch information
jacovinus committed Sep 26, 2023
1 parent 94f7aee commit e03407a
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@mui/icons-material": "^5.11.16",
"@mui/material": "^5.13.6",
"@mui/styles": "^5.13.2",
"@mui/x-charts": "6.0.0-alpha.12",
"@szhsin/react-menu": "^4.0.0",
"@tanstack/match-sorter-utils": "^8.8.4",
"@tanstack/react-table": "^8.9.2",
Expand Down
112 changes: 112 additions & 0 deletions packages/main/plugins/Cardinality/Chart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React, { useMemo, forwardRef, useEffect } from "react";
import { BarChart } from "@mui/x-charts/BarChart";

import { SeriesRowProps } from "./SeriesRow";
import { useSelector } from "react-redux";
import { createTheme, ThemeProvider } from "@mui/material";
type ChartProps = {
rows: SeriesRowProps[];
hasDiff?: boolean;
chart?: any;
isChartGroup?: boolean;
chartData?: any;
};

const CardinalityChart = forwardRef(
({ chart = "", isChartGroup = false, chartData }: ChartProps, ref: any) => {
const [width, setWidth] = React.useState(
ref?.current?.offsetWidth || 0
);

const theme = useSelector((store: any) => store.theme);
const chartTheme = useMemo(() => {
return createTheme({ palette: { mode: theme } });
}, [theme]);

useEffect(() => {
if (ref?.current?.offsetWidth) {
setWidth(ref?.current?.offsetWidth - 8);
}
}, [ref?.current]);

const chartRenderer = (isChartGroup, chartData) => {
if (chartData && isChartGroup && chart !== "") {
console.log(chartData?.valueTypesMapped[chart]?.names);
return (
<BarChart
colors={["#babc00", "#ff5555"]}
xAxis={[
{
id: "barCategories",
dataKey: "xAxisData",
data: [
...chartData?.valueTypesMapped[chart]
?.names,
],

scaleType: "band",
},
]}
series={[
{
data: [
...chartData?.valueTypesMapped[chart]
?.values,
],
label: "current",
},

{
data: [
...chartData?.valueTypesMapped[chart]?.diff,
],
label: "previous",
},
]}
margin={{ bottom: 60, left: 60 }}
width={width}
height={300}
/>
);
} else {
return (
<BarChart
colors={["#babc00", "#ff5555"]}
xAxis={[
{
id: "barCategories",
dataKey: "xAxisData",
data: [...chartData?.xAxisData],
scaleType: "band",
},
]}
series={[
{
data: [...chartData?.valueData],
label: "current",
},
{
data: [...chartData?.diffData],
label: "previous",
},
]}
margin={{ bottom: 60, left: 60 }}
width={width}
height={300}
/>
);
}
};

// console.log(chartData, "chartData");
return (
<ThemeProvider theme={chartTheme}>
{width > 0 &&
chartData &&
chartRenderer(isChartGroup, chartData)}
</ThemeProvider>
);
}
);

export default CardinalityChart;
63 changes: 63 additions & 0 deletions packages/main/plugins/Cardinality/ChartContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useRef } from "react";
import { SeriesRowProps } from "./SeriesRow";
import CardinalityChart from "./Chart";
import { css, cx } from "@emotion/css";
import { QrynTheme } from "@ui/theme/types";
import styled from "@emotion/styled";

export const CardinalityChartsContainer = styled.div`
display: flex;
flex: 1;
flex-wrap: wrap;
gap: 4px;
`;

export const CardinalityChartContainerStyles = (theme: QrynTheme) => css`
display: flex;
flex: 1;
flex-direction: column;
min-width: 33%;
border: 1px solid ${theme.neutral};
.label {
display: flex;
padding: 6px 8px;
text-align: center;
}
`;

export type ChartContainerProps = {
rows: SeriesRowProps[];
theme: QrynTheme;
hasDiff?: boolean;
chart?: any;
chartData?: any;
};

const ChartContainer: React.FC<ChartContainerProps> = ({
chart = "",
rows,
theme,
hasDiff = true,
chartData,
}) => {
const containerRef = useRef<any>(null);

return (
<div
className={cx(CardinalityChartContainerStyles(theme))}
ref={containerRef}
>
{chart !== "" && <div className="label">{chart}</div>}
<CardinalityChart
chartData={chartData}
isChartGroup={chart !== ""}
chart={chart}
rows={rows}
hasDiff={hasDiff}
ref={containerRef}
/>
</div>
);
};

export default ChartContainer;
121 changes: 121 additions & 0 deletions packages/main/plugins/Cardinality/ChartsGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import React, { useMemo } from "react";
import { type SeriesRowProps } from "./SeriesRow";
import { type QrynTheme } from "@ui/theme/types";
import { CardinalityChartsContainer } from "./ChartContainer";
import ChartContainer from "./ChartContainer";

type ChartsGroupProps = {
rows: SeriesRowProps[];
theme: QrynTheme;
};

export const formatValueTypesMapped = (
valueTypesMapped: any,
xAxisData,
valueData,
diffData
) => {
return valueTypesMapped.reduce((acc: any, curr: any, i: number) => {
let currName: any = xAxisData[i]?.split("=")[0];
if (acc[currName] === undefined) {
acc[currName] = {};
acc[currName].names = [];
acc[currName].values = [];
acc[currName].diff = [];
}
acc[currName].names.push(curr);
acc[currName].values.push(valueData[i]);
acc[currName].diff.push(diffData[i]);
return acc;
}, {});
};

export const formatChartProps = (rows: SeriesRowProps[]) => {
const xAxisData = rows?.map((row) => row.name);
const valueData = rows?.map((row) => row.value);
const diffData = rows?.map((row) =>
row.diff < 0 ? row.diff * -1 : row.diff
);
const labelTypes = rows?.map((row) => row.name.split("=")[0]);
const valueTypes: any = rows?.map((row) => row.name.split("=")[1]);

return {
xAxisData,
valueData,
diffData,
labelTypes,
valueTypes,
valueTypesMapped: {},
valueTypesKeys: [],
};
};

export const useChartData = (rows: SeriesRowProps[]) => {
return useMemo(() => {
if (rows && rows?.length > 0) {
const { xAxisData, valueData, diffData, labelTypes, valueTypes } =
formatChartProps(rows);

let valueTypesMapped = {};

if (valueTypes?.[0] !== undefined) {
valueTypesMapped = formatValueTypesMapped(
valueTypes,
xAxisData,
valueData,
diffData
);
}

let valueTypesKeys = Object.keys(valueTypesMapped);

return {
xAxisData,
valueData,
diffData,
labelTypes,
valueTypes,
valueTypesMapped,
valueTypesKeys,
};
}

return {
xAxisData: [],
valueData: [],
diffData: [],
labelTypes: [],
valueTypes: [],
valueTypesMapped: {},
valueTypesKeys: [],
};
}, [rows]);
};

export const ChartsGroup: React.FC<ChartsGroupProps> = ({ rows, theme }) => {
const chartData = useChartData(rows);

return (
<CardinalityChartsContainer>
{chartData?.valueTypes[0] !== undefined ? (
chartData?.valueTypesKeys?.map((chart, id) => (
<ChartContainer
key={id}
theme={theme}
rows={rows}
chart={chart}
hasDiff={false}
chartData={chartData}
/>
))
) : (
<ChartContainer
theme={theme}
rows={rows}
chartData={chartData}
hasDiff={true}
/>
)}
</CardinalityChartsContainer>
);
};
5 changes: 5 additions & 0 deletions packages/main/plugins/Cardinality/Configurator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const Configurator: React.FC<ConfiguratorProps> = ({
handleCardinalityRequest();
};

// this feature is not implemented yet
// useEffect(() => {
// handleGetDeletedFingerprints();
// }, []);

return (
<div className={cx(ConfigContainerStyles(theme))}>
<div className="form-row">
Expand Down
14 changes: 11 additions & 3 deletions packages/main/plugins/Cardinality/SeriesGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useRef } from "react";
import SeriesHeader, { type SeriesHeaderProps } from "./SeriesHeader";
import SeriesRow, { type SeriesRowProps } from "./SeriesRow";
import SeriesRowHeaders from "./SeriesRowHeaders";
Expand All @@ -12,6 +12,8 @@ export type SeriesGroupProps = {
sectionHeaderName: string;
} & SeriesHeaderProps;

import { ChartsGroup } from "./ChartsGroup";

// This components is used to display a group of series
export const SeriesGroup: React.FC<SeriesGroupProps> = ({
title,
Expand All @@ -26,8 +28,10 @@ export const SeriesGroup: React.FC<SeriesGroupProps> = ({

const { sortedRows, handleSort } = useSortedColumns(rows);

const containerRef: any = useRef(null);

return (
<div className={seriesGroupContainer}>
<div className={seriesGroupContainer} ref={containerRef}>
<SeriesHeader title={title} />
<div className="c-table">
{rows && (
Expand All @@ -44,11 +48,15 @@ export const SeriesGroup: React.FC<SeriesGroupProps> = ({
<SeriesRow
key={key}
theme={seriesGroupStyles}
hasShare={sectionHeaderName !== "labelValueCountByLabelName"}
hasShare={
sectionHeaderName !==
"labelValueCountByLabelName"
}
{...row}
/>
))}
</div>
<ChartsGroup rows={sortedRows} theme={theme} />
</div>
);
};
9 changes: 5 additions & 4 deletions packages/main/plugins/Cardinality/SeriesGroupStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const SeriesGroupContainer = (theme: Partial<QrynTheme>) => css`
flex-direction: column;
padding: 8px 4px;
background: ${theme.shadow};
gap: 4px;
.c-header {
font-size: 14px;
padding: 8px 6px;
Expand All @@ -41,11 +42,11 @@ export const SeriesGroupContainer = (theme: Partial<QrynTheme>) => css`
}
`;


export const useSeriesGroupStyles = (theme: QrynTheme) => {
return {
seriesGroupContainer: cx(SeriesGroupContainer(getSeriesGroupTheme(theme) )),
seriesGroupContainer: cx(
SeriesGroupContainer(getSeriesGroupTheme(theme))
),
seriesGroupStyles: getSeriesGroupTheme(theme),
};

}
};
Loading

0 comments on commit e03407a

Please sign in to comment.