-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
224 lines (180 loc) · 5.5 KB
/
demo.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="metrics-graphics-2.6.0/dist/metricsgraphics.js" charset="utf-8"></script>
<link rel="stylesheet" href="metrics-graphics-2.6.0/dist/metricsgraphics.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
<title>PERI demo</title>
</head>
<style>
.legend, #userProduct, #userRegion {
font: 300 10px "Opens Sans", sans-serif;
}
.mg-x-axis, .mg-y-axis, .mg-active-datapoint {
font: 300 15px "Opens Sans", sans-serif;
}
.mg-chart-title{
font: 300 24px "Opens Sans", sans-serif;
}
.mg-missing-text {
font: 500 15px "Opens Sans", sans-serif;
}
#userProduct, #userRegion {
margin: 2px;
position: absolute;
top: 100px;
left: 700px;
}
#userRegion {
top: 175px;
}
#unselect-button:hover, #selectall-button:hover {
cursor: pointer;
}
ul {
list-style: none;
padding-left:0;
}
.chkbox {
padding-left: 3px;
}
</style>
<body>
<div id="multiLine"></div>
<div class="legend"></div>
<div id="userProduct"><p>Select a commodity:</p></div>
<div id="userRegion"><p>Select region(s):</p></div>
</body>
<script>
var dropdown = d3.select("#userProduct")
.append("select");
var checkboxes = d3.select("#userRegion").append("ul");
d3.json('example-data/alldata.json', function(data) {
var productNames = Object.keys(data);
// Dropdown: Products
dropdown.selectAll("option")
.data(productNames)
.enter()
.append("option")
.attr("value", function (d) { return d; })
.text(function (d) { return d; });
dropdown
.on("change", function(d) {
product = d3.select(this).property("value");
draw(data,product,selectedRegions,allRegions);
});
var product = dropdown.property("value");
var allRegions = Object.keys(data[product]);
var selectedRegions = {};
allRegions.forEach(function(d) {
selectedRegions[d] = true;
});
// Checkboxes: Regions
allRegions.forEach(function(d) {
var li = checkboxes.append("li");
li.append("input")
.attr("type", "checkbox")
.attr("checked",true)
.attr("value", d)
.attr("id",d)
li.append("label")
.text(d)
.attr("for",d)
.attr("class","chkbox")
})
checkboxes.selectAll("input")
.on("click", function(d) {
var checked = $("input[type=checkbox]:checked");
var regions = checked.map(function(i) { return $(this).val() }).get();
allRegions.forEach(function(region) {
if (regions.indexOf(region)===-1) selectedRegions[region] = false;
else selectedRegions[region] = true;
})
draw(data,product,selectedRegions,allRegions);
});
var unselect = d3.select("#userRegion")
.append("p")
.attr("id", "unselect-button")
.text("Clear all");
unselect.on("click", function() {
checkboxes.selectAll('input')
.property('checked', false);
var checked = $("input[type=checkbox]:checked");
var regions = checked.map(function(i) { return $(this).val() }).get();
allRegions.forEach(function(region) {
if (regions.indexOf(region)===-1) selectedRegions[region] = false;
else selectedRegions[region] = true;
})
draw(data,product,selectedRegions,allRegions);
});
var selectAll = d3.select("#userRegion")
.append("p")
.attr("id", "selectall-button")
.text("Select all");
selectAll.on("click", function() {
checkboxes.selectAll('input')
.property('checked', true);
var checked = $("input[type=checkbox]:checked");
var regions = checked.map(function(i) { return $(this).val() }).get();
allRegions.forEach(function(region) {
if (regions.indexOf(region)===-1) selectedRegions[region] = false;
else selectedRegions[region] = true;
})
draw(data,product,selectedRegions,allRegions);
});
draw(data,product,selectedRegions,allRegions);
}); //d3.json()
function draw(data,product,selectedRegions,allRegions) {
var chartData = prepForChart(data[product],selectedRegions,allRegions);
var chart_type = (chartData.length>0 ? 'line' : 'missing-data');
MG.data_graphic({
title: "Commodity Prices Across Divisions of Punjab",
area: false,
width: 600,
height: 300,
right: 40,
data: chartData,
interpolate:'linear',
target: '#multiLine',
y_accessor: 'price',
x_accessor: 'date',
legend: allRegions,
legend_target: '.legend',
chart_type: chart_type,
missing_text: 'Select a region',
y_extended_ticks: true,
mouseover: function(d, i) {
var df = d3.time.format('%b %d, %Y');
var date = df(d.date);
var y_val = (d.price === 0) ? 'no data' : d.price;
var region = allRegions[d.line_id-1]
d3.select('#multiLine svg .mg-active-datapoint')
.text(region + ' | ' + product + ' | ' + date + ' | Rs. ' + y_val);
}
});
function prepForChart(productData,selectedRegions,allRegions) {
var array = [];
var count = 0;
allRegions.forEach(function(region) { // reshape data structure for MG: convert array of key value pairs into array of arrays
if (selectedRegions[region] === true) {
array.push(productData[region]);
count += 1;
}
else {
array.push([]);
}
});
if (count===0) { // for chart_type: 'missing-data' to work we need to pass in an empty array, not an array of all empty arrays
array = [];
}
for (var i = 0; i < array.length; i++) {
array[i] = MG.convert.date(array[i], 'date');
}
return array;
} //prepForChart()
} //draw()
</script>
</html>