-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_customer.html
More file actions
150 lines (134 loc) · 4.4 KB
/
interactive_customer.html
File metadata and controls
150 lines (134 loc) · 4.4 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
<!DOCTYPE html>
<html>
<head>
<title>Interactive Customer Search</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script>
function getCustomers() {
return [{gender:'M', income: 'high', location: 'texas'},
{gender:'M', income: 'low', location: 'texas'},
{gender:'F', income: 'low', location: 'texas'},
{gender:'M', income: 'medium', location: 'florida'},
{gender:'F', income: 'high', location: 'florida'},
{gender:'F', income: 'medium', location: 'virginia'},
{gender:'', income: 'low', location: 'virginia'},
{gender:'M', income: 'high', location: 'virginia'},
{gender:'M', income: 'low', location: 'kansas'},
{gender:'F', income: 'low', location: 'ohio'},
{gender:'M', income: 'medium', location: 'chicago'},
{gender:'F', income: 'high', location: 'kansas'},
{gender:'F', income: 'medium', location: 'ohio'},
{gender:'', income: 'low', location: 'ohio'},
{gender:'M', income: 'high', location: 'london'},
{gender:'M', income: 'low', location: 'london'},
{gender:'F', income: 'low', location: 'ohio'},
{gender:'M', income: 'medium', location: 'ohio'},
{gender:'F', income: 'high', location: 'london'},
{gender:'F', income: 'medium', location: 'chicago'},
{gender:'', income: 'low', location: 'chicago'}];
}
function applyFilters(filters) {
var customers = getCustomers(),
obj = [],
counter = {high: 0, medium: 0, low: 0};
console.log(filters);
for(var i in customers){
var itrFlag = true;
for(var key in filters) {
if((key != 'income' && filters[key] != customers[i][key]) || (key == 'income' && filters[key].indexOf(customers[i][key]) == -1)) {
itrFlag = false;
}
}
if(itrFlag) {
counter[customers[i].income] += 1;
obj.push(customers[i]);
}
}
console.log(counter);
return counter;
}
function submitSearch() {
var searchStr = document.getElementsByName('search')[0].value,
filterObj = {},
searchStr = searchStr.replace(/,/g , " ");
console.log(searchStr);
searchArray = searchStr.split(" ");
if(searchArray.indexOf('female') > -1) {
filterObj['gender'] = 'F';
} else if(searchStr.indexOf('male') > -1) {
filterObj['gender'] = 'M';
}
if(searchStr.indexOf('income level') > -1 && searchArray.length > searchArray.indexOf('level')) {
filterObj['income'] = [];
if(searchArray.indexOf('high') > -1) {
filterObj['income'].push('high');
}
if(searchArray.indexOf('medium') > -1) {
filterObj['income'].push('medium');
}
if(searchArray.indexOf('low') > -1) {
filterObj['income'].push('low');
}
console.log(searchArray);
}
if(searchStr.indexOf('from') > -1 && searchArray.length > searchArray.indexOf('from')) {
filterObj['location'] = searchArray[searchArray.indexOf('from') + 1];
}
var result = applyFilters(filterObj);
renderChart(prepareChartData(result));
}
function prepareChartData(result) {
var series = [];
if(result) {
for(var key in result) {
series.push({name: key, y: result[key]});
}
}
return series;
}
function renderChart(sData) {
Highcharts.chart('container', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'Interactive Customer Data'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.y}</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
}
},
series: [{
name: 'Customers',
colorByPoint: true,
data: sData
}]
});
}
</script>
</head>
<body>
<div style="height:30px;width:100%;text-align:center;">
<label>Customer Search</label>
<input type="text" name="search" size="75" style="height:inherit;"/>
<button name="doSearch" style="height:inherit;margin-left:5px;" onclick="submitSearch();">Search</button></br>
<!-- <canvas id="piechart" width="200" height="200"></canvas> -->
<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 10px auto"></div>
</div>
</body>
</html>