-
Notifications
You must be signed in to change notification settings - Fork 0
/
barchart.html
231 lines (188 loc) · 5.91 KB
/
barchart.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
225
226
227
228
229
230
231
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Marta Kule | Bar Chart</title>
<link href="https://fonts.googleapis.com/css?family=Ubuntu:400,500" rel="stylesheet">
<link rel="stylesheet" href="activity-style.css">
<script src="script/jquery-1.7.2.min.js"></script>
<script src="script/navigation.js"></script>
</head>
<body>
<div class="wrapper">
<div class="header">
<div class="mainColor">
<h1>Bar Chart</h1>
</div>
<div class="darker"></div>
<div class="lighter"></div>
</div>
<div class="slide show">
<p>This is one of the features I built for online activities that accompany a business communication textbook. This bar chart was used in all cases where percentage results were being shown.</p>
<p>Type in positive decimal numbers to see it in action:</p>
<form id="input-form" class="graph">
<div class="labels">
<div class="label">First Value</div>
<div class="label">Second Value</div>
<div class="label">Third Value</div>
<div class="label">Fourth Value</div>
</div>
<div class="bars">
<div class="unit">
<input type="text" id="one">
</div>
<div class="unit">
<input type="text" id="two">
</div>
<div class="unit">
<input type="text" id="three">
</div>
<div class="unit">
<input type="text" id="four">
</div>
</div>
</form>
<button id="generate" class="answer_button">Generate the Chart</button>
<h2>Your Results</h2>
<div class="graph">
<div class="labels">
<div class="label">First Value</div>
<div class="label">Second Value</div>
<div class="label">Third Value</div>
<div class="label">Fourth Value</div>
</div>
<div class="bars">
<div class="unit">
<div class="bar" id="first"><span>17%</span></div>
<span></span>
</div>
<div class="unit">
<div class="bar" id="second"><span>23%</span></div>
<span></span>
</div>
<div class="unit">
<div class="bar" id="third"><span>34%</span></div>
<span></span>
</div>
<div class="unit">
<div class="bar" id="fourth"><span>26%</span></div>
<span></span>
</div>
<div class="scale">
<div>0</div>
<div>25</div>
<div>50</div>
<div id="end">
<span>75</span>
<span>100</span>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<div class="mainColor"></div>
<div class="darker"></div>
<div class="lighter">
<div id="navButtons"><button id="back">Back</button> <button id="next">Next</button></div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#generate").on("click", function(e){
e.preventDefault();
// Remove classes in input fields before each input evaluation
$('.wrong').remove();
$(".wrongInput").removeClass("wrongInput");
// Values object
var numbers = $("#input-form").find("input"),
values = {
"first":0,
"second":0,
"third":0,
"fourth":0
},
valueList = [],
validator = 0;
// Ensure input isn't empty, negative number, or a value other than number
for (var i = 0; i < numbers.length; i++) {
var userInput = numbers[i].value;
var id = numbers[i].id;
if (isNaN(userInput) || userInput < 0 || userInput.length == 0){
$("#"+id).addClass("wrongInput").parent().append("<p class='wrong'>Pick a positive decimal number</p>");
}
else {
validator++;
valueList.push(parseInt(userInput));
}
}
// Ensure not all values are zeros
function getTotal(total, num) {
return total + num;
}
function areAllZeros(item) {
return valueList.reduce(getTotal);
}
if (areAllZeros()===0) {
alert("Pick at least one number larger than zero.");
}
// Render a chart only if all values are of correct type
else if (validator === numbers.length && areAllZeros()!=0){
values.first = valueList[0];
values.second = valueList[1];
values.third = valueList[2];
values.fourth = valueList[3];
// Get viewport width for bar behaviour
function hypotheticalUnitSize() {
if ( $(window).width() > 1100 ) {
return 998 }
else {
return ( $(window).width() ) - 100 }
}
renderStuff = function() {
// Add up all input values
var total = 0;
for (var key in values) {
total = total + +values[key];
}
var fullWidth = hypotheticalUnitSize();
var size;
// Get each bar's size as percentage of total
for (var key in values) {
size = Math.floor( (values[key]/total)*100 );
// Use size as width of bar div and text for label
var text = size.toString()+'%';
$("#"+key).css("width", size+'%').find("span").text(text);
// Calculate each bar's width and put label outside if width less than 50px
var thisWidth = size/100*fullWidth;
// If the value is 0, don't show the bar at all
if (thisWidth==0){
$("#"+key+" span").addClass('spanOutside').css('left',(thisWidth+7)+"px");
$("#"+key).css('padding','14px 0');
}
// If 100%, make the bar slightly shorter to fit in the box
else if (thisWidth==fullWidth){
$("#"+key).css("width", '99.6%')
}
else if (thisWidth < 50 && thisWidth!==0){
$("#"+key+" span").addClass('spanOutside').css('left',(thisWidth+7)+"px").css('padding-left','0');
}
else {
$("#"+key+" span").removeClass('spanOutside').css('padding-left','10px');
}
}
}
// Initial render of the chart on click
renderStuff();
// Render of the chart on each window width resize
$( window ).resize(function() {
renderStuff();
});
}
});
});
</script>
</body>
</html>