@@ -21,98 +21,112 @@ import { useMemo, useState } from "react";
2121import { Pie , PieChart } from "recharts" ;
2222import { EmptyChartState , LoadingChartState } from "./EmptyChartState" ;
2323
24- type ChartToShow = "unique " | "total " ;
24+ type ChartToShow = "totalConnections " | "uniqueWalletsConnected " ;
2525
2626type ChartData = {
2727 walletType : string ;
28- totalWallets : number ;
29- uniqueWallets : number ;
28+ value : number ;
3029 fill : string ;
3130} ;
3231
3332const chartLabelToShow : Record < ChartToShow , string > = {
34- unique : "Unique Wallets" ,
35- total : "Total Wallets" ,
33+ uniqueWalletsConnected : "Unique Wallets" ,
34+ totalConnections : "Total Wallets" ,
3635} ;
3736
3837export function WalletDistributionChartCard ( props : {
3938 walletStats : WalletStats [ ] ;
4039 isPending : boolean ;
4140} ) {
41+ // show these many top wallets as distinct, and combine the rest as "Others"
42+ const topWalletsToShow = 10 ;
4243 const { walletStats } = props ;
43- const [ chartToShow , setChartToShow ] = useState < ChartToShow > ( "total" ) ;
44- const chartToShowOptions : ChartToShow [ ] = [ "total" , "unique" ] ;
44+ const [ chartToShow , setChartToShow ] =
45+ useState < ChartToShow > ( "totalConnections" ) ;
46+ const chartToShowOptions : ChartToShow [ ] = [
47+ "totalConnections" ,
48+ "uniqueWalletsConnected" ,
49+ ] ;
4550
4651 const { chartConfig, chartData, totalConnections, uniqueConnections } =
4752 useMemo ( ( ) => {
48- const _chartConfig : ChartConfig = { } ;
49- const _chartDataMap : Map <
50- string ,
51- {
52- total : number ;
53- unique : number ;
54- }
55- > = new Map ( ) ;
53+ const _chartDataMap : Map < string , number > = new Map ( ) ;
54+ const walletTypeToValueMap : Map < string , number > = new Map ( ) ;
5655
5756 let _totalConnections = 0 ;
5857 let _uniqueConnections = 0 ;
5958
60- for ( const data of walletStats ) {
61- const chartData = _chartDataMap . get ( data . walletType ) ;
62-
63- _totalConnections += data . totalConnections ;
64- _uniqueConnections += data . uniqueWalletsConnected ;
65-
66- // if no data for current day - create new entry
67- if ( ! chartData ) {
68- _chartDataMap . set ( data . walletType , {
69- total : data . totalConnections ,
70- unique : data . uniqueWalletsConnected ,
71- } ) ;
72- } else {
73- _chartDataMap . set ( data . walletType , {
74- total : chartData . total + data . totalConnections ,
75- unique : chartData . unique + data . uniqueWalletsConnected ,
76- } ) ;
77- }
59+ for ( const stat of walletStats ) {
60+ const { walletType } = stat ;
61+ const chartData = _chartDataMap . get ( walletType ) ;
62+
63+ _totalConnections += stat . totalConnections ;
64+ _uniqueConnections += stat . uniqueWalletsConnected ;
65+ _chartDataMap . set ( walletType , ( chartData || 0 ) + stat [ chartToShow ] ) ;
66+ walletTypeToValueMap . set (
67+ walletType ,
68+ ( walletTypeToValueMap . get ( walletType ) || 0 ) + stat [ chartToShow ] ,
69+ ) ;
7870 }
7971
80- // create chart config for each wallet type and assign a unique color, start from 0hue to 360hue
81- const uniqueWalletTypes = Array . from (
82- new Set ( walletStats . map ( ( data ) => data . walletType ) ) ,
72+ const walletTypesSortedByValue = Array . from (
73+ walletTypeToValueMap . entries ( ) ,
74+ )
75+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
76+ . map ( ( w ) => w [ 0 ] ) ;
77+
78+ const walletTypesToShow = walletTypesSortedByValue . slice (
79+ 0 ,
80+ topWalletsToShow ,
8381 ) ;
8482
85- const hueIncrement = 360 / uniqueWalletTypes . length ;
83+ const walletTypesToTagAsOthers =
84+ walletTypesSortedByValue . slice ( topWalletsToShow ) ;
8685
87- for ( let i = 0 ; i < uniqueWalletTypes . length ; i ++ ) {
88- const walletType = uniqueWalletTypes [ i ] ;
86+ for ( const walletType of walletTypesToTagAsOthers ) {
87+ const val = _chartDataMap . get ( walletType ) ;
88+ if ( val ) {
89+ const othersVal = _chartDataMap . get ( "others" ) ;
90+ _chartDataMap . set ( "others" , othersVal ? othersVal + val : val ) ;
91+ }
92+
93+ _chartDataMap . delete ( walletType ) ;
94+ }
95+
96+ const _chartConfig : ChartConfig = { } ;
97+ walletTypesToShow . forEach ( ( walletType , i ) => {
8998 _chartConfig [ walletType ] = {
90- label : uniqueWalletTypes [ i ] ,
91- color : `hsl(${ i + hueIncrement * i } deg, var(--chart-saturation), var(--chart-lightness ))` ,
99+ label : walletTypesToShow [ i ] ,
100+ color : `hsl(var(--chart-${ ( i % 10 ) + 1 } ))` ,
92101 } ;
93- }
102+ } ) ;
103+
104+ // Add Others
105+ _chartConfig . others = {
106+ label : "Others" ,
107+ color : "hsl(var(--muted-foreground))" ,
108+ } ;
94109
95110 const _chartData : ChartData [ ] = Array . from ( _chartDataMap ) . map (
96111 ( [ walletType , data ] ) => {
97112 return {
98113 walletType,
99- totalWallets : data . total ,
100- uniqueWallets : data . unique ,
114+ value : data ,
101115 fill : _chartConfig [ walletType ] . color || "transparent" ,
102116 } ;
103117 } ,
104118 ) ;
105119
106120 // sort the data
107- _chartData . sort ( ( a , b ) => b . totalWallets - a . totalWallets ) ;
121+ _chartData . sort ( ( a , b ) => b . value - a . value ) ;
108122
109123 return {
110124 chartData : _chartData ,
111125 chartConfig : _chartConfig ,
112126 totalConnections : _totalConnections ,
113127 uniqueConnections : _uniqueConnections ,
114128 } ;
115- } , [ walletStats ] ) ;
129+ } , [ walletStats , chartToShow ] ) ;
116130
117131 const disableActions = props . isPending || chartData . length === 0 ;
118132 return (
@@ -152,23 +166,14 @@ export function WalletDistributionChartCard(props: {
152166 getData = { async ( ) => {
153167 const header = [
154168 "Wallet" ,
155- "Total Connections" ,
156- "Unique Connections" ,
157- "Percentage of Total connections" ,
158- "Percentage of Unique connections" ,
169+ `${ chartToShow === "totalConnections" ? "Total" : "Unique" } Connections` ,
170+ `Percentage of ${ chartToShow === "totalConnections" ? "Total" : "Unique" } Connections` ,
159171 ] ;
160172 const rows = chartData . map ( ( d ) => {
161173 return [
162- // name
163174 d . walletType ,
164- // total connections
165- d . totalWallets . toString ( ) ,
166- // unique connections
167- d . uniqueWallets . toString ( ) ,
168- // percentage of total connections
169- `${ ( ( d . totalWallets / totalConnections ) * 100 ) . toFixed ( 2 ) } %` ,
170- // percentage of unique connections
171- `${ ( ( d . uniqueWallets / uniqueConnections ) * 100 ) . toFixed ( 2 ) } %` ,
175+ d . value . toString ( ) ,
176+ `${ ( ( d . value / ( chartToShow === "totalConnections" ? totalConnections : uniqueConnections ) ) * 100 ) . toFixed ( 2 ) } %` ,
172177 ] ;
173178 } ) ;
174179 return {
@@ -197,7 +202,7 @@ export function WalletDistributionChartCard(props: {
197202 valueFormatter = { ( v ) => {
198203 if ( typeof v === "number" ) {
199204 const sumValue =
200- chartToShow === "unique "
205+ chartToShow === "uniqueWalletsConnected "
201206 ? uniqueConnections
202207 : totalConnections ;
203208 const percentageValue = ( ( v / sumValue ) * 100 ) . toFixed ( 2 ) ;
@@ -210,9 +215,7 @@ export function WalletDistributionChartCard(props: {
210215 < ChartLegend content = { < ChartLegendContent /> } className = "" />
211216 < Pie
212217 data = { chartData }
213- dataKey = {
214- chartToShow === "unique" ? "uniqueWallets" : "totalWallets"
215- }
218+ dataKey = "value"
216219 nameKey = "walletType"
217220 innerRadius = { 60 }
218221 strokeWidth = { 2 }
0 commit comments