Repository navigation
Expand file tree
/
Copy pathgraphcalc.js
More file actions
173 lines (152 loc) · 5.73 KB
/
Copy pathgraphcalc.js
File metadata and controls
173 lines (152 loc) · 5.73 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
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
var graphcalc = (function () {
var exports = {};
// canvas is the DOM object
function graph(canvas,expression,x1,x2) {
var w = canvas.width;
var h = canvas.height;
var ctx;
var tree,v1,v2;
try {
// step 1: compute expression values
tree = calculator.parse(expression);
v1 = calculator.evaluate(calculator.parse(x1));
v2 = calculator.evaluate(calculator.parse(x2));
if (v2 < v1) {
var temp = v1;
v1 = v2;
v2 = temp;
}
var x = [];
var y = [];
var e = {e: Math.E, pi: Math.pi};
var step = (v2-v1)/w;
if (step == 0) { v2 = v1 + 1; step = 1/w; }
for (var v = v1; v <= v2; v += step) {
x.push(v);
e.x = v;
y.push(calculator.evaluate(tree,e));
}
var npoints = x.length;
canvas.x_data = x; // save for event handlers
canvas.y_data = y;
// step 2: figure out plot scale
var xscale = (x[npoints-1]-x[0])/w;
var ymin = Math.min.apply(null,y);
var ymax = Math.max.apply(null,y);
var yextra = (ymax - ymin)/10;
if (yextra == 0) yextra = 1;
ymin -= yextra;
ymax += yextra;
var yscale = (ymax - ymin)/h;
function xform(xv,yv) {
return {x: (xv-x[0])/xscale, y: (ymax - yv)/yscale};
}
canvas.x_scale = xscale; // save for event handlers
canvas.y_scale = yscale;
canvas.y_max = ymax;
// step 3: plot points into background image
var bg_image = $('<canvas></canvas>')[0];
canvas.bg_image = bg_image; // save for event handlers
bg_image.width = w;
bg_image.height = h;
ctx = bg_image.getContext('2d');
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0,0,w,h);
ctx.beginPath();
var pt = xform(x[0],y[0]); ctx.moveTo(pt.x,pt.y);
for (var i = 1; i < npoints; i += 1) {
pt = xform(x[i],y[i]);
ctx.lineTo(pt.x,pt.y);
}
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.strokeStyle = "red";
ctx.stroke();
// step 4: paint background on canvas
ctx = canvas.getContext('2d');
ctx.drawImage(bg_image,0,0);
} catch (err) {
// display error message in middle of canvas
ctx = canvas.getContext('2d');
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.font = "20px Georgia";
ctx.fillStyle = "black";
ctx.fillText(err,w/2,h/2);
}
}
// using array of xdata and ydata, interpolate y value
// given a particular x
function interpolate(xdata,ydata,x) {
for (var i = 0; i < xdata.length; i += 1) {
if (x < xdata[i]) {
// t falls between times[i-1] and times[i]
var t1 = (i === 0) ? xdata[0] : xdata[i - 1];
var t2 = xdata[i];
if (t2 === undefined) return undefined;
var v1 = (i === 0) ? ydata[0] : ydata[i - 1];
var v2 = ydata[i];
var v = v1;
if (x != t1) v += (x - t1) * (v2 - v1) / (t2 - t1);
return v;
}
}
}
// called from mousemove event handler. Canvas is the jQuery object
function measure(canvas,ex) {
// no data, nothing to measure :)
if (c.x_data === undefined) return;
var c = canvas[0];
var offset = canvas.offset();
var mx = ex - offset.left;
var xintersect = mx*c.x_scale + c.x_data[0];
var yintersect = interpolate(c.x_data,c.y_data,xintersect);
if (yintersect === undefined) return;
var my = (c.y_max - yintersect)/c.y_scale;
// redraw background followed by vertical cursor overlay
var ctx = c.getContext('2d');
ctx.drawImage(c.bg_image,0,0);
ctx.lineWidth = 0.5;
ctx.strokeStyle = "#C0C0C0";
ctx.beginPath();
ctx.moveTo(mx,0);
ctx.lineTo(mx,c.height);
ctx.moveTo(0,my);
ctx.lineTo(c.width,my);
ctx.stroke();
// add intersection information
ctx.font = "10px Arial";
ctx.fillStyle = "black";
ctx.fillText(xintersect.toExponential(2)+","+yintersect.toExponential(2),mx,my);
}
function setup(div) {
var canvas = $('<canvas></canvas>');
var div1 = $('<div class="control"></div>');
var div2 = $('<div class="control"></div>');
var button = $('<button>Plot</button>');
var expression = $('<input type="text" id="expression"></input>');
div1.append('f(x):',expression);
var minx = $('<input type="text"></input>');
var maxx = $('<input type="text"></input>');
div2.append('min x:',minx,'max x:',maxx);
button.on("click",function() {
canvas[0].width = canvas.width();
canvas[0].height = canvas.height();
var exp = expression.val();
var x1 = minx.val();
var x2 = maxx.val();
graph(canvas[0],exp,x1,x2);
});
canvas.on("mousemove",function(event) {
measure(canvas,event.pageX);
});
$(div).append(canvas,div1,div2,button);
}
exports.setup = setup;
return exports;
}());
$(document).ready(function() {
$('.graphcalc').each(function() {
graphcalc.setup(this);
});
});