statewise-chart-processor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +212 -0
  2. package/package.json +9 -0
package/index.js ADDED
@@ -0,0 +1,212 @@
1
+ async function processstatewisechart(parameterData, chartData, tablewiseChartData) {
2
+ // ...your function code here...
3
+ if (!chartData || chartData.length === 0) return {};
4
+
5
+ let reportHeaderObj = {};
6
+ chartData.forEach(item => {
7
+ if (item.report_header && item.report_order !== undefined) {
8
+ reportHeaderObj[item.report_order] = item.report_header;
9
+ }
10
+ });
11
+
12
+ let reportHeader = Object.values(
13
+ Object.keys(reportHeaderObj)
14
+ .sort((a, b) => a - b)
15
+ .reduce((acc, key) => {
16
+ acc[key] = reportHeaderObj[key];
17
+ return acc;
18
+ }, {})
19
+ );
20
+
21
+ let tableheaders = [...reportHeader];
22
+
23
+ let areatypeArr = [
24
+ ...new Set(chartData.map(x => x.area_type).filter(a => a))
25
+ ];
26
+
27
+ areatypeArr.sort();
28
+
29
+ const areaarray = ["rural", "urban", "combined"];
30
+ if (
31
+ areatypeArr.length > 1 &&
32
+ areaarray.every(a => areatypeArr.includes(a))
33
+ ) {
34
+ areatypeArr = areaarray;
35
+ }
36
+
37
+ let areatype = [...areatypeArr];
38
+
39
+ let finaldata = { State: {} };
40
+ let grandtotal = { grandTotal: { finaltotal: 0 } };
41
+
42
+ // ---------------------------
43
+ // MAIN LOOP
44
+ // ---------------------------
45
+ for (let row of chartData) {
46
+ if (row.disttName === "(NULL)") continue;
47
+ if (!reportHeader.includes(row.report_header)) continue;
48
+
49
+ if (!finaldata.State[row.State]) {
50
+ finaldata.State[row.State] = { State: row.State, disttName: row.disttName };
51
+ }
52
+
53
+ if (!finaldata.State[row.State][row.disttName]) {
54
+ finaldata.State[row.State][row.disttName] = { disttName: row.disttName };
55
+ }
56
+
57
+ if (!finaldata.State[row.State][row.disttName][row.area_type]) {
58
+ finaldata.State[row.State][row.disttName][row.area_type] = {
59
+ report_header: []
60
+ };
61
+ }
62
+
63
+ finaldata.State[row.State][row.disttName][row.area_type].report_header.push({
64
+ name: row.report_header,
65
+ total: row.total
66
+ });
67
+
68
+ // grandTotal by area type + header
69
+ if (!grandtotal.grandTotal[row.area_type])
70
+ grandtotal.grandTotal[row.area_type] = {};
71
+
72
+ if (!grandtotal.grandTotal[row.area_type][row.report_header])
73
+ grandtotal.grandTotal[row.area_type][row.report_header] = { total: 0 };
74
+
75
+ grandtotal.grandTotal[row.area_type][row.report_header].total += row.total;
76
+
77
+ // final total
78
+ grandtotal.grandTotal.finaltotal += row.total;
79
+ }
80
+
81
+ // remove first areatype from grandTotal
82
+ delete grandtotal.grandTotal[areatype[0]];
83
+ grandtotal.grandTotal.finaltotal = 0;
84
+
85
+ if (areatype.length > 1) delete grandtotal.grandTotal[areatype[1]];
86
+
87
+ // ---------------------------
88
+ // PROCESS EACH DISTRICT
89
+ // ---------------------------
90
+ for (let stateKey in finaldata.State) {
91
+ const stateBlock = finaldata.State[stateKey];
92
+
93
+ for (let distKey in stateBlock) {
94
+ const distBlock = stateBlock[distKey];
95
+ if (!distBlock || typeof distBlock !== "object") continue;
96
+
97
+ // RURAL
98
+ if (distBlock[areatype[0]]) {
99
+ let ruralTotals = Object.fromEntries(
100
+ distBlock[areatype[0]].report_header.map(h => [h.name, h.total])
101
+ );
102
+
103
+ distBlock[areatype[0]].report_header = [];
104
+ distBlock[areatype[0]].areatotal = 0;
105
+
106
+ for (let header of tableheaders) {
107
+ let total = ruralTotals[header] || 0;
108
+ distBlock[areatype[0]].report_header.push({ name: header, total });
109
+
110
+ distBlock[areatype[0]].areatotal += total;
111
+ }
112
+ } else {
113
+ // no rural data
114
+ distBlock[areatype[0]] = { report_header: [], areatotal: 0 };
115
+ for (let header of tableheaders) {
116
+ distBlock[areatype[0]].report_header.push({ name: header, total: 0 });
117
+ }
118
+ }
119
+
120
+ // URBAN
121
+ if (areatype.length > 1) {
122
+ if (distBlock[areatype[1]]) {
123
+ let urbanTotals = Object.fromEntries(
124
+ distBlock[areatype[1]].report_header.map(h => [h.name, h.total])
125
+ );
126
+
127
+ distBlock[areatype[1]].report_header = [];
128
+ distBlock[areatype[1]].areatotal = 0;
129
+
130
+ for (let header of tableheaders) {
131
+ let total = urbanTotals[header] || 0;
132
+ distBlock[areatype[1]].report_header.push({ name: header, total });
133
+ distBlock[areatype[1]].areatotal += total;
134
+
135
+ // combined total
136
+ let ruralTotal =
137
+ distBlock[areatype[0]].report_header.find(h => h.name === header)?.total || 0;
138
+ let combinedTotal = ruralTotal + total;
139
+
140
+ if (!distBlock.combined) distBlock.combined = { report_header: [] };
141
+
142
+ distBlock.combined.report_header.push({
143
+ name: header,
144
+ total: combinedTotal
145
+ });
146
+ }
147
+ } else {
148
+ // no urban → fill zero + combined = rural
149
+ distBlock[areatype[1]] = { report_header: [], areatotal: 0 };
150
+ distBlock.combined = { report_header: [] };
151
+
152
+ for (let header of tableheaders) {
153
+ distBlock[areatype[1]].report_header.push({ name: header, total: 0 });
154
+
155
+ let ruralTotal =
156
+ distBlock[areatype[0]].report_header.find(h => h.name === header)?.total || 0;
157
+
158
+ distBlock.combined.report_header.push({
159
+ name: header,
160
+ total: ruralTotal
161
+ });
162
+ }
163
+ }
164
+ }
165
+ }
166
+ }
167
+
168
+ // ---------------------------
169
+ // TABLE DATA
170
+ // ---------------------------
171
+ if (tablewiseChartData && tablewiseChartData.length > 0) {
172
+ let tableHeadersObj = {};
173
+ tablewiseChartData.forEach(item => {
174
+ if (item.report_header && item.report_order !== undefined) {
175
+ tableHeadersObj[item.report_order] = item.report_header;
176
+ }
177
+ });
178
+
179
+ let tHeaders = Object.values(
180
+ Object.keys(tableHeadersObj)
181
+ .sort((a, b) => a - b)
182
+ .reduce((acc, key) => {
183
+ acc[key] = tableHeadersObj[key];
184
+ return acc;
185
+ }, {})
186
+ );
187
+
188
+ for (let row of tablewiseChartData) {
189
+ if (row.disttName === "(NULL)") continue;
190
+
191
+ if (!finaldata.tabledata) finaldata.tabledata = {};
192
+ if (!finaldata.tabledata[row.State]) finaldata.tabledata[row.State] = {};
193
+ if (!finaldata.tabledata[row.State][row.disttName])
194
+ finaldata.tabledata[row.State][row.disttName] = {};
195
+ if (!finaldata.tabledata[row.State][row.disttName][row.tableName])
196
+ finaldata.tabledata[row.State][row.disttName][row.tableName] = {
197
+ report_header: []
198
+ };
199
+
200
+ if (tHeaders.includes(row.report_header)) {
201
+ finaldata.tabledata[row.State][row.disttName][row.tableName].report_header.push({
202
+ name: row.report_header,
203
+ total: row.total
204
+ });
205
+ }
206
+ }
207
+ }
208
+
209
+ return { ...finaldata, ...grandtotal };
210
+ }
211
+
212
+ module.exports = { processstatewisechart };
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "statewise-chart-processor",
3
+ "version": "1.0.0",
4
+ "description": "Processes statewise chart data for census reports.",
5
+ "main": "index.js",
6
+ "keywords": ["census", "statewise", "chart", "data", "processor"],
7
+ "author": "Your Name",
8
+ "license": "MIT"
9
+ }