Skip to content

Commit ceff5a3

Browse files
committed
Merge branch 'flot'
2 parents 6e0c822 + 47d632a commit ceff5a3

File tree

8 files changed

+166
-3
lines changed

8 files changed

+166
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ COMPILER = ${JS_ENGINE} ${BUILD_DIR}/uglify.js --unsafe
1010

1111
BASE_FILES = ${SRC_DIR}/core.js\
1212
${SRC_DIR}/distribution.js\
13-
${SRC_DIR}/special.js
13+
${SRC_DIR}/special.js\
14+
${SRC_DIR}/plugin/flot.jstat.js
1415

1516
JS = ${DIST_DIR}/jstat.js
1617
JS_MIN = ${DIST_DIR}/jstat.min.js

src/core.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,31 @@ var slice = Array.prototype.slice,
2626
// test if object
2727
isObject = function( arg ) {
2828
return toString.call( arg ) === "[object Object]";
29+
},
30+
31+
// calculate correction for IEEE
32+
calcRdx = function( n, m ) {
33+
var val = n > m ? n : m;
34+
return Math.pow( 10, 15 - ~~( Math.log((( val > 0 ) ? val : -val )) * Math.LOG10E ));
2935
};
3036

37+
// implement bind if browser doesn't natively support it
38+
if ( !Function.prototype.bind ) {
39+
Function.prototype.bind = function( obj ) {
40+
var slice = [].slice,
41+
args = slice.call( arguments, 1 ),
42+
self = this,
43+
nop = function() {},
44+
bound = function() {
45+
return self.apply( this instanceof nop ? this : ( obj || {} ),
46+
args.concat( slice.call( arguments ))
47+
);
48+
};
49+
bound.prototype = this.prototype;
50+
return bound;
51+
};
52+
}
53+
3154
// global function
3255
function jStat() {
3356
return new jStat.fn.init( arguments );
@@ -67,7 +90,7 @@ jStat.fn = jStat.prototype = {
6790
},
6891

6992
// only to be used internally
70-
push : Array.prototype.push,
93+
push : [].push,
7194
sort : [].sort,
7295
splice : [].splice
7396
};
@@ -273,7 +296,7 @@ jStat.extend({
273296
// generate sequence
274297
seq : function( min, max, length, func ) {
275298
var arr = [],
276-
hival = 1e15, // simple fix for IEEE floating point errors
299+
hival = calcRdx( min, max ),
277300
step = ( max * hival - min * hival ) / (( length - 1 ) * hival ),
278301
current = min,
279302
cnt = 0;

src/distribution.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ jStat.beta = function( alpha, beta ) {
55
if (!( this instanceof arguments.callee )) return new jStat.beta( alpha, beta );
66
this.alpha = alpha;
77
this.beta = beta;
8+
9+
for ( var i in jStat.beta.prototype ) this[i] = this[i].bind( this );
810
};
911

1012
// extend beta function with static methods
@@ -76,6 +78,8 @@ jStat.cauchy = function( local, scale ) {
7678
if (!( this instanceof arguments.callee )) return new jStat.cauchy( local, scale );
7779
this.local = local;
7880
this.scale = scale;
81+
82+
for ( var i in jStat.cauchy.prototype ) this[i] = this[i].bind( this );
7983
};
8084

8185
// extend cauchy function with static methods
@@ -144,6 +148,8 @@ jStat.cauchy.prototype.sample = function( arr ) {
144148
jStat.chisquare = function( dof ) {
145149
if (!( this instanceof arguments.callee )) return new jStat.chisquare( dof );
146150
this.dof = dof;
151+
152+
for ( var i in jStat.chisquare.prototype ) this[i] = this[i].bind( this );
147153
};
148154

149155
// extend chisquare function with static methods
@@ -213,6 +219,8 @@ jStat.chisquare.prototype.sample = function( arr ) {
213219
jStat.exponential = function( rate ) {
214220
if (!( this instanceof arguments.callee )) return new jStat.exponential( rate );
215221
this.rate = rate;
222+
223+
for ( var i in jStat.exponential.prototype ) this[i] = this[i].bind( this );
216224
};
217225

218226
// extend exponential function with static methods
@@ -282,6 +290,8 @@ jStat.gamma = function( shape, scale ) {
282290
if (!( this instanceof arguments.callee )) return new jStat.gamma( shape, scale );
283291
this.shape = shape;
284292
this.scale = scale;
293+
294+
for ( var i in jStat.gamma.prototype ) this[i] = this[i].bind( this );
285295
};
286296

287297
// extend gamma function with static methods
@@ -348,6 +358,8 @@ jStat.kumaraswamy = function( alpha, beta ) {
348358
if (!( this instanceof arguments.callee )) return new jStat.kumaraswamy( alpha, beta );
349359
this.alpha = alpha;
350360
this.beta = beta;
361+
362+
for ( var i in jStat.kumaraswamy.prototype ) this[i] = this[i].bind( this );
351363
};
352364

353365
// extend kumaraswamy function with static methods
@@ -399,6 +411,8 @@ jStat.lognormal = function( mu, sigma ) {
399411
if (!( this instanceof arguments.callee )) return new jStat.lognormal( mu, sigma );
400412
this.mu = mu;
401413
this.sigma = sigma;
414+
415+
for ( var i in jStat.lognormal.prototype ) this[i] = this[i].bind( this );
402416
};
403417

404418
// extend lognormal function with static methods
@@ -468,6 +482,8 @@ jStat.normal = function( mean, std ) {
468482
if (!( this instanceof arguments.callee )) return new jStat.normal( mean, std );
469483
this.mean = mean;
470484
this.std = std;
485+
486+
for ( var i in jStat.normal.prototype ) this[i] = this[i].bind( this );
471487
};
472488

473489
// extend normal function with static methods
@@ -537,6 +553,8 @@ jStat.pareto = function( scale, shape ) {
537553
if (!( this instanceof arguments.callee )) return new jStat.pareto( scale, shape );
538554
this.scale = scale;
539555
this.shape = shape;
556+
557+
for ( var i in jStat.pareto.prototype ) this[i] = this[i].bind( this );
540558
};
541559

542560
// extend pareto function with static methods
@@ -588,6 +606,8 @@ jStat.extend( jStat.pareto, {
588606
jStat.studentt = function( dof ) {
589607
if (!( this instanceof arguments.callee )) return new jStat.studentt( dof );
590608
this.dof = dof;
609+
610+
for ( var i in jStat.studentt.prototype ) this[i] = this[i].bind( this );
591611
};
592612

593613
// extend studentt function with static methods
@@ -660,6 +680,8 @@ jStat.weibull = function( scale, shape ) {
660680
if (!( this instanceof arguments.callee )) return new jStat.weibull( scale, shape );
661681
this.scale = scale;
662682
this.shape = shape;
683+
684+
for ( var i in jStat.weibull.prototype ) this[i] = this[i].bind( this );
663685
};
664686

665687
// extend weibull function with static methods
@@ -729,6 +751,8 @@ jStat.uniform = function( a, b ) {
729751
if (!( this instanceof arguments.callee )) return new jStat.uniform( a, b );
730752
this.a = a;
731753
this.b = b;
754+
755+
for ( var i in jStat.uniform.prototype ) this[i] = this[i].bind( this );
732756
};
733757

734758
// extend uniform function with static methods

src/plugin/flot.jstat.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function( jStat, jQuery ) {
2+
3+
// options:
4+
// - start: beginning of graph
5+
// - stop: end of graph
6+
// - step: number of steps in the graph
7+
// - flotopts : flot specific options
8+
var defaults = {
9+
start : 0,
10+
stop : 1,
11+
step : 101,
12+
flotopts : {}
13+
};
14+
15+
jStat.flot = function( selector, funcs, opts ) {
16+
opts = jQuery.extend( true, {}, defaults, opts );
17+
18+
var data = [],
19+
i = 0;
20+
21+
if ( jQuery.isFunction( funcs[0] )) {
22+
for ( ; i < funcs.length; i++ )
23+
data.push( jStat.seq( opts.start, opts.stop, opts.step, function( x ) { return [ x, funcs[i]( x )]; }));
24+
} else {
25+
for ( ; i < funcs.length; i++ ) {
26+
funcs[i].data = jStat.seq( opts.start, opts.stop, opts.step, function( x ) { return [ x, funcs[i].data( x )]; });
27+
}
28+
data = funcs;
29+
}
30+
31+
return jQuery.plot( selector, data, opts.flotopts);
32+
};
33+
34+
})( this.jStat, this.jQuery );

test/css/flot.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.graph-example{
2+
width: 600px;
3+
height: 400px;
4+
}

test/flot.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>Flot Tests</title>
6+
<link rel="stylesheet" href="css/flot.css" />
7+
<script src="js/jquery.min.js"></script>
8+
<script src="js/jquery.flot.min.js"></script>
9+
<script src="../src/core.js"></script>
10+
<script src="../src/special.js"></script>
11+
<script src="../src/distribution.js"></script>
12+
<script src="../src/plugin/flot.jstat.js"></script>
13+
<script src="js/tests/flot-tests.js"></script>
14+
</head>
15+
<body>
16+
<h1>Basic Examples</h1>
17+
<h2>Beta Distriubtion</h2>
18+
<div class="graph-example" id="betadiv"></div>
19+
<h2>Cauchy Distriubtion</h2>
20+
<div class="graph-example" id="cauchydiv"></div>
21+
<h2>Student T Distriubtion</h2>
22+
<div class="graph-example" id="studenttdiv"></div>
23+
</body>
24+
</html>

test/js/jquery.flot.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/js/tests/flot-tests.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
$(function() {
2+
var betaInst = jStat.beta( 3, 4 );
3+
j$.flot( '#betadiv', [{
4+
data : betaInst.pdf,
5+
label : 'PDF'
6+
},{
7+
data : betaInst.cdf,
8+
label : 'CDF',
9+
yaxis : 2
10+
}],{
11+
flotopts : {
12+
yaxes : [{}, { position : 'right' }]
13+
}
14+
});
15+
16+
var cauchyInst = jStat.cauchy( 3, 4 );
17+
j$.flot( '#cauchydiv', [{
18+
data : cauchyInst.pdf,
19+
label : 'PDF'
20+
},{
21+
data : cauchyInst.cdf,
22+
label : 'CDF',
23+
yaxis : 2
24+
}],{
25+
start : -20,
26+
stop : 20,
27+
flotopts : {
28+
yaxes : [{}, { position : 'right' }]
29+
}
30+
});
31+
32+
var stInst = jStat.studentt( 4 );
33+
j$.flot( '#studenttdiv', [{
34+
data : stInst.pdf,
35+
label : 'PDF'
36+
},{
37+
data : stInst.cdf,
38+
label : 'CDF',
39+
yaxis : 2
40+
}],{
41+
start : -6,
42+
stop : 6,
43+
flotopts : {
44+
yaxes : [{}, { position : 'right' }]
45+
}
46+
});
47+
});

0 commit comments

Comments
 (0)