-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
239 lines (215 loc) · 7.4 KB
/
script.js
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
// Function to process uploaded CSV file
// function processCSV(event) {
// const file = event.target.files[0];
// Papa.parse(file, {
// header: true,
// dynamicTyping: true,
// complete: function (results) {
// const data = results.data;
// plotData1(data);
// plotData2(data);
// generateAnalysis(data);
// },
// });
// }
// Base OUTLET UNITS
var customerValue = 50000;
var intervalTime = 2000;
// Dynamic graph - line plot
function plotData1() {
const ctx = document.getElementById("plot1").getContext("2d");
const chart = new Chart(ctx, {
type: "line",
data: {
labels: [],
datasets: [
{
label: "Frequency of Purchase",
data: [],
backgroundColor: "white",
borderColor: "red",
borderWidth: 1,
fill: false, // Disable fill to see lines more clearly
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
animation: {
duration: 0, // Disable animation for immediate updates
},
},
});
let dataIndex = 0; // Start index for data update
function updateChart() {
if (dataIndex < customerValue) {
// Generating 50 random data points
const randomCustomerID = "CID" + (Math.floor(Math.random() * 1000) + 1); // Random CustomerID
const randomFrequency = Math.random(); // Random Frequency between 0 and 1
chart.data.labels.push(randomCustomerID);
chart.data.datasets[0].data.push(randomFrequency);
chart.update();
generateAnalysis(chart.data); // Call generateAnalysis with updated chart data
dataIndex++;
} else {
clearInterval(interval); // Stop updating when all data is used
}
}
const interval = setInterval(updateChart, intervalTime); // Update every 1 second
}
plotData1(); // Call the function to start updating the chart and analysis
// Dynamic graph for plot 2
function plotData2() {
const ctx = document.getElementById("plot2").getContext("2d");
const chart = new Chart(ctx, {
type: "polarArea",
data: {
labels: [],
datasets: [
{
label: "Frequency of Purchase",
data: [],
backgroundColor: "#00000059",
borderColor: "lime",
borderWidth: 1,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
let dataIndex = 0; // Start index for data update
function updateChart() {
if (dataIndex < customerValue) {
// Generating 50 random data points
const randomCustomerID = "CID" + (Math.floor(Math.random() * 1000) + 1); // Random CustomerID
const randomFrequency = Math.random(); // Random Frequency between 0 and 1
chart.data.labels.push(randomCustomerID);
chart.data.datasets[0].data.push(randomFrequency);
chart.update();
generateAnalysis(chart.data); // Call generateAnalysis with updated chart data
dataIndex++;
} else {
clearInterval(interval); // Stop updating when all data is used
}
}
const interval = setInterval(updateChart, intervalTime); // Update every 1 second
}
plotData2(); // Call the function to start updating the chart and analysis
// Dynamic graph for plot 3
function plotData3() {
const ctx = document.getElementById("plot3").getContext("2d");
const chart = new Chart(ctx, {
type: "radar",
data: {
labels: [],
datasets: [
{
label: "Frequency of Purchase",
data: [],
backgroundColor: "#f2ff006b",
borderColor: "black",
borderWidth: 1,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
let dataIndex = 0; // Start index for data update
function updateChart() {
if (dataIndex < customerValue) {
// Generating 50 random data points
const randomCustomerID = "CID" + (Math.floor(Math.random() * 1000) + 1); // Random CustomerID
const randomFrequency = Math.random(); // Random Frequency between 0 and 1
chart.data.labels.push(randomCustomerID);
chart.data.datasets[0].data.push(randomFrequency);
chart.update();
generateAnalysis(chart.data); // Call generateAnalysis with updated chart data
dataIndex++;
} else {
clearInterval(interval); // Stop updating when all data is used
}
}
const interval = setInterval(updateChart, intervalTime); // Update every 1 second
}
plotData3(); // Call the function to start updating the chart and analysis
// dynamic analysis
function generateAnalysis(chartData) {
const totalCustomers = chartData.labels.length;
let totalPurchases = 0;
const overallPurchases = {};
chartData.labels.forEach((customerId, index) => {
const frequency = chartData.datasets[0].data[index];
if (!isNaN(frequency)) {
overallPurchases[customerId] = overallPurchases[customerId]
? overallPurchases[customerId] + frequency
: frequency;
totalPurchases += frequency;
}
});
const customerIds = Object.keys(overallPurchases);
const maxPurchasedCustomer = customerIds.reduce((a, b) =>
overallPurchases[a] > overallPurchases[b] ? a : b
);
const minPurchasedCustomer = customerIds.reduce((a, b) =>
overallPurchases[a] < overallPurchases[b] ? a : b
);
const averagePurchasedCustomer = totalPurchases / totalCustomers;
// Find the actual customer ID for maximum and minimum purchased customers
const maxPurchasedCustomerID = maxPurchasedCustomer;
const minPurchasedCustomerID = minPurchasedCustomer;
// Generate analysis report in a table-like structure
const analysisHTML = `
<h2>Customer Pulse Analysis</h2>
<div class="analysis-table">
<div class="table-row">
<div class="table-cell title-table">Total Customers Frequency:</div>
<div class="table-cell value-table">${totalCustomers}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">Total Purchases Frequency:</div>
<div class="table-cell value-table">${totalPurchases.toFixed(2)}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">Overall Frequency Cheat-Sheet:</div>
<div class="table-cell value-table">${JSON.stringify(
overallPurchases
)}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">High Frequent Customer:</div>
<div class="table-cell value-table">${maxPurchasedCustomerID}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">Low Frequent Customer:</div>
<div class="table-cell value-table">${minPurchasedCustomerID}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">Average RFM Value:</div>
<div class="table-cell value-table">${averagePurchasedCustomer.toFixed(
2
)}</div>
</div>
<div class="table-row">
<div class="table-cell title-table">Nominated Customer:</div>
<div class="table-cell value-table markedResult">${maxPurchasedCustomerID}</div>
</div>
</div>
`;
document.getElementById("analysis").innerHTML = analysisHTML;
}