-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.php
255 lines (209 loc) · 10.8 KB
/
index.php
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>Create your own XKCD-style Graphs</title>
<meta name="description" content="Instant XKCD-style Graphs created in Javascript D3 for your enjoyment" />
<meta name="author" content="Kevin Xu, Charlie Guo" />
<!-- <link rel="icon" type="image/x-icon" href="graph.png" /> -->
<!-- Apple Meta Data -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="apple-touch-icon-precomposed" href="apple-touch-icon.png" />
<!-- Facebook Meta Data -->
<meta property="og:url" content="http://xkcdgraphs.com" />
<meta property="og:title" content="Create your own XKCD-style Graphs" />
<meta property="og:description" content="Instant XKCD-style Graphs created in Javascript D3 for your enjoyment" />
<meta property="og:image" content="http://xkcdgraphs.com/graph.png" />
<meta property="og:type" content="website" />
<!-- CSS Files -->
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36917714-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=407882832614060";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</head>
<body>
<div class="container">
<h1>Create your own XKCD-style Graphs</h1>
<h2>Major credit to <a href="http://dan.iel.fm/xkcd/" target="_blank">Dan Foreman-Mackey</a></h2>
<form>
<div class="left">
<div class="input">
<label for="equation">Equation</label>
<input type="text" id="equation" placeholder="x*sin(x)" />
</div>
<div class="input">
<label for="xmin">X-minimum</label>
<input type="text" id="xmin" placeholder="-10" value="-10" />
</div>
<div class="input">
<label for="xmax">X-maximum</label>
<input type="text" id="xmax" placeholder="10" value="10" />
</div>
</div>
<div class="right">
<div class="input">
<label for="title">Title</label>
<input type="text" id="title" placeholder="Your Graph" value="Your Graph" />
</div>
<div class="input">
<label for="xlabel">X-label</label>
<input type="text" id="xlabel" placeholder="X" value="X" />
</div>
<div class="input">
<label for="ylabel">Y-label</label>
<input type="text" id="ylabel" placeholder="Y" value="Y" />
</div>
</div>
<div class="clear"></div>
<label for="slider" class="slider">Refinement</label>
<div id="slider"></div>
<span id="more">+</span>
<div class="left">
<div class="input">
<label for="equation2" class="equation2">Equation 2</label>
<input type="text" id="equation2" class="equation2" placeholder="x^2" />
</div>
<div class="input">
<label for="equation4" class="equation4">Equation 4</label>
<input type="text" id="equation4" class="equation4" placeholder="1/tan(x)" />
</div>
</div>
<div class="right">
<div class="input">
<label for="equation3" class="equation3">Equation 3</label>
<input type="text" id="equation3" class="equation3" placeholder="sqrt(x*10)" />
</div>
<div class="input">
<label for="equation5" class="equation5">Equation 5</label>
<input type="text" id="equation5" class="equation5" placeholder="abs(x)" />
</div>
</div>
<div class="clear"></div>
</form>
</div>
<div id="plot"></div>
<div id="examples"></div>
<footer class="container">
<div class="social">
<div class="fb-like" data-href="http://xkcdgraphs.com/" data-send="true" data-layout="button_count" data-width="450" data-show-faces="true"></div>
<a href="https://twitter.com/share" class="twitter-share-button" data-text="Create your own XKCD-style Graphs instantly" data-via="imkevinxu">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
<div class="credit">
<a href="https://twitter.com/charlierguo" target="_blank">@charlierguo</a>
<a href="http://twitter.com/imkevinxu" target="_blank">@imkevinxu</a>
</div>
</footer>
<a href="https://github.com/imkevinxu/xkcdgraphs" target="_blank">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub">
</a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script src="d3.v2.min.js"></script>
<script src="jquery.textchange.min.js"></script>
<script src="xkcd.js"></script>
<script src="examples.js"></script>
<script src="parser.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#equation').focus();
$("#slider").slider({ min: 1, max: 250, value: 100 });
$("#slider").on("slide", function() {
drawGraph();
});
$('input').on('textchange', function () {
drawGraph();
});
$('#more').on('click', function() {
if ($(".equation4").css("display") === "inline-block") $(".equation5").fadeIn();
if ($(".equation3").css("display") === "inline-block") $(".equation4").fadeIn();
if ($(".equation2").css("display") === "inline-block") $(".equation3").fadeIn();
$(".equation2").fadeIn();
});
function drawGraph() {
var plot = xkcdplot();
drawGraphEquation(plot, '#equation');
if ($("#equation2").val()) drawGraphEquation(plot, '#equation2', 'red');
if ($("#equation3").val()) drawGraphEquation(plot, '#equation3', 'green');
if ($("#equation4").val()) drawGraphEquation(plot, '#equation4', 'purple');
if ($("#equation5").val()) drawGraphEquation(plot, '#equation5', 'gray');
}
function drawGraphEquation(plot, equation, color) {
$("#plot").empty();
if (!color) color = "steelBlue";
var expression = string_eval($(equation).val()),
xmin = parseInt($('#xmin').val()),
xmax = parseInt($('#xmax').val()),
N = $("#slider").slider( "option", "value");
if (expression != "'Invalid function'" && !isNaN(xmin) && !isNaN(xmax) && xmin < xmax) {
function f(d) {
current_expression = expression.split("-x").join(-d);
var result = eval(current_expression.split("x").join(d));
if (isNaN(result)) {
return 0;
} else if (result === -Infinity) {
return -25;
} else if (result === Infinity) {
return 25;
} else {
return result;
}
}
var data = d3.range(xmin, xmax, (xmax - xmin) / N).map(function (d) {
return {x: d, y: f(d)};
});
var parameters = { title: $('#title').val(),
xlabel: $('#xlabel').val(),
ylabel: $('#ylabel').val(),
xlim: [xmin - (xmax - xmin) / 16, xmax + (xmax - xmin) / 16] };
if (expression.indexOf("x") < 0) {
if (eval(expression) < -10) {
parameters["ylim"] = [eval(expression), 10];
} else if (eval(expression) > 10) {
parameters["ylim"] = [-10, eval(expression)];
} else {
parameters["ylim"] = [-10, 10];
}
}
plot("#plot", parameters);
plot.plot(data, {stroke: color});
plot.draw();
for (var i = xmin; i < xmax; i++) {
current_expression = expression.split("-x").join(-i);
var result = eval(current_expression.split("x").join(i));
if (isNaN(result) || result === Infinity) {
$("#plot").append("<h1>Some part of the equation is invalid along the domain you chose</h1>");
break;
}
}
console.log("[Graph Equation] " + $('#equation').val());
console.log("[JS Expression] " + expression);
} else {
$("#plot").append("<h1>Sorry, invalid function</h1>");
console.log("[Invalid Function] " + $('#equation').val());
}
}
});
</script>
</body>
</html>