-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.html
More file actions
1234 lines (1003 loc) · 26.6 KB
/
Copy pathstatistics.html
File metadata and controls
1234 lines (1003 loc) · 26.6 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BlackRoad Statistics</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
:root{
--bg:#000;
--card:#121212;
--text:#fff;
--muted:#888;
--accent:#FFF;
}
*{box-sizing:border-box;font-family: 'Inter', sans-serif;}
body{
margin:0;
background:var(--bg);
color:var(--text);
font-family: 'Inter', sans-serif;
}
/* HEADER */
.header{
position:sticky;
top:0;
background:#000;
padding:12px;
font-size:18px;
font-weight:bold;
border-bottom:1px solid #222;
font-family: 'Inter', sans-serif;
z-index:10;
box-shadow: grey 0px 0px 3px;
}
/* MESSAGE */
.msg{
margin:10px;
padding:10px;
background:var(--bg);
border-radius:10px;
font-size:13px;
border:1.3px solid grey;
color:var(--accent);
font-family: 'Inter', sans-serif;
box-shadow: grey 0px 0px 3px;
}
/* CARDS */
.grid{
display:flex;
gap:10px;
overflow-x:auto;
padding:10px;
}
.card{
min-width:120px;
background:var(--bg);
padding:12px;
border-radius:12px;
border:1.3px solid grey;
box-shadow: grey 0px 0px 3px;
}
.label{
font-size:12px;
color:var(--accent);
}
.value{
font-size:18px;
margin-top:5px;
font-weight:bold;
color:var(--accent);
}
/* SECTION */
.section{
margin:15px 10px;
}
.section h3{
margin-bottom:8px;
font-size:14px;
color:var(--accent);
}
/* CHART */
.chart-box{
background:var(--bg);
padding:10px;
border-radius:12px;
overflow-x:auto;
box-shadow: grey 0px 0px 3px;
}
canvas{
height:260px !important;
}
.gauge-card {
width: 100%;
background: var(--bg);
padding: 12px;
border-radius: 12px;
border: 1px solid #333;
box-shadow: 0 2px 5px rgba(0,0,0,0.5);
cursor: pointer;
transition: transform 0.2s;
}
.gauge-card:hover {
transform: scale(1.02);
}
.gauge-label {
font-size: 14px;
color: var(--accent);
font-weight: 500;
margin-bottom: 6px;
}
.gauge-bar-container {
width: 100%;
background: #222;
border-radius: 10px;
height: 18px;
overflow: hidden;
}
.gauge-bar {
height: 100%;
border-radius: 10px;
text-align: right;
color: #fff;
font-size: 12px;
line-height: 18px;
padding-right: 8px;
font-family: 'Inter', sans-serif;
transition: width 1s ease;
}
/* Sources list (toggleable) */
.gauge-sources {
margin-top: 8px;
font-size: 13px;
color: #ccc;
background: #1a1a1a;
border-radius: 8px;
padding: 8px;
display: none;
transition: all 0.3s ease;
}
.gauge-sources div {
padding: 2px 0;
}
</style>
</head>
<body>
<div class="header"> BlackRoad Statistics</div>
<!--
<div id="msg" class="msg">Loading data...</div>
-->
<!-- 1. MAIN SUMMARY -->
<div class="grid">
<div class="card"><div class="label">Income</div><div id="income" class="value">0</div></div>
<div class="card"><div class="label">Expense</div><div id="expense" class="value">0</div></div>
<div class="card"><div class="label">Balance</div><div id="balance" class="value">0</div></div>
</div>
<div class="section">
<h3>Financial Summary</h3>
<div class="chart-box">
<canvas id="summaryChart"></canvas>
</div>
</div>
<!-- 2. CASH FLOW (MOST IMPORTANT TREND FIRST) -->
<div class="section">
<h3>Cash Flow Trend</h3>
<div class="chart-box"><canvas id="trendChart"></canvas></div>
</div>
<!-- 3. MONTHLY PERFORMANCE -->
<div class="section">
<h3>Monthly Overview</h3>
<div class="chart-box"><canvas id="monthChart"></canvas></div>
</div>
<!-- 4. SPENDING BREAKDOWN -->
<div class="section">
<h3>Category Spending</h3>
<div class="chart-box"><canvas id="catChart"></canvas></div>
</div>
<!-- 5. CATEGORY BEHAVIOR -->
<div class="section">
<h3>Category Trend</h3>
<div class="chart-box"><canvas id="catTrendChart"></canvas></div>
</div>
<!-- 6. INSIGHTS (QUICK UNDERSTANDING) -->
<div class="section">
<h3>Insights</h3>
<div class="grid">
<div class="card">
<div class="label">Highest Spend</div>
<div id="highDay" class="value">-</div>
</div>
<div class="card">
<div class="label">Avg Daily Spend</div>
<div id="avgSpend" class="value">-</div>
</div>
</div>
<div class="chart-box">
<canvas id="topDaysChart"></canvas>
</div>
</div>
<!-- 7. EFFICIENCY (HOW WELL YOU MANAGE MONEY) -->
<div class="section">
<h3>Financial Efficiency</h3>
<div class="grid">
<div class="card">
<div class="label">Savings</div>
<div id="savingsVal" class="value">-</div>
</div>
<div class="card">
<div class="label">Savings Rate</div>
<div id="savingsRate" class="value">-</div>
</div>
</div>
<div class="msg" id="effMsg">Analyzing...</div>
</div>
<!-- 8. RATIOS (ADVANCED ANALYSIS) -->
<div class="section">
<h3>Financial Ratios</h3>
<div class="grid">
<div class="card">
<div class="label">Expense Ratio</div>
<div id="expRatio" class="value">-</div>
</div>
<div class="card">
<div class="label">Savings Ratio</div>
<div id="savRatio" class="value">-</div>
</div>
<div class="card">
<div class="label">Burn Rate</div>
<div id="burnRate" class="value">-</div>
</div>
<div class="card">
<div class="label">Income Dependency</div>
<div id="depRatio" class="value">-</div>
</div>
</div>
<div class="msg" id="ratioMsg">Analyzing...</div>
</div>
<!-- 9. GROWTH (LONG TERM) -->
<div class="section">
<h3>Wealth Growth</h3>
<div class="grid">
<div class="card">
<div class="label">Growth %</div>
<div id="growthRate" class="value">-</div>
</div>
<div class="card">
<div class="label">Trend</div>
<div id="growthTrend" class="value">-</div>
</div>
</div>
<div class="msg" id="growthMsg">Analyzing...</div>
<div class="chart-box">
<canvas id="growthChart"></canvas>
</div>
</div>
<!-- 10. FINAL SCORE (SUMMARY OF EVERYTHING) -->
<div class="section">
<h3>Financial Health Score</h3>
<div class="grid">
<div class="card">
<div class="label">Score</div>
<div id="finScore" class="value">-</div>
</div>
<div class="card">
<div class="label">Health</div>
<div id="finHealth" class="value">-</div>
</div>
</div>
<div class="msg" id="finMsg">Analyzing...</div>
<div class="chart-box">
<canvas id="scoreChart"></canvas>
</div>
</div>
<div class="section">
<h3>Expense Category Gauges</h3>
<div id="expenseGauges" style="display:flex; flex-direction:column; gap:8px;"></div>
</div>
<script>
let data = [];
/* MESSAGE */
function sendB(t){
document.getElementById("msg").innerHTML = t;
}
/* EXTRACT */
function ext(html){
const doc = new DOMParser().parseFromString(html, "text/html");
const out = [];
doc.querySelectorAll(".child").forEach(c => {
const h = c.querySelector(".idfeb");
const obj = {
income: +h?.querySelector(".incomeColumn")?.innerText || 0,
date: h?.querySelector(".dateColumn")?.innerText || "",
from: h?.querySelector(".fromm")?.innerText || "",
expense: +h?.querySelector(".exep")?.innerText || 0,
balance: +h?.querySelector(".Abal")?.innerText || 0,
transactions: []
};
c.querySelectorAll(".trand .trans").forEach(t => {
obj.transactions.push({
amount: +t.querySelector(".mony")?.innerText || 0,
date: t.querySelector(".tdate")?.innerText || "",
category: t.dataset.category || "",
description: t.querySelector(".tdes")?.innerText || ""
});
});
out.push(obj);
});
return out;
}
/* GET DATA */
function get(){
const r = indexedDB.open("black");
r.onsuccess = e => {
const db = e.target.result;
let tx = db.transaction("sst","readonly");
let st = tx.objectStore("sst");
let req = st.get("blackroadINC");
req.onsuccess = ()=>{
if(req.result?.data){
data = ext(req.result.data);
//sendB("✅ Data Loaded");
analyze();
}else{
sendB("❌ No Data Found");
}
};
};
r.onerror = ()=>{
sendB("❌ Failed to open DB");
};
}
/* FORMAT */
function formatMonth(d){
if(!d) return "Unknown";
let p = d.split(/[.,]/);
if(p.length<3) return "Unknown";
return "20"+p[2]+"-"+p[1].padStart(2,'0');
}
function getCategory(t){
if(t.category) return t.category;
let d = t.description.toLowerCase();
if(d.includes("food")) return "Food";
if(d.includes("phone")) return "Phone";
if(d.includes("home")) return "Home";
if(d.includes("travel")) return "Travel";
return "Other";
}
function formatMoney(n){
return "₹ " + n.toLocaleString();
}
/* CANVAS WIDTH FIX */
function setCanvasWidth(id, count){
const c = document.getElementById(id);
c.style.width = Math.max(320, count * 40) + "px";
}
/* ANALYZE */
function analyze(){
let totalIncome=0;
let totalExpense=0;
let monthly={};
let category={};
let trend={};
data.forEach(d=>{
let m = formatMonth(d.date);
totalIncome += d.income;
if(!monthly[m]) monthly[m]={income:0,expense:0};
monthly[m].income += d.income;
if(!trend[d.date]) trend[d.date]=0;
trend[d.date]+=d.income;
d.transactions.forEach(t=>{
let cat = getCategory(t);
totalExpense += t.amount;
monthly[m].expense += t.amount;
if(!category[cat]) category[cat]=0;
category[cat]+=t.amount;
if(!trend[t.date]) trend[t.date]=0;
trend[t.date]-=t.amount;
});
});
document.getElementById("income").innerText = formatMoney(totalIncome);
document.getElementById("expense").innerText = formatMoney(totalExpense);
document.getElementById("balance").innerText = formatMoney(totalIncome-totalExpense);
drawSummary(totalIncome, totalExpense);
drawMonthly(monthly);
drawCategory(category);
drawTrend(trend);
drawInsights(data);
drawCategoryTrend(data);
drawEfficiency(totalIncome, totalExpense);
drawFinancialScore(totalIncome, totalExpense, category);
drawGrowth(data);
drawRatios(data, totalIncome, totalExpense);
drawExpenseGauges(category);
}
function drawExpenseGauges(category){
const container = document.getElementById("expenseGauges");
container.innerHTML = ""; // clear previous
// Sort categories high → low
const sorted = Object.entries(category).sort((a,b)=> b[1] - a[1]);
const total = Object.values(category).reduce((sum,val)=>sum+val,0);
const colors = ["#FF6384","#36A2EB","#FFCE56","#4BC0C0","#9966FF","#FF9F40","#C9CBCF"];
let i = 0;
for(const [cat, val] of sorted){
const percent = total ? ((val / total) * 100).toFixed(1) : 0;
const card = document.createElement("div");
card.className = "gauge-card";
// Add placeholder for sources
card.innerHTML = `
<div class="gauge-label">${cat} ${val} => ${percent}% </div>
<div class="gauge-bar-container">
<div class="gauge-bar" style="width:0%; background:${colors[i % colors.length]}"></div>
</div>
<div class="gauge-sources"></div>
`;
container.appendChild(card);
// Animate bar
setTimeout(()=>{
card.querySelector(".gauge-bar").style.width = percent + "%";
}, 50);
// Click event: toggle sources
card.querySelector(".gauge-bar-container").addEventListener("click", ()=>{
const sourcesDiv = card.querySelector(".gauge-sources");
if(sourcesDiv.style.display === "block"){
sourcesDiv.style.display = "none";
} else {
sourcesDiv.innerHTML = ""; // clear previous
// Filter all transactions for this category
let trans = [];
data.forEach(d=>{
d.transactions.forEach(t=>{
if(getCategory(t) === cat) trans.push(t);
});
});
// Group by source
const bySource = {};
trans.forEach(t=>{
const src = t.from || t.description || "Unknown";
if(!bySource[src]) bySource[src] = 0;
bySource[src] += t.amount;
});
// Build HTML
let njk=1;
for(const [src, amt] of Object.entries(bySource)){
const p = ((amt/val)*100).toFixed(1);
const div = document.createElement("div");
div.className="expentry";
div.innerHTML = ` <strong>${njk}. ${src}</strong> : ₹ ${amt.toLocaleString()} (${p}%)<br>`;
sourcesDiv.appendChild(div);
njk++;
}
sourcesDiv.style.display = "block";
}
});
i++;
}
}
function drawMonthly(m){
let entries = Object.entries(m);
// ✅ Sort months (important)
entries.sort((a,b)=> new Date(a[0]) - new Date(b[0]));
// ✅ Show only last 8 months (mobile friendly)
let last = entries.slice(-8);
let labels = last.map(x=>x[0]);
let income = last.map(x=>x[1].income);
let expense = last.map(x=>x[1].expense);
setCanvasWidth("monthChart", labels.length);
new Chart(document.getElementById("monthChart"),{
type:'bar',
data:{
labels:labels,
datasets:[
{
label:"Income",
data:income,
borderRadius:6,
barPercentage:0.6
},
{
label:"Expense",
data:expense,
borderRadius:6,
barPercentage:0.6
}
]
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{
position:"bottom",
labels:{color:accent}
}
},
scales:{
x:{
ticks:{
color:"#aaa",
maxRotation:0,
minRotation:0
}
},
y:{
ticks:{color:"#aaa"}
}
}
}
});
}
function drawCategory(c){
let entries = Object.entries(c);
entries.sort((a,b)=>b[1]-a[1]);
let top = entries.slice(0,8);
let others = entries.slice(8);
let otherSum = others.reduce((s,x)=>s+x[1],0);
if(otherSum > 0){
top.push(["Others", otherSum]);
}
let labels = top.map(x=>x[0]);
let values = top.map(x=>x[1]);
new Chart(document.getElementById("catChart"),{
type:'doughnut',
data:{
labels:labels,
datasets:[{data:values}]
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{
position:"bottom",
labels:{color:accent, boxWidth:10}
}
}
}
});
}
function drawTrend(t){
let entries = Object.entries(t);
// ✅ Sort dates properly
entries.sort((a,b)=> new Date(a[0]) - new Date(b[0]));
// ✅ If too many points → compress (important)
if(entries.length > 40){
let grouped = {};
entries.forEach(([date,val])=>{
let d = new Date(date);
let key = d.getFullYear() + "-" + (d.getMonth()+1); // group by month
if(!grouped[key]) grouped[key]=0;
grouped[key]+=val;
});
entries = Object.entries(grouped);
}
// ✅ Show last 20 points (mobile friendly)
let last = entries.slice(-20);
let labels = last.map(x=>x[0]);
let values = last.map(x=>x[1]);
setCanvasWidth("trendChart", labels.length);
new Chart(document.getElementById("trendChart"),{
type:'line',
data:{
labels:labels,
datasets:[{
label:"Cash Flow",
data:values,
tension:0.4, // smooth curve
fill:false,
pointRadius:2
}]
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{
position:"bottom",
labels:{color:accent}
}
},
scales:{
x:{
ticks:{
color:accent,
maxRotation:45,
minRotation:20
}
},
y:{
ticks:{color:"#aaa"}
}
}
}
});
}
function drawInsights(data){
let daily = {};
// ✅ collect daily expense
data.forEach(d=>{
d.transactions.forEach(t=>{
if(!daily[t.date]) daily[t.date]=0;
daily[t.date]+=t.amount;
});
});
let entries = Object.entries(daily);
if(entries.length === 0) return;
// ✅ highest day
entries.sort((a,b)=>b[1]-a[1]);
let highest = entries[0];
document.getElementById("highDay").innerText =
highest[0] + " (₹ " + highest[1].toLocaleString() + ")";
// ✅ average spend
let total = entries.reduce((s,x)=>s+x[1],0);
let avg = total / entries.length;
document.getElementById("avgSpend").innerText =
"₹ " + Math.round(avg).toLocaleString();
// ✅ top 5 days chart
let top = entries.slice(0,5);
let labels = top.map(x=>x[0]);
let values = top.map(x=>x[1]);
setCanvasWidth("topDaysChart", labels.length);
new Chart(document.getElementById("topDaysChart"),{
type:'bar',
data:{
labels:labels,
datasets:[{
label:"Top Spending Days",
data:values,
borderRadius:6
}]
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{
position:"bottom",
labels:{color:accent}
}
},
scales:{
x:{ticks:{color:"#aaa"}},
y:{ticks:{color:"#aaa"}}
}
}
});
}
function drawCategoryTrend(data){
let monthlyCat = {};
// ✅ Build monthly category data
data.forEach(d=>{
let m = formatMonth(d.date);
if(!monthlyCat[m]) monthlyCat[m] = {};
d.transactions.forEach(t=>{
let cat = getCategory(t);
if(!monthlyCat[m][cat]) monthlyCat[m][cat] = 0;
monthlyCat[m][cat] += t.amount;
});
});
// ✅ Sort months
let months = Object.keys(monthlyCat).sort((a,b)=> new Date(a)-new Date(b));
// ✅ Get top 3 categories overall (avoid clutter)
let totalCat = {};
Object.values(monthlyCat).forEach(m=>{
Object.entries(m).forEach(([cat,val])=>{
if(!totalCat[cat]) totalCat[cat]=0;
totalCat[cat]+=val;
});
});
let topCats = Object.entries(totalCat)
.sort((a,b)=>b[1]-a[1])
.slice(0,10) // ✅ Top 10
.map(x=>x[0]);
// ✅ Build datasets
let datasets = topCats.map((cat,i)=>{
return {
label: cat,
data: months.map(m=> monthlyCat[m][cat] || 0),
tension:0.4,
borderWidth:2,
pointRadius:1
};
});
setCanvasWidth("catTrendChart", months.length);
// destroy old
if(window.catTrendChart && typeof window.catTrendChart.destroy === "function"){
window.catTrendChart.destroy();
}
window.catTrendChart = new Chart(document.getElementById("catTrendChart"),{
type:'line',
data:{
labels:months,
datasets:datasets
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{
position:"bottom",
labels:{
color:accent,
boxWidth:10,
font:{size:10} // smaller text
}
}
},
scales:{
x:{
ticks:{color:"#aaa"}
},
y:{
ticks:{color:"#aaa"}
}
}
}
});
}
function drawEfficiency(income, expense){
if(income === 0){
document.getElementById("effMsg").innerText = "No income data";
return;
}
let savings = income - expense;
let rate = (savings / income) * 100;
document.getElementById("savingsVal").innerText =
"₹ " + savings.toLocaleString();
document.getElementById("savingsRate").innerText =
rate.toFixed(1) + "%";
// ✅ Smart message
let msg = "";
if(rate >= 40){
msg = "🔥 Excellent saving habit";
}else if(rate >= 20){
msg = "👍 Good, but can improve";
}else if(rate >= 0){
msg = "⚠️ Low savings, control spending";
}else{
msg = "🚨 You are spending more than earning";
}
document.getElementById("effMsg").innerText = msg;
}
function drawFinancialScore(income, expense, category){
if(income === 0){
document.getElementById("finMsg").innerText = "No income data";
return;
}
let savings = income - expense;
let savingsRate = (savings / income) * 100;
// ✅ Score calculation
let score = 0;
// 1. Savings weight (50 points)
if(savingsRate >= 40) score += 50;
else if(savingsRate >= 20) score += 35;
else if(savingsRate >= 10) score += 20;
else if(savingsRate >= 0) score += 10;
else score += 0;
// 2. Spending control (30 points)
let totalExpense = Object.values(category).reduce((s,x)=>s+x,0);
let maxCat = Math.max(...Object.values(category));
let concentration = (maxCat / totalExpense) * 100;
if(concentration < 30) score += 30;
else if(concentration < 50) score += 20;
else score += 10;
// 3. Expense ratio (20 points)
let expenseRatio = (expense / income) * 100;
if(expenseRatio < 60) score += 20;
else if(expenseRatio < 80) score += 10;
else score += 5;
// ✅ Final score
score = Math.round(score);
document.getElementById("finScore").innerText = score + " / 100";
// ✅ Health label
let health = "";
let msg = "";
if(score >= 80){
health = "Excellent 🔥";
msg = "You are financially strong";
}else if(score >= 60){
health = "Good 👍";
msg = "You are doing well, small improvements needed";
}else if(score >= 40){
health = "Average ⚠️";
msg = "Try to control spending and save more";
}else{
health = "Poor 🚨";
msg = "High risk! Improve savings immediately";
}
document.getElementById("finHealth").innerText = health;
document.getElementById("finMsg").innerText = msg;
// ✅ Chart (visual score)
if(window.scoreChart && typeof window.scoreChart.destroy === "function"){
window.scoreChart.destroy();
}
window.scoreChart = new Chart(document.getElementById("scoreChart"),{
type:'doughnut',
data:{
labels:["Score","Remaining"],
datasets:[{
data:[score, 100-score]
}]
},
options:{
responsive:true,
maintainAspectRatio:false,
plugins:{
legend:{display:false}
}
}
});
}
function drawGrowth(data){
let timeline = [];
// ✅ collect balance over time
data.forEach(d=>{