-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2666 lines (2351 loc) · 115 KB
/
Copy pathindex.html
File metadata and controls
2666 lines (2351 loc) · 115 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 charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bihar Electoral Trends - Format 1B Analysis</title>
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<script src="utils/d3_v790.js"></script>
<style>
@page {
size: A4 portrait;
margin: 15mm;
}
@media print {
body { margin: 0; }
.no-print { display: none; }
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f7fa;
}
.container {
max-width: 1400px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h1 {
color: #2c3e50;
text-align: center;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
text-align: center;
color: #7f8c8d;
margin-bottom: 30px;
font-size: 14px;
}
.controls {
display: flex;
justify-content: center;
gap: 20px;
margin-bottom: 30px;
padding: 15px;
background: #ecf0f1;
border-radius: 6px;
}
.control-group {
display: flex;
align-items: center;
gap: 10px;
}
label {
font-weight: 600;
color: #34495e;
}
select {
padding: 8px 12px;
border: 2px solid #bdc3c7;
border-radius: 4px;
background: white;
font-size: 14px;
cursor: pointer;
min-width: 150px;
}
select:hover {
border-color: #3498db;
}
.charts-grid {
display: grid;
grid-template-columns: 1fr;
gap: 30px;
margin-top: 20px;
}
.chart-container {
background: #fafafa;
padding: 20px;
border-radius: 6px;
border: 1px solid #e1e8ed;
}
.chart-title {
font-size: 18px;
font-weight: 600;
color: #2c3e50;
margin-bottom: 15px;
text-align: center;
}
.local-filters {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 20px;
padding: 10px;
background-color: #f8f9fa;
border-radius: 4px;
}
.local-filters select {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
background-color: white;
cursor: pointer;
}
.chart {
display: flex;
justify-content: center;
}
.line {
fill: none;
stroke-width: 2.5;
}
.line:hover {
stroke-width: 4;
}
.axis path,
.axis line {
stroke: #95a5a6;
}
.axis text {
fill: #34495e;
font-size: 12px;
}
.grid line {
stroke: #ecf0f1;
stroke-opacity: 0.7;
}
.tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.85);
color: white;
padding: 10px 15px;
border-radius: 6px;
font-size: 14px;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s;
z-index: 1000;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}
.tooltip.show {
opacity: 1;
}
.legend {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 15px;
margin-top: 20px;
font-size: 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 30px;
height: 3px;
}
.info-box {
background: #e8f4f8;
border-left: 4px solid #3498db;
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
}
.info-box h3 {
margin: 0 0 8px 0;
color: #2980b9;
font-size: 16px;
}
.info-box p {
margin: 0;
color: #34495e;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h1>Bihar Electoral Data Analysis (30.09.2025)</h1>
<div class="info-box">
<h3>Understanding the Data</h3>
<p>Each chart has its own dropdown filters. Select different districts or constituencies to explore the data independently for each visualization.</p>
</div>
<div class="charts-grid">
<div class="chart-container">
<div class="chart-title" id="title-combined">Combined Chart</div>
<div class="local-filters">
<select id="viewType-combined">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-combined"></select>
</div>
<div id="chart-combined" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-total">Total Voters Trend (Male + Female + Third Gender)</div>
<div class="local-filters">
<select id="viewType-total">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-total"></select>
</div>
<div id="chart-total" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-male">Male Voters Trend</div>
<div class="local-filters">
<select id="viewType-male">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-male"></select>
</div>
<div id="chart-male" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-female">Female Voters Trend</div>
<div class="local-filters">
<select id="viewType-female">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-female"></select>
</div>
<div id="chart-female" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-third">Third Gender Voters Trend</div>
<div class="local-filters">
<select id="viewType-third">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-third"></select>
</div>
<div id="chart-third" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-ratio">Gender Ratio Trend</div>
<div class="local-filters">
<select id="viewType-ratio">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-ratio"></select>
</div>
<div id="chart-ratio" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-donut">Bihar Gender Ratio: Census (2011) vs Electoral Roll (2025)</div>
<div class="subtitle" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;">Difference: 26 (Electoral Roll lower by 26)</div>
<div id="chart-donut" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-population">Bihar: Projected Population (2025) vs Electors (2025)</div>
<div class="subtitle" id="subtitle-population" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-population" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-age-breakdown">Age-Wise Breakdown</div>
<div class="local-filters">
<select id="viewType-age">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-age"></select>
</div>
<div class="subtitle" id="subtitle-age-breakdown" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-age-breakdown" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-claims-chart">Total Claims: Lodged vs Admitted (Form 6)</div>
<div class="local-filters">
<select id="viewType-claims">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-claims"></select>
</div>
<div class="subtitle" id="subtitle-claims-chart" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-claims" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-deletions-chart">Voter Deletions Breakdown</div>
<div class="local-filters">
<select id="viewType-deletions">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-deletions"></select>
</div>
<div class="subtitle" id="subtitle-deletions-chart" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-deletions" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-epic-chart">EPIC Card Coverage and Electoral Roll Photos</div>
<div class="local-filters">
<select id="viewType-epic">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-epic"></select>
</div>
<div class="subtitle" id="subtitle-epic-chart" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-epic" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-service-voters-chart">Service Voters Analysis</div>
<div class="local-filters">
<select id="viewType-service-voters">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-service-voters"></select>
</div>
<div class="subtitle" id="subtitle-service-voters-chart" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-service-voters" class="chart"></div>
</div>
<div class="chart-container">
<div class="chart-title" id="title-shifted-electors-chart">Shifted Electors and Deletions</div>
<div class="local-filters">
<select id="viewType-shifted-electors">
<option value="district">View by District</option>
<option value="constituency">View by Constituency</option>
</select>
<select id="selected-shifted-electors"></select>
</div>
<div class="subtitle" id="subtitle-shifted-electors-chart" style="text-align: center; color: #7f8c8d; margin-top: -10px; margin-bottom: 15px; font-size: 14px;"></div>
<div id="chart-shifted-electors" class="chart"></div>
</div>
</div>
</div>
<div class="tooltip" id="tooltip"></div>
<script>
// District data from Format_1B_03.csv
// Load data from JSON file
let districtData = [];
let constituencyData = [];
let stateTotal = null;
let stateStatsData = [];
let genderRatioData = [];
let districtAgeData = [];
let stateAgeData = [];
let constituencyClaimsData = [];
let districtClaimsData = [];
let stateClaimsData = {};
let constituencyServiceVotersData = [];
let districtServiceVotersData = [];
let constituencyShiftedElectorsData = [];
let districtShiftedElectorsData = [];
// Load data from JSON file
fetch('Bihar_DataAnalysis.json')
.then(response => response.json())
.then(data => {
districtData = data.districtData;
constituencyData = data.constituencyData;
stateTotal = data.stateTotal;
stateStatsData = data.stateStats;
genderRatioData = data.genderRatioComparison;
districtAgeData = data.districtAgeData;
stateAgeData = data.stateAgeData;
constituencyClaimsData = data.constituencyClaimsData;
districtClaimsData = data.districtClaimsData;
stateClaimsData = data.stateClaimsData;
constituencyServiceVotersData = data.constituencyServiceVotersData || [];
districtServiceVotersData = data.districtServiceVotersData || [];
constituencyShiftedElectorsData = data.constituencyShiftedElectorsData || [];
districtShiftedElectorsData = data.districtShiftedElectorsData || [];
console.log('Data loaded:', {
districts: districtData.length,
constituencies: constituencyData.length,
stateStats: stateStatsData,
genderRatio: genderRatioData,
districtAge: districtAgeData.length,
stateAge: stateAgeData.length,
constituentyClaims: constituencyClaimsData.length,
districtClaims: districtClaimsData.length,
constituencyServiceVoters: constituencyServiceVotersData.length,
districtServiceVoters: districtServiceVotersData.length,
constituencyShiftedElectors: constituencyShiftedElectorsData.length,
districtShiftedElectors: districtShiftedElectorsData.length
});
// Initial setup after data loads - populate all dropdowns
initializeAllDropdowns();
// Draw all charts with initial state
drawChart('combined');
drawChart('total');
drawChart('male');
drawChart('female');
drawChart('third');
drawChart('ratio');
drawGenderRatioBarChart('#chart-donut');
drawStateStatsChart('#chart-population');
drawAgeBreakdownChart();
drawClaimsChart();
drawDeletionsChart();
drawEPICChart();
drawServiceVotersChart();
drawShiftedElectorsChart();
})
.catch(error => {
console.error('Error loading data:', error);
alert('Error loading data. Please ensure Bihar_DataAnalysis.json is in the same folder.');
});
const timePoints = ['1st January 2025\n(Final Roll)', '1st July 2025\n(Draft Roll)', '1st July 2025\n(Final Roll)'];
// Separate filters for each chart
let filters = {
combined: { view: 'district', filter: 'all' },
total: { view: 'district', filter: 'all' },
male: { view: 'district', filter: 'all' },
female: { view: 'district', filter: 'all' },
third: { view: 'district', filter: 'all' },
ratio: { view: 'district', filter: 'all' },
age: { view: 'district', filter: 'all' },
claims: { view: 'district', filter: 'all' },
deletions: { view: 'district', filter: 'all' },
epic: { view: 'district', filter: 'all' },
'service-voters': { view: 'district', filter: 'all' },
'shifted-electors': { view: 'district', filter: 'all' }
};
// Color scale
const colorScale = d3.scaleOrdinal(d3.schemeCategory10);
// Chart dimensions
const margin = {top: 20, right: 100, bottom: 80, left: 120};
const width = 1000 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;
// Tooltip
const tooltip = d3.select('#tooltip');
// Indian numbering system formatter
function formatIndianNumber(value) {
if (value >= 10000000) {
// Crores (1 Crore = 10 million)
return (value / 10000000).toFixed(1) + ' Crores';
} else if (value >= 100000) {
// Lakhs (1 Lakh = 100 thousand)
return (value / 100000).toFixed(1) + ' Lakhs';
} else if (value >= 1000) {
// Thousands
return (value / 1000).toFixed(1) + ' Thousands';
} else {
return value.toString();
}
}
// Format number with Indian comma system (lakhs and crores)
function formatIndianCommas(num) {
// Handle negative numbers
const isNegative = num < 0;
const absNum = Math.abs(num);
const numStr = absNum.toString();
let result = '';
let count = 0;
// Process from right to left
for (let i = numStr.length - 1; i >= 0; i--) {
if (count === 3 || (count > 3 && (count - 3) % 2 === 0)) {
result = ',' + result;
}
result = numStr[i] + result;
count++;
}
return isNegative ? '-' + result : result;
}
function drawGenderRatioBarChart(selector) {
if (!genderRatioData || genderRatioData.length === 0) return;
const barData = genderRatioData.map((d, i) => ({
label: d.label,
value: d.value,
color: ['#3498db', '#e74c3c'][i]
}));
d3.select(selector).selectAll('*').remove();
const svg = d3.select(selector)
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Scales
const x = d3.scaleBand()
.domain(barData.map(d => d.label))
.range([0, width])
.padding(0.3);
const y = d3.scaleLinear()
.domain([0, d3.max(barData, d => d.value) * 1.1])
.range([height, 0]);
// Grid
svg.append('g')
.attr('class', 'grid')
.call(d3.axisLeft(y).tickSize(-width).tickFormat(''));
// Axes
svg.append('g')
.attr('class', 'axis')
.attr('transform', `translate(0,${height})`)
.call(d3.axisBottom(x))
.selectAll('text')
.attr('font-size', '13px')
.attr('fill', '#34495e');
svg.append('g')
.attr('class', 'axis')
.call(d3.axisLeft(y));
// Y-axis label
svg.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 0 - margin.left + 20)
.attr('x', 0 - (height / 2))
.attr('dy', '1em')
.style('text-anchor', 'middle')
.style('font-size', '14px')
.style('fill', '#34495e')
.text('Females per 1000 Males');
// Bars
svg.selectAll('.bar')
.data(barData)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('x', d => x(d.label))
.attr('width', x.bandwidth())
.attr('y', d => y(d.value))
.attr('height', d => height - y(d.value))
.attr('fill', d => d.color)
.attr('opacity', 0.85)
.on('mouseover', function(event, d) {
d3.select(this)
.transition()
.duration(200)
.attr('opacity', 1);
tooltip.classed('show', true)
.html(`
<strong>${d.label}</strong><br/>
Ratio: <strong>${d.value}</strong> females per 1000 males
`)
.style('left', (event.pageX + 10) + 'px')
.style('top', (event.pageY - 10) + 'px');
})
.on('mouseout', function() {
d3.select(this)
.transition()
.duration(200)
.attr('opacity', 0.85);
tooltip.classed('show', false);
});
// Value labels on top of bars
svg.selectAll('.label')
.data(barData)
.enter()
.append('text')
.attr('class', 'label')
.attr('x', d => x(d.label) + x.bandwidth() / 2)
.attr('y', d => y(d.value) - 10)
.attr('text-anchor', 'middle')
.attr('font-size', '16px')
.attr('font-weight', 'bold')
.attr('fill', '#2c3e50')
.text(d => d.value);
}
function drawStateStatsChart(selector) {
if (!stateStatsData || stateStatsData.length === 0) return;
// Separate ratio from bar data
const ratioData = stateStatsData.find(d => d.metric === 'Elector-Population Ratio');
const chartData = stateStatsData
.filter(d => d.metric !== 'Elector-Population Ratio')
.map((d, i) => ({
label: d.metric,
value: d.value,
color: ['#3498db', '#e74c3c'][i]
}));
// Update subtitle with ratio information
if (ratioData) {
d3.select('#subtitle-population')
.text(`Elector-Population Ratio: ${ratioData.value.toFixed(3)} (56.6% of projected population are registered electors)`);
}
// Reuse the bar chart structure
d3.select(selector).selectAll('*').remove();
const svg = d3.select(selector)
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const x = d3.scaleBand().domain(chartData.map(d => d.label)).range([0, width]).padding(0.3);
const y = d3.scaleLinear().domain([0, d3.max(chartData, d => d.value) * 1.1]).range([height, 0]);
svg.append('g').attr('class', 'grid').call(d3.axisLeft(y).tickSize(-width).tickFormat(''));
svg.append('g').attr('class', 'axis').attr('transform', `translate(0,${height})`).call(d3.axisBottom(x));
svg.append('g').attr('class', 'axis').call(d3.axisLeft(y).tickFormat(formatIndianNumber));
svg.selectAll('.bar').data(chartData).enter().append('rect')
.attr('x', d => x(d.label)).attr('width', x.bandwidth())
.attr('y', d => y(d.value)).attr('height', d => height - y(d.value))
.attr('fill', d => d.color).attr('opacity', 0.85)
.on('mouseover', function(event, d) {
d3.select(this).attr('opacity', 1);
tooltip.classed('show', true)
.html(`<strong>${d.label}</strong><br/>Value: <strong>${formatIndianCommas(d.value)}</strong>`)
.style('left', (event.pageX + 10) + 'px').style('top', (event.pageY - 10) + 'px');
})
.on('mouseout', function() {
d3.select(this).attr('opacity', 0.85);
tooltip.classed('show', false);
});
svg.selectAll('.label').data(chartData).enter().append('text')
.attr('x', d => x(d.label) + x.bandwidth() / 2).attr('y', d => y(d.value) - 10)
.attr('text-anchor', 'middle').attr('font-size', '16px').attr('font-weight', 'bold').attr('fill', '#2c3e50')
.text(d => (d.value >= 10000000 ? (d.value / 10000000).toFixed(1) + ' Cr' : formatIndianCommas(d.value)));
}
function drawAgeBreakdownChart() {
const selector = '#chart-age-breakdown';
const currentView = filters.age.view;
const currentFilter = filters.age.filter;
// Determine which data to show based on current view and filter
let ageData = [];
let titleSuffix = '';
let locationName = '';
if (currentFilter === 'all') {
// Show state-level data when "All Districts" or "All Constituencies" is selected
ageData = stateAgeData;
titleSuffix = 'Bihar State';
locationName = 'Bihar';
} else if (currentView === 'district') {
// Show specific district data
const districtAge = districtAgeData.find(d => d.district === currentFilter);
if (districtAge) {
ageData = districtAge.age_breakdown;
titleSuffix = currentFilter;
locationName = currentFilter;
} else {
ageData = stateAgeData;
titleSuffix = 'Bihar State';
locationName = 'Bihar';
}
} else {
// Constituency view - for now use state data as placeholder
// TODO: Replace with constituency-specific data when available
ageData = stateAgeData;
titleSuffix = currentFilter + ' (Constituency data pending)';
locationName = currentFilter;
}
// Update title and remove subtitle
d3.select('#title-age-breakdown').text(`Age-Bracket wise breakdown - ${titleSuffix}`);
d3.select('#subtitle-age-breakdown').text('');
if (!ageData || ageData.length === 0) {
console.warn('No age data available');
return;
}
// Prepare data for grouped bar chart
const chartData = ageData.map(d => ({
age_cohort: d.age_cohort,
projected_population: d.projected_population,
electors: d.electors
}));
// Clear existing chart
d3.select(selector).selectAll('*').remove();
// Create SVG
const svg = d3.select(selector)
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Scales
const x0 = d3.scaleBand()
.domain(chartData.map(d => d.age_cohort))
.range([0, width])
.padding(0.2);
const x1 = d3.scaleBand()
.domain(['projected_population', 'electors'])
.range([0, x0.bandwidth()])
.padding(0.05);
const maxValue = d3.max(chartData, d => Math.max(d.projected_population, d.electors));
const y = d3.scaleLinear()
.domain([0, maxValue * 1.1])
.range([height, 0]);
const colorScale = d3.scaleOrdinal()
.domain(['projected_population', 'electors'])
.range(['#3498db', '#e74c3c']);
// Grid
svg.append('g')
.attr('class', 'grid')
.call(d3.axisLeft(y).tickSize(-width).tickFormat(''));
// Axes
svg.append('g')
.attr('class', 'axis')
.attr('transform', `translate(0,${height})`)
.call(d3.axisBottom(x0))
.selectAll('text')
.style('font-size', '12px')
.style('font-weight', '600');
svg.append('g')
.attr('class', 'axis')
.call(d3.axisLeft(y).tickFormat(formatIndianNumber));
// Create bar groups
const ageGroups = svg.selectAll('.age-group')
.data(chartData)
.enter()
.append('g')
.attr('class', 'age-group')
.attr('transform', d => `translate(${x0(d.age_cohort)},0)`);
// Draw bars
ageGroups.selectAll('rect')
.data(d => [
{key: 'projected_population', value: d.projected_population, age: d.age_cohort},
{key: 'electors', value: d.electors, age: d.age_cohort}
])
.enter()
.append('rect')
.attr('x', d => x1(d.key))
.attr('y', d => y(d.value))
.attr('width', x1.bandwidth())
.attr('height', d => height - y(d.value))
.attr('fill', d => colorScale(d.key))
.attr('opacity', 0.85)
.on('mouseover', function(event, d) {
d3.select(this).attr('opacity', 1);
const label = d.key === 'projected_population' ? 'Projected Population' : 'Registered Electors';
tooltip.classed('show', true)
.html(`<strong>${locationName} - Age ${d.age}</strong><br/>${label}: <strong>${formatIndianCommas(d.value)}</strong>`)
.style('left', (event.pageX + 10) + 'px')
.style('top', (event.pageY - 10) + 'px');
})
.on('mouseout', function() {
d3.select(this).attr('opacity', 0.85);
tooltip.classed('show', false);
});
// Add value labels on top of bars
ageGroups.selectAll('.value-label')
.data(d => [
{key: 'projected_population', value: d.projected_population, age: d.age_cohort},
{key: 'electors', value: d.electors, age: d.age_cohort}
])
.enter()
.append('text')
.attr('class', 'value-label')
.attr('x', d => x1(d.key) + x1.bandwidth() / 2)
.attr('y', d => y(d.value) - 5)
.attr('text-anchor', 'middle')
.attr('font-size', '10px')
.attr('font-weight', 'bold')
.attr('fill', '#2c3e50')
.text(d => {
if (d.value >= 10000000) {
return (d.value / 10000000).toFixed(1) + 'Cr';
} else if (d.value >= 100000) {
return (d.value / 100000).toFixed(1) + 'L';
} else if (d.value >= 1000) {
return (d.value / 1000).toFixed(1) + 'K';
}
return d.value;
});
// Add legend
const legend = svg.append('g')
.attr('class', 'legend')
.attr('transform', `translate(${width - 200}, 20)`);
const legendData = [
{label: 'Projected Population (18+ only)', key: 'projected_population'},
{label: 'Registered Electors', key: 'electors'}
];
legendData.forEach((item, i) => {
const legendRow = legend.append('g')
.attr('transform', `translate(0, ${i * 25})`);
legendRow.append('rect')
.attr('width', 15)
.attr('height', 15)
.attr('fill', colorScale(item.key))
.attr('opacity', 0.85);
legendRow.append('text')
.attr('x', 20)
.attr('y', 12)
.attr('font-size', '12px')
.attr('fill', '#2c3e50')
.text(item.label);
});
// Calculate totals for display
const totalProjectedPopulation = chartData.reduce((sum, d) => sum + d.projected_population, 0);
const totalElectors = chartData.reduce((sum, d) => sum + d.electors, 0);
// Create breakdown strings
const popBreakdown = chartData.map(d => formatIndianCommas(d.projected_population)).join(' + ');
const electorsBreakdown = chartData.map(d => formatIndianCommas(d.electors)).join(' + ');
// Add summary text below the chart
const summaryY = height + margin.bottom - 30;
// Projected Population line
svg.append('text')
.attr('x', 0)
.attr('y', summaryY)
.attr('font-size', '12px')
.attr('fill', '#3498db')
.attr('font-weight', 'bold')
.text('Projected Population (18+ only): ');
svg.append('text')
.attr('x', 195)
.attr('y', summaryY)
.attr('font-size', '12px')
.attr('fill', '#2c3e50')
.text(`${popBreakdown} = ${formatIndianCommas(totalProjectedPopulation)}`);
// Registered Electors line
svg.append('text')
.attr('x', 0)
.attr('y', summaryY + 18)
.attr('font-size', '12px')
.attr('fill', '#e74c3c')
.attr('font-weight', 'bold')
.text('Registered Electors: ');
svg.append('text')
.attr('x', 135)
.attr('y', summaryY + 18)
.attr('font-size', '12px')
.attr('fill', '#2c3e50')
.text(`${electorsBreakdown} = ${formatIndianCommas(totalElectors)}`);
}
function drawClaimsChart() {
const selector = '#chart-claims';
const currentView = filters.claims.view;
const currentFilter = filters.claims.filter;
// Determine which data to show based on current view and filter
let claimsData = [];
let titleSuffix = '';
if (currentFilter === 'all') {
// Show state-level totals for both "All Districts" and "All Constituencies"
titleSuffix = 'Bihar State';
claimsData = [
{category: 'Claims Lodged (Form 6)', value: stateClaimsData.claims_lodged},
{category: 'Claims Admitted (Form 6)', value: stateClaimsData.claims_admitted},
{category: 'Objections Lodged (Form 7)', value: stateClaimsData.objections_lodged},
{category: 'Objections Admitted (Form 7)', value: stateClaimsData.objections_admitted}
];
} else if (currentView === 'district') {
// Show specific district data
titleSuffix = currentFilter;
const districtData = districtClaimsData.find(d => d.district === currentFilter);
if (districtData) {
claimsData = [
{category: 'Claims Lodged (Form 6)', value: districtData.claims_lodged},
{category: 'Claims Admitted (Form 6)', value: districtData.claims_admitted},
{category: 'Objections Lodged (Form 7)', value: districtData.objections_lodged},
{category: 'Objections Admitted (Form 7)', value: districtData.objections_admitted}
];
}
} else if (currentView === 'constituency') {
// Show specific constituency data
titleSuffix = currentFilter;
const constituency = constituencyClaimsData.find(c => c.constituency_name === currentFilter);
if (constituency) {
claimsData = [
{category: 'Claims Lodged (Form 6)', value: constituency.claims_lodged},
{category: 'Claims Admitted (Form 6)', value: constituency.claims_admitted},
{category: 'Objections Lodged (Form 7)', value: constituency.objections_lodged},
{category: 'Objections Admitted (Form 7)', value: constituency.objections_admitted}
];
}
}
// Update title
d3.select('#title-claims-chart').text(`Total Claims: Lodged vs Admitted (Form 6) - ${titleSuffix}`);
d3.select('#subtitle-claims-chart').text('');
if (!claimsData || claimsData.length === 0) {
console.warn('No claims data available');
return;
}
// Clear existing chart
d3.select(selector).selectAll('*').remove();
// Create SVG
const svg = d3.select(selector)
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
// Scales
const x = d3.scaleBand()
.domain(claimsData.map(d => d.category))
.range([0, width])
.padding(0.3);
const maxValue = d3.max(claimsData, d => d.value);
const y = d3.scaleLinear()