This repository has been archived by the owner on May 11, 2021. It is now read-only.
/
creating_bar_charts_1.html
196 lines (185 loc) · 9.54 KB
/
creating_bar_charts_1.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
<!DOCTYPE html>
<html data-require="math math-format expressions graphie interactive word-problems stat">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Creating bar charts 1</title>
<script src="../khan-exercise.js"></script>
<style>
span.rotate {
-moz-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
-ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=3)";
}
body.ie span.rotate {
left: 60px !important;
top: 140px !important;
}
</style>
</head>
<body>
<div class="exercise">
<div class="problems">
<div>
<div class="vars">
<var id="SUBJECT,SUBJECT_FUNC">randFromArray([
[new Plural(function(num) {
return $.ngettext("animal", "animals", num);
}), KhanUtil.animal],
[new Plural(function(num) {
return $.ngettext("color", "colors", num);
}), KhanUtil.color],
[new Plural(function(num) {
return $.ngettext("fruit", "fruits", num);
}), KhanUtil.fruit],
[new Plural(function(num) {
return $.ngettext("course", "courses", num);
}), KhanUtil.course]
])</var>
<var id="CATEGORIES">_.times(5, function(n) { return SUBJECT_FUNC(n + 1); } )</var>
<var id="DATA">_.times(5, function() { return randRange(0, 10); } )</var>
<var id="TOTAL">sum(DATA)</var>
<var id="RESPONDENT">randFromArray([
new Plural(function(num) {
return $.ngettext("student", "students", num);
}),
new Plural(function(num) {
return $.ngettext("teacher", "teachers", num);
}),
new Plural(function(num) {
return $.ngettext("person", "people", num);
})
])</var>
<var id="SHUFFLED">shuffle(_.zip(DATA, CATEGORIES))</var>
</div>
<p>
<span data-if="isSingular(TOTAL)">
<code><var>TOTAL</var></code> <var>RESPONDENT</var> was asked about their favorite <var>SUBJECT</var>.
</span><span data-else="">
<code><var>TOTAL</var></code> <var>plural_form(RESPONDENT, TOTAL)</var> were asked about their favorite <var>SUBJECT</var>.
</span>
<span data-each="SHUFFLED as DATA">
<span data-if="isSingular(DATA[0])">
<code><var>DATA[0]</var></code> <var>RESPONDENT</var> said <var>DATA[1]</var>.
</span><span data-else>
<code><var>DATA[0]</var></code> <var>plural_form(RESPONDENT, DATA[0])</var> said <var>DATA[1]</var>.
</span>
</span>
</p>
<p class="question">Create a bar chart showing everyone's favorite <var>plural_form(SUBJECT)</var>:</p>
<div class="problem">
<div class="graphie" id="barchart">
init({
range: [ [ -2, 17 ], [ -3, 12 ] ],
scale: [ 26, 30 ]
});
addMouseLayer();
for ( var y = 0; y <= 10; ++y ) {
label( [ 0, y ], y, "left", false );
style({ stroke: "#000", strokeWidth: 1, opacity: 0.3 }, function() {
line( [ 0, y ], [ 16, y ] )
});
}
graph.leftPoints = [];
graph.rightPoints = [];
graph.lines = [];
for ( var index = 0; index < CATEGORIES.length; ++index ) {
graph.leftPoints[ index ] = addMovablePoint({ coord: [ index * 3 + 1, 0.5 ], visible: false });
graph.rightPoints[ index ] = addMovablePoint({ coord: [ index * 3 + 3, 0.5 ], visible: false });
graph.lines[ index ] = addMovableLineSegment({ pointA: graph.leftPoints[ index ], pointZ: graph.rightPoints[ index ], snapY: 0.5, constraints: { constrainX: true } });
// graph.lines[ index ] = {};
style({ stroke: "none", fill: "#9ab8ed", opacity: 1.0 }, function() {
graph.lines[ index ].bar = path([ [ index * 3 + 1, 0 ], [ index * 3 + 1, 0.5 ], [ index * 3 + 3, 0.5 ], [ index * 3 + 3, 0 ], [ index * 3 + 1, 0 ] ]);
});
// graph.lines[ index ].toFront();
label( [ index * 3 + 2, 0 ], CATEGORIES[ index ] + "", "below", false ).css( "text-transform", "capitalize" );
var bar = index;
graph.lines[ index ].onMove = function( dX, dY ) {
if ( this.coordA[1] < 0 || this.coordA[1] > 10 ) {
this.coordA[1] -= dY;
this.coordZ[1] -= dY;
}
this.pointA.setCoord([ this.pointA.coord[0], this.coordA[1] ]);
this.pointZ.setCoord([ this.pointZ.coord[0], this.coordA[1] ]);
this.pointA.updateLineEnds();
this.pointZ.updateLineEnds();
this.bar.scale( 1, Math.max( 0.01, this.coordA[1] * 2 ), scalePoint( 0 )[0], scalePoint( 0 )[1] );
};
}
style({ stroke: "#000", strokeWidth: 2, opacity: 1.0 }, function() {
line( [ 0, 0 ], [ 16, 0 ] );
line( [ 0, 0 ], [ 0, 10 ] );
});
label([8, -0.8],
SUBJECT + "",
"below", false)
.css("text-transform", "capitalize")
.css("font-weight", "bold");
label([-1.5, 5],
$._("Number of %(respondent)s", { respondent: plural_form(RESPONDENT) }),
"center", false)
.css("font-weight", "bold")
.addClass("rotate");
label([8, 10.5],
$._("Favorite %(subject)s", { subject: plural_form(SUBJECT) }),
"above", false)
.css("font-weight", "bold");
</div>
</div>
<div class="solution" data-type="custom">
<div class="instruction">
Create a bar chart by dragging the top of each bar up and down.
</div>
<div class="guess">
_.times(5, function(n) { return graph.lines[ n ].coordA[1]; })
</div>
<div class="validator-function">
if ( _.isEqual( guess, [ 0.5, 0.5, 0.5, 0.5, 0.5 ] ) ) {
return "";
}
return _.isEqual( guess, DATA );
</div>
<div class="show-guess">
$.each( guess, function( index ) {
graph.lines[ index ].pointA.setCoord([ graph.lines[ index ].pointA.coord[0], this ]);
graph.lines[ index ].pointZ.setCoord([ graph.lines[ index ].pointZ.coord[0], this ]);
graph.lines[ index ].pointA.updateLineEnds();
graph.lines[ index ].pointZ.updateLineEnds();
graph.lines[ index ].bar.scale( 1, Math.max( 0.01, this * 2 ), scalePoint( 0 )[0], scalePoint( 0 )[1] );
});
</div>
</div>
</div>
</div>
<div class="hints">
<div data-each="DATA as INDEX, NUM">
<p>
<span data-if="isSingular(NUM)">
<code><var>NUM</var></code> <var>RESPONDENT</var> said their
favorite <var>SUBJECT</var> was <var>CATEGORIES[ INDEX ]</var>.
</span><span data-else="">
<code><var>NUM</var></code> <var>plural_form(RESPONDENT, NUM)</var> said their
favorite <var>SUBJECT</var> was <var>CATEGORIES[ INDEX ]</var>.
</span>
<span data-if="NUM > 0">
So the top of the bar for
"<span style="text-transform: capitalize"><var>CATEGORIES[ INDEX ]</var></span>"
should line up with the number
<code><var>NUM</var></code> on the left side of the chart.
</span><span data-else>
So there should be no bar above
"<span style="text-transform: capitalize"><var>CATEGORIES[ INDEX ]</var></span>".
Drag the top of the bar all the way to the bottom to get
rid of the bar.
</span>
</p>
<div class="graphie" data-if="NUM > 0" data-update="barchart">
style({ stroke: ORANGE, fill: "none", strokeWidth: 1, strokeDasharray: "- " }, function() {
path([ [ INDEX * 3 + 1, 0 ], [ INDEX * 3 + 1, NUM ], [ INDEX * 3 + 3, NUM ], [ INDEX * 3 + 3, 0 ] ]).toBack();
});
</div>
</div>
</div>
</div>
</body>
</html>