2023-05-11 12:08:46 +00:00
|
|
|
// nivo
|
|
|
|
import { BarDatum } from "@nivo/bar";
|
|
|
|
// components
|
|
|
|
import { CustomTooltip } from "./custom-tooltip";
|
|
|
|
// ui
|
|
|
|
import { BarGraph } from "components/ui";
|
|
|
|
// helpers
|
|
|
|
import { findStringWithMostCharacters } from "helpers/array.helper";
|
2023-05-15 05:52:06 +00:00
|
|
|
import { generateBarColor } from "helpers/analytics.helper";
|
2023-05-11 12:08:46 +00:00
|
|
|
// types
|
|
|
|
import { IAnalyticsParams, IAnalyticsResponse } from "types";
|
|
|
|
// constants
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
analytics: IAnalyticsResponse;
|
|
|
|
barGraphData: {
|
|
|
|
data: BarDatum[];
|
|
|
|
xAxisKeys: string[];
|
|
|
|
};
|
|
|
|
params: IAnalyticsParams;
|
2023-05-16 05:11:37 +00:00
|
|
|
yAxisKey: "count" | "estimate";
|
2023-05-11 12:08:46 +00:00
|
|
|
fullScreen: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AnalyticsGraph: React.FC<Props> = ({
|
|
|
|
analytics,
|
|
|
|
barGraphData,
|
|
|
|
params,
|
|
|
|
yAxisKey,
|
|
|
|
fullScreen,
|
|
|
|
}) => {
|
|
|
|
const generateYAxisTickValues = () => {
|
|
|
|
if (!analytics) return [];
|
|
|
|
|
|
|
|
let data: number[] = [];
|
|
|
|
|
|
|
|
if (params.segment)
|
|
|
|
// find the total no of issues in each segment
|
|
|
|
data = Object.keys(analytics.distribution).map((segment) => {
|
|
|
|
let totalSegmentIssues = 0;
|
|
|
|
|
|
|
|
analytics.distribution[segment].map((s) => {
|
|
|
|
totalSegmentIssues += s[yAxisKey] as number;
|
|
|
|
});
|
|
|
|
|
|
|
|
return totalSegmentIssues;
|
|
|
|
});
|
|
|
|
else data = barGraphData.data.map((d) => d[yAxisKey] as number);
|
|
|
|
|
2023-05-15 05:52:06 +00:00
|
|
|
return data;
|
2023-05-11 12:08:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const longestXAxisLabel = findStringWithMostCharacters(barGraphData.data.map((d) => `${d.name}`));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BarGraph
|
|
|
|
data={barGraphData.data}
|
|
|
|
indexBy="name"
|
|
|
|
keys={barGraphData.xAxisKeys}
|
|
|
|
colors={(datum) =>
|
|
|
|
generateBarColor(
|
|
|
|
params.segment ? `${datum.id}` : `${datum.indexValue}`,
|
|
|
|
analytics,
|
|
|
|
params,
|
|
|
|
params.segment ? "segment" : "x_axis"
|
|
|
|
)
|
|
|
|
}
|
2023-05-15 05:52:06 +00:00
|
|
|
customYAxisTickValues={generateYAxisTickValues()}
|
2023-05-19 11:38:32 +00:00
|
|
|
tooltip={(datum) => <CustomTooltip datum={datum} analytics={analytics} params={params} />}
|
2023-05-11 12:08:46 +00:00
|
|
|
height={fullScreen ? "400px" : "300px"}
|
2023-05-16 09:41:40 +00:00
|
|
|
margin={{
|
|
|
|
right: 20,
|
|
|
|
bottom: params.x_axis === "assignees__email" ? 50 : longestXAxisLabel.length * 5 + 20,
|
|
|
|
}}
|
|
|
|
axisBottom={{
|
|
|
|
tickSize: 0,
|
|
|
|
tickPadding: 10,
|
|
|
|
tickRotation: barGraphData.data.length > 7 ? -45 : 0,
|
|
|
|
renderTick:
|
|
|
|
params.x_axis === "assignees__email"
|
|
|
|
? (datum) => {
|
|
|
|
const avatar = analytics.extras.assignee_details?.find(
|
2023-05-19 09:43:55 +00:00
|
|
|
(a) => a?.assignees__email === datum?.value
|
2023-05-16 09:41:40 +00:00
|
|
|
)?.assignees__avatar;
|
|
|
|
|
|
|
|
if (avatar && avatar !== "")
|
|
|
|
return (
|
|
|
|
<g transform={`translate(${datum.x},${datum.y})`}>
|
|
|
|
<image
|
|
|
|
x={-8}
|
|
|
|
y={10}
|
|
|
|
width={16}
|
|
|
|
height={16}
|
|
|
|
xlinkHref={avatar}
|
|
|
|
style={{ clipPath: "circle(50%)" }}
|
|
|
|
/>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
else
|
|
|
|
return (
|
|
|
|
<g transform={`translate(${datum.x},${datum.y})`}>
|
|
|
|
<circle cy={18} r={8} fill="#374151" />
|
|
|
|
<text x={0} y={21} textAnchor="middle" fontSize={9} fill="#ffffff">
|
|
|
|
{datum.value && datum.value !== "None"
|
|
|
|
? `${datum.value}`.toUpperCase()[0]
|
|
|
|
: "?"}
|
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
}}
|
2023-05-11 12:08:46 +00:00
|
|
|
theme={{
|
2023-05-16 05:11:37 +00:00
|
|
|
background: "rgb(var(--color-bg-base))",
|
2023-05-11 12:08:46 +00:00
|
|
|
axis: {},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|