File size: 4,172 Bytes
67c8aca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Chart as ChartJS,
  ArcElement,
  Tooltip,
  Legend,
  CategoryScale,
  LinearScale,
  BarElement,
  Title,
  RadialLinearScale,
  PointElement,
  LineElement,
  Filler,
} from 'chart.js';
import { Doughnut, Bar, Radar } from 'react-chartjs-2';

ChartJS.register(
  ArcElement,
  Tooltip,
  Legend,
  CategoryScale,
  LinearScale,
  BarElement,
  Title,
  RadialLinearScale,
  PointElement,
  LineElement,
  Filler
);

export function DTIGauge({ dti }) {
  const data = {
    labels: ['DTI', 'Remaining Capacity'],
    datasets: [
      {
        data: [dti, Math.max(0, 100 - dti)],
        backgroundColor: [
          dti > 50 ? '#ef4444' : dti > 30 ? '#f59e0b' : '#10b981',
          '#f3f4f6'
        ],
        borderWidth: 0,
        cutout: '85%'
      }
    ]
  };

  const options = {
    rotation: -90,
    circumference: 180,
    plugins: {
      legend: { display: false },
      tooltip: { enabled: false }
    },
    maintainAspectRatio: true,
  };

  return (
    <div style={{ position: 'relative', width: '220px', margin: '0 auto' }}>
      <Doughnut data={data} options={options} />
      <div style={{
        position: 'absolute',
        top: '70%',
        left: '50%',
        transform: 'translate(-50%, -50%)',
        textAlign: 'center'
      }}>
        <div style={{ fontSize: '1.75rem', fontWeight: '700' }}>{dti.toFixed(1)}%</div>
        <div style={{ fontSize: '0.75rem', color: '#6b7280', fontWeight: '600' }}>DEBT-TO-INCOME</div>
      </div>
    </div>
  );
}

export function ComparisonRadar({ userResults }) {
  const benchmarks = JSON.parse(userResults.benchmarks_json || "{}");
  
  // Clean keys for display
  const labelMap = {
    'ApplicantIncome': 'Income',
    'LoanAmount': 'Loan Size',
    'Total_Income': 'Total Rev',
    'EMI': 'EMI Load',
    'Credit_History': 'Credit Score'
  };

  const keys = Object.keys(labelMap);
  
  // Normalize values roughly for radar (0-100 scale)
  const normalize = (val, max) => Math.min(100, (val / max) * 100);

  const userData = [
    normalize(userResults.applicant_income, benchmarks.ApplicantIncome * 2 || 10000),
    normalize(userResults.loan_amount, benchmarks.LoanAmount * 2 || 500),
    normalize(userResults.applicant_income + userResults.coapplicant_income, benchmarks.Total_Income * 2 || 15000),
    normalize((userResults.loan_amount * 1000) / userResults.loan_amount_term, benchmarks.EMI * 2 || 2000),
    userResults.credit_history * 100
  ];

  const benchData = [50, 50, 50, 50, 50]; // Benchmarks are normalized to center

  const data = {
    labels: keys.map(k => labelMap[k]),
    datasets: [
      {
        label: 'Your Profile',
        data: userData,
        backgroundColor: 'rgba(242, 98, 47, 0.2)',
        borderColor: '#f2622f',
        borderWidth: 2,
        pointBackgroundColor: '#f2622f',
      },
      {
        label: 'Approved Average',
        data: benchData,
        backgroundColor: 'rgba(107, 114, 128, 0.1)',
        borderColor: '#9ca3af',
        borderWidth: 1,
        borderDash: [5, 5],
        pointRadius: 0
      }
    ]
  };

  const options = {
    scales: {
      r: {
        angleLines: { display: false },
        suggestedMin: 0,
        suggestedMax: 100,
        ticks: { display: false }
      }
    },
    plugins: {
      legend: { position: 'bottom', labels: { boxWidth: 12, font: { size: 10 } } }
    }
  };

  return <Radar data={data} options={options} />;
}

export function FeatureImportanceBar({ featuresJson }) {
  let features = {};
  try {
    features = JSON.parse(featuresJson);
  } catch(e) {
    return null;
  }

  const sorted = Object.entries(features)
    .sort(([, a], [, b]) => b - a)
    .slice(0, 5);

  const data = {
    labels: sorted.map(([k]) => k.replace(/_/g, ' ')),
    datasets: [{
      label: 'Impact Score',
      data: sorted.map(([,v]) => v),
      backgroundColor: '#f2622f',
      borderRadius: 6
    }]
  };

  const options = {
    indexAxis: 'y',
    plugins: { legend: { display: false } },
    scales: {
      x: { display: false },
      y: { grid: { display: false }, ticks: { font: { size: 11 } } }
    }
  };

  return <Bar data={data} options={options} />;
}