File size: 9,932 Bytes
d328b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"use client"

import { useMemo } from "react"
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell, Legend } from "recharts"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"

interface Dataset {
  Name: string
  Category: string
  Description: string
  Task: string
  Data_Type: string
  Source: string
  "Paper link": string
  Availability: string
  Contact: string
}

interface DatasetStatisticsProps {
  datasets: Dataset[]
  selectedCategory: string | null
  selectedDataType: string | null
  selectedAvailability: string | null
  onCategoryClick: (category: string) => void
  onDataTypeClick: (dataType: string) => void
  onAvailabilityClick: (availability: string) => void
}

const CATEGORY_COLORS = [
  "#6366f1", // indigo
  "#10b981", // emerald
  "#f59e0b", // amber
  "#3b82f6", // blue
  "#8b5cf6", // violet
  "#06b6d4", // cyan
]

const DATA_TYPE_COLORS = [
  "#3b82f6", // blue
  "#10b981", // emerald
  "#6366f1", // indigo
  "#f59e0b", // amber
  "#8b5cf6", // violet
  "#06b6d4", // cyan
]

const AVAILABILITY_COLORS = {
  "Open source": "#3b82f6", // blue
  "Gated": "#10b981", // emerald
  "Unknown": "#6b7280", // gray
}

export function DatasetStatistics({ 
  datasets, 
  selectedCategory, 
  selectedDataType, 
  selectedAvailability,
  onCategoryClick,
  onDataTypeClick,
  onAvailabilityClick
}: DatasetStatisticsProps) {
  const categoryData = useMemo(() => {
    const counts: Record<string, number> = {}
    datasets.forEach((dataset) => {
      if (dataset.Category) {
        counts[dataset.Category] = (counts[dataset.Category] || 0) + 1
      }
    })
    return Object.entries(counts)
      .map(([name, value]) => ({ name, value }))
      .sort((a, b) => b.value - a.value)
  }, [datasets])

  const dataTypeData = useMemo(() => {
    const counts: Record<string, number> = {}
    datasets.forEach((dataset) => {
      if (dataset.Data_Type) {
        counts[dataset.Data_Type] = (counts[dataset.Data_Type] || 0) + 1
      }
    })
    return Object.entries(counts)
      .map(([name, value]) => ({ name, value }))
      .sort((a, b) => b.value - a.value)
  }, [datasets])

  const availabilityData = useMemo(() => {
    const counts: Record<string, number> = {}
    datasets.forEach((dataset) => {
      if (dataset.Availability) {
        counts[dataset.Availability] = (counts[dataset.Availability] || 0) + 1
      }
    })
    return Object.entries(counts).map(([name, value]) => ({ name, value }))
  }, [datasets])

  const CustomTooltip = ({ active, payload }: any) => {
    if (active && payload && payload.length) {
      return (
        <div className="bg-popover border border-border rounded-lg shadow-lg p-3">
          <p className="font-medium text-sm">{payload[0].payload.name}</p>
          <p className="text-sm text-muted-foreground">Count: {payload[0].value}</p>
        </div>
      )
    }
    return null
  }

  const renderCustomBarLabel = (props: any) => {
    const { x, y, width, height, value } = props
    return (
      <text
        x={x + width + 5}
        y={y + height / 2}
        fill="currentColor"
        className="text-sm font-medium fill-foreground"
        textAnchor="start"
        dominantBaseline="middle"
      >
        {value}
      </text>
    )
  }

  return (
    <div className="space-y-6 mb-8">
      {/* Top Row: By Data Type and Availability */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {/* By Data Type */}
        <Card>
          <CardHeader>
            <CardTitle className="text-lg font-semibold">
              By Data Type
              {selectedDataType && (
                <span className="text-sm font-normal text-muted-foreground ml-2">
                  (Click to clear filter)
                </span>
              )}
            </CardTitle>
          </CardHeader>
          <CardContent>
            <ResponsiveContainer width="100%" height={300}>
              <BarChart
                data={dataTypeData}
                layout="vertical"
                margin={{ top: 5, right: 40, left: 5, bottom: 5 }}
              >
                <CartesianGrid strokeDasharray="3 3" className="stroke-border" />
                <XAxis type="number" className="text-xs" tick={{ fill: "currentColor" }} />
                <YAxis
                  type="category"
                  dataKey="name"
                  width={100}
                  className="text-xs"
                  tick={{ fill: "currentColor" }}
                />
                <Tooltip content={<CustomTooltip />} />
                <Bar 
                  dataKey="value" 
                  radius={[0, 4, 4, 0]} 
                  label={renderCustomBarLabel}
                  onClick={(data) => onDataTypeClick(data.name)}
                  cursor="pointer"
                >
                  {dataTypeData.map((entry, index) => {
                    const isSelected = selectedDataType === entry.name
                    const shouldGreyscale = selectedDataType && !isSelected
                    const color = shouldGreyscale 
                      ? "#94a3b8" 
                      : DATA_TYPE_COLORS[index % DATA_TYPE_COLORS.length]
                    return (
                      <Cell 
                        key={`cell-${index}`} 
                        fill={color}
                        opacity={shouldGreyscale ? 0.4 : 1}
                      />
                    )
                  })}
                </Bar>
              </BarChart>
            </ResponsiveContainer>
          </CardContent>
        </Card>

        {/* Availability */}
        <Card>
          <CardHeader>
            <CardTitle className="text-lg font-semibold">
              Availability
              {selectedAvailability && (
                <span className="text-sm font-normal text-muted-foreground ml-2">
                  (Click to clear filter)
                </span>
              )}
            </CardTitle>
          </CardHeader>
          <CardContent>
            <ResponsiveContainer width="100%" height={300}>
              <PieChart>
                <Pie
                  data={availabilityData}
                  cx="50%"
                  cy="50%"
                  labelLine={false}
                  label={({ name, value, percent }) => `${name}: ${value}`}
                  outerRadius={80}
                  fill="#8884d8"
                  dataKey="value"
                  onClick={(data) => onAvailabilityClick(data.name)}
                  cursor="pointer"
                >
                  {availabilityData.map((entry, index) => {
                    const isSelected = selectedAvailability === entry.name
                    const shouldGreyscale = selectedAvailability && !isSelected
                    const originalColor = AVAILABILITY_COLORS[entry.name as keyof typeof AVAILABILITY_COLORS] || "#6b7280"
                    const color = shouldGreyscale ? "#94a3b8" : originalColor
                    return (
                      <Cell
                        key={`cell-${index}`}
                        fill={color}
                        opacity={shouldGreyscale ? 0.4 : 1}
                      />
                    )
                  })}
                </Pie>
                <Tooltip content={<CustomTooltip />} />
                <Legend
                  verticalAlign="bottom"
                  height={36}
                  iconType="circle"
                  formatter={(value) => <span className="text-sm">{value}</span>}
                />
              </PieChart>
            </ResponsiveContainer>
          </CardContent>
        </Card>
      </div>

      {/* Bottom Row: By Category */}
      <div className="grid grid-cols-1 gap-6">
        <Card>
          <CardHeader>
            <CardTitle className="text-lg font-semibold">
              By Category
              {selectedCategory && (
                <span className="text-sm font-normal text-muted-foreground ml-2">
                  (Click to clear filter)
                </span>
              )}
            </CardTitle>
          </CardHeader>
          <CardContent>
            <ResponsiveContainer width="100%" height={300}>
              <BarChart
                data={categoryData}
                layout="vertical"
                margin={{ top: 5, right: 40, left: 5, bottom: 5 }}
              >
                <CartesianGrid strokeDasharray="3 3" className="stroke-border" />
                <XAxis type="number" className="text-xs" tick={{ fill: "currentColor" }} />
                <YAxis
                  type="category"
                  dataKey="name"
                  width={180}
                  className="text-xs"
                  tick={{ fill: "currentColor" }}
                />
                <Tooltip content={<CustomTooltip />} />
                <Bar 
                  dataKey="value" 
                  radius={[0, 4, 4, 0]} 
                  label={renderCustomBarLabel}
                  onClick={(data) => onCategoryClick(data.name)}
                  cursor="pointer"
                >
                  {categoryData.map((entry, index) => {
                    const isSelected = selectedCategory === entry.name
                    const shouldGreyscale = selectedCategory && !isSelected
                    const color = shouldGreyscale 
                      ? "#94a3b8" 
                      : CATEGORY_COLORS[index % CATEGORY_COLORS.length]
                    return (
                      <Cell 
                        key={`cell-${index}`} 
                        fill={color}
                        opacity={shouldGreyscale ? 0.4 : 1}
                      />
                    )
                  })}
                </Bar>
              </BarChart>
            </ResponsiveContainer>
          </CardContent>
        </Card>
      </div>
    </div>
  )
}