Skip to content

Commit 909243b

Browse files
committed
basic flot api support
1 parent 62dcf30 commit 909243b

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed

src/plugin/flot.jstat.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
(function( jStat, jQuery ) {
22

3-
jStat.plot = function( selector, opts ) {
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.plot = 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+
}
430

31+
return jQuery.plot( selector, data, opts.flotopts);
532
};
633

734
})( this.jStat, this.jQuery );

test/js/tests/flot-tests.js

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
$(function() {
22
var betaInst = jStat.beta( 3, 4 );
3-
$.plot( '#betadiv', [
4-
{
5-
data : jStat.seq( 0, 1, 101, function( x ) { return [ x, betaInst.pdf( x )];}),
6-
label : 'PDF'
7-
},{
8-
data : jStat.seq( 0, 1, 101, function( x ) { return [ x, betaInst.cdf( x )];}),
9-
label : 'CDF',
10-
yaxis : 2
11-
}],{
3+
j$.plot( '#betadiv', [{
4+
data : betaInst.pdf,
5+
label : 'PDF'
6+
},{
7+
data : betaInst.cdf,
8+
label : 'CDF',
9+
yaxis : 2
10+
}],{
11+
flotopts : {
1212
yaxes : [{}, { position : 'right' }]
13+
}
1314
});
1415

1516
var cauchyInst = jStat.cauchy( 3, 4 );
16-
$.plot( '#cauchydiv', [
17-
{
18-
data : jStat.seq( -20, 20, 101, function( x ) { return [ x, cauchyInst.pdf( x )];}),
19-
label : 'PDF'
20-
},{
21-
data : jStat.seq( -20, 20, 101, function( x ) { return [ x, cauchyInst.cdf( x )];}),
22-
label : 'CDF',
23-
yaxis : 2
24-
}],{
17+
j$.plot( '#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 : {
2528
yaxes : [{}, { position : 'right' }]
29+
}
2630
});
2731

2832
var stInst = jStat.studentt( 4 );
29-
$.plot( '#studenttdiv', [
30-
{
31-
data : jStat.seq( -6, 6, 101, function( x ) { return [ x, stInst.pdf( x, 4 )];}),
32-
label : 'PDF'
33-
},{
34-
data : jStat.seq( -6, 6, 101, function( x ) { return [ x, stInst.cdf( x, 4 )];}),
35-
label : 'CDF',
36-
yaxis : 2
37-
}],{
33+
j$.plot( '#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 : {
3844
yaxes : [{}, { position : 'right' }]
45+
}
3946
});
4047
});

0 commit comments

Comments
 (0)