"use client"
import { useState } from "react"
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { Checkbox } from "@/components/ui/checkbox"
import { Badge } from "@/components/ui/badge"
import { Separator } from "@/components/ui/separator"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { Search, Filter, Download, RefreshCw, Play, ArrowRight, Settings } from 'lucide-react'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
// 模拟图表组件
const NetworkGraph = ({ data, title }: { data: any; title: string }) => (
)
const ScatterPlot = ({ data, title }: { data: any; title: string }) => (
)
const HeatMap = ({ data, title }: { data: any; title: string }) => (
)
interface CorrelationResult {
id: string
field1: string
field2: string
correlationCoefficient: number
significance: number
sampleSize: number
createdAt: string
dataSource: string
}
interface UserSegmentCorrelation {
id: string
segment1: string
segment2: string
overlapPercentage: number
uniqueUsers1: number
uniqueUsers2: number
commonUsers: number
}
export function DataCorrelationAnalysis() {
const [activeTab, setActiveTab] = useState("field-correlation")
const [isCreatingAnalysis, setIsCreatingAnalysis] = useState(false)
const [selectedFields, setSelectedFields] = useState([])
const [selectedDataSources, setSelectedDataSources] = useState([])
// 模拟字段列表
const fields = [
{ id: "userId", name: "用户ID", type: "string", dataSource: "所有数据源" },
{ id: "phoneNumber", name: "手机号", type: "string", dataSource: "所有数据源" },
{ id: "identityNumber", name: "身份证号", type: "string", dataSource: "所有数据源" },
{ id: "deviceImei", name: "IMEI设备号", type: "string", dataSource: "所有数据源" },
{ id: "registrationDate", name: "注册时间", type: "datetime", dataSource: "用户数据库" },
{ id: "lastActiveTime", name: "最后活跃时间", type: "datetime", dataSource: "行为分析平台" },
{ id: "age", name: "年龄", type: "number", dataSource: "用户数据库" },
{ id: "gender", name: "性别", type: "string", dataSource: "用户数据库" },
{ id: "location", name: "地区", type: "string", dataSource: "用户数据库" },
{ id: "totalSpent", name: "消费总额", type: "number", dataSource: "交易系统" },
{ id: "purchaseFrequency", name: "购买频率", type: "number", dataSource: "交易系统" },
{ id: "averageOrderValue", name: "平均订单金额", type: "number", dataSource: "交易系统" },
{ id: "preferredCategory", name: "偏好类别", type: "string", dataSource: "交易系统" },
{ id: "loginFrequency", name: "登录频率", type: "number", dataSource: "行为分析平台" },
{ id: "timeSpentPerSession", name: "单次会话时长", type: "number", dataSource: "行为分析平台" },
{ id: "clickThroughRate", name: "点击率", type: "number", dataSource: "行为分析平台" },
{ id: "conversionRate", name: "转化率", type: "number", dataSource: "行为分析平台" },
{ id: "rfmScore", name: "RFM评分", type: "number", dataSource: "用户价值模型" },
{ id: "lifetimeValue", name: "生命周期价值", type: "number", dataSource: "用户价值模型" },
{ id: "churnRisk", name: "流失风险", type: "number", dataSource: "用户价值模型" },
]
// 模拟数据源
const dataSources = [
{ id: "all", name: "所有数据源" },
{ id: "user-db", name: "用户数据库" },
{ id: "transaction", name: "交易系统" },
{ id: "behavior", name: "行为分析平台" },
{ id: "user-value", name: "用户价值模型" },
]
// 模拟用户分群
const userSegments = [
{ id: "high-value", name: "高价值用户", count: 12500 },
{ id: "new-users", name: "新注册用户", count: 45600 },
{ id: "inactive", name: "非活跃用户", count: 28900 },
{ id: "potential", name: "潜在高转化用户", count: 18700 },
{ id: "loyal", name: "忠诚用户", count: 9800 },
{ id: "risk", name: "流失风险用户", count: 15400 },
]
// 模拟相关性分析结果
const correlationResults: CorrelationResult[] = [
{
id: "corr-1",
field1: "年龄",
field2: "消费总额",
correlationCoefficient: 0.72,
significance: 0.001,
sampleSize: 45678,
createdAt: "2023-07-20 15:30",
dataSource: "用户数据库 & 交易系统",
},
{
id: "corr-2",
field1: "登录频率",
field2: "购买频率",
correlationCoefficient: 0.85,
significance: 0.001,
sampleSize: 38945,
createdAt: "2023-07-20 15:30",
dataSource: "行为分析平台 & 交易系统",
},
{
id: "corr-3",
field1: "单次会话时长",
field2: "转化率",
correlationCoefficient: 0.65,
significance: 0.01,
sampleSize: 42156,
createdAt: "2023-07-20 15:30",
dataSource: "行为分析平台",
},
{
id: "corr-4",
field1: "地区",
field2: "偏好类别",
correlationCoefficient: 0.58,
significance: 0.05,
sampleSize: 45678,
createdAt: "2023-07-20 15:30",
dataSource: "用户数据库 & 交易系统",
},
{
id: "corr-5",
field1: "RFM评分",
field2: "生命周期价值",
correlationCoefficient: 0.92,
significance: 0.001,
sampleSize: 45678,
createdAt: "2023-07-20 15:30",
dataSource: "用户价值模型",
},
]
// 模拟用户分群相关性
const segmentCorrelations: UserSegmentCorrelation[] = [
{
id: "seg-corr-1",
segment1: "高价值用户",
segment2: "忠诚用户",
overlapPercentage: 68.5,
uniqueUsers1: 12500,
uniqueUsers2: 9800,
commonUsers: 6713,
},
{
id: "seg-corr-2",
segment1: "新注册用户",
segment2: "潜在高转化用户",
overlapPercentage: 12.3,
uniqueUsers1: 45600,
uniqueUsers2: 18700,
commonUsers: 5609,
},
{
id: "seg-corr-3",
segment1: "非活跃用户",
segment2: "流失风险用户",
overlapPercentage: 75.2,
uniqueUsers1: 28900,
uniqueUsers2: 15400,
commonUsers: 11581,
},
{
id: "seg-corr-4",
segment1: "高价值用户",
segment2: "潜在高转化用户",
overlapPercentage: 8.7,
uniqueUsers1: 12500,
uniqueUsers2: 18700,
commonUsers: 1088,
},
{
id: "seg-corr-5",
segment1: "忠诚用户",
segment2: "流失风险用户",
overlapPercentage: 2.1,
uniqueUsers1: 9800,
uniqueUsers2: 15400,
commonUsers: 206,
},
]
const getCorrelationStrength = (coefficient: number) => {
const absCoefficient = Math.abs(coefficient)
if (absCoefficient >= 0.8) return "强"
if (absCoefficient >= 0.5) return "中"
return "弱"
}
const getCorrelationBadge = (coefficient: number) => {
const absCoefficient = Math.abs(coefficient)
if (absCoefficient >= 0.8) {
return 强相关
}
if (absCoefficient >= 0.5) {
return 中等相关
}
return 弱相关
}
const getSignificanceBadge = (significance: number) => {
if (significance <= 0.001) {
return 高度显著 (p≤0.001)
}
if (significance <= 0.01) {
return 显著 (p≤0.01)
}
if (significance <= 0.05) {
return 边际显著 (p≤0.05)
}
return {'不显著 (p>0.05)'}
}
const toggleFieldSelection = (fieldId: string) => {
if (selectedFields.includes(fieldId)) {
setSelectedFields(selectedFields.filter((id) => id !== fieldId))
} else {
setSelectedFields([...selectedFields, fieldId])
}
}
const toggleDataSourceSelection = (sourceId: string) => {
if (selectedDataSources.includes(sourceId)) {
setSelectedDataSources(selectedDataSources.filter((id) => id !== sourceId))
} else {
setSelectedDataSources([...selectedDataSources, sourceId])
}
}
return (
数据关联分析
字段相关性
分群相关性
用户关联网络
用户路径分析
字段1
字段2
相关系数
相关强度
显著性
样本量
数据源
操作
{correlationResults.map((result) => (
{result.field1}
{result.field2}
{result.correlationCoefficient.toFixed(2)}
{getCorrelationBadge(result.correlationCoefficient)}
{getSignificanceBadge(result.significance)}
{result.sampleSize.toLocaleString()}
{result.dataSource}
))}
相关性热力图
字段间相关性强度可视化
散点图分析
选择两个字段查看其相关性散点图
用户分群相关性分析
分群1
分群2
重叠率
分群1用户数
分群2用户数
共同用户数
操作
{segmentCorrelations.map((correlation) => (
{correlation.segment1}
{correlation.segment2}
{correlation.overlapPercentage.toFixed(1)}%
{correlation.overlapPercentage > 50 ? (
高重叠
) : correlation.overlapPercentage > 20 ? (
中等重叠
) : (
低重叠
)}
{correlation.uniqueUsers1.toLocaleString()}
{correlation.uniqueUsers2.toLocaleString()}
{correlation.commonUsers.toLocaleString()}
))}
分群重叠分析
选择两个用户分群查看其重叠情况
共同用户: 6,713人 | 高价值用户: 12,500人 | 忠诚用户: 9,800人
用户关联网络分析
基于多维度标识符的用户关联网络
网络统计
节点: 1,245 | 连接: 3,567 | 平均连接度: 2.87
用户ID
手机号
身份证号
IMEI设备号
关联群组分析
自动检测的用户关联群组
群组ID
用户数
关联强度
主要关联维度
检测时间
操作
群组-001
125
强
手机号, IMEI设备号
2023-07-20 15:30
群组-002
87
强
身份证号, 用户ID
2023-07-20 15:30
群组-003
215
中
手机号, 用户ID
2023-07-20 15:30
群组-004
56
弱
IMEI设备号
2023-07-20 15:30
用户路径分析
分析用户在不同系统和渠道间的行为路径
路径转化率
注册到浏览
85%
浏览到加购
65%
加购到购买
40%
总体转化率
22%
路径流失点
注册后未浏览
15%
浏览后未加购
35%
加购后未购买
60%
总体流失率
78%
{/* 创建分析对话框 */}
)
}