Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import React, {
memo,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { Chart as ChartJS } from "chart.js";
import {
ChoroplethController,
GeoFeature,
ProjectionScale,
ColorScale,
} from "chartjs-chart-geo";
import * as topojson from "topojson-client";
import { Box } from "@mui/material";
import { useQuery } from "react-query"; // Import useQuery
import { StatesCustomerTypes } from "@pages/Dashboard/hooks/useGetMerchantStats";
import MapsLoading from "@assets/rebrandIcons/MapsLoading";
import { isEmpty } from "lodash";
import { getPeriodText } from "@utils/date.helpers";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
ChartJS.register(ChoroplethController, GeoFeature, ProjectionScale, ColorScale);
const fetchMapData = async () => {
const response = await fetch("https://unpkg.com/us-atlas/states-10m.json");
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
};
const ChoroplethMap = ({
isLoading,
data,
start_date,
end_date,
}: {
isLoading?: boolean;
data?: StatesCustomerTypes[];
start_date?: string | null;
end_date?: string | null;
}) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const isNostate = useMemo(() => {
if (!Array.isArray(data)) {
return false; // or whatever default value you want
}
const allowedStates = ["", "State"]; // Add any other states you want to allow here
for (let i = 0; i < data.length; i++) {
if (!allowedStates.includes(data[i].state)) {
return false;
}
}
return true;
}, [data]);
const { data: us, status } = useQuery("mapData", fetchMapData);
const generateData = useCallback(
(states: any[]) => {
const objectData = states.map((d: any) => {
const matchingStateObject = data?.find(
(stateObj) => d.properties.name === stateObj?.state,
);
if (matchingStateObject) {
return {
feature: d,
value: matchingStateObject?.totalCustomers,
};
} else {
return {
feature: d,
value: 0,
};
}
});
return objectData;
},
[data, isLoading],
);
useEffect(() => {
let chart: ChartJS | null = null;
if (status === "success" && us && canvasRef.current && !isLoading && data) {
const nation = (topojson.feature(us, us.objects.nation) as any)
.features[0];
const states = (topojson.feature(us, us.objects.states) as any).features;
chart = new ChartJS(canvasRef.current, {
type: "choropleth",
data: {
labels: states.map((d: any) => d.properties.name),
datasets: [
{
label: "States",
outline: nation,
data: generateData(states),
},
],
},
options: {
plugins: {
legend: {
display: false,
},
tooltip: {
padding: {
left: 12,
right: 12,
top: 9,
bottom: 9,
},
displayColors: false,
caretSize: 0,
cornerRadius: 6,
titleColor(ctx, options) {
return "#B8B8B8";
},
callbacks: {
title: () =>
`Customers -Last ${getPeriodText(start_date, end_date)}`,
labelTextColor(tooltipItem) {
return "#FFFFFF";
},
},
titleFont: {
size: 12,
weight: "normal",
},
bodyFont: {
size: 12,
weight: "normal",
},
},
},
scales: {
projection: {
axis: "x",
projection: "albersUsa",
},
color: {
display: false,
axis: "x",
},
},
},
});
}
return () => {
if (chart) {
chart.destroy();
}
};
}, [status, us, isLoading, data, generateData, start_date, end_date]);
return (
<>
{isLoading || isEmpty(data) || isNostate ? (
<MapsLoading />
) : (
<FadeUpWrapper delay={400}>
<Box
sx={{
overflowX: "auto",
"&::-webkit-scrollbar": {
display: "none",
},
}}
>
<Box
borderRadius="12px"
bgcolor="#F8F8F6"
width="100%"
height="100%"
minWidth="600px"
>
<canvas id="canvas" ref={canvasRef} />
</Box>
</Box>
</FadeUpWrapper>
)}
</>
);
};
export default memo(ChoroplethMap);
|