3 * Copyright 2015 Petr Shevtsov (petr.shevtsov@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * On pan/zoom event, each series will rebase to a specified value (e.g. 100) at the
9 * start of the displayed period.
11 * See http://stats.oecd.org/glossary/detail.asp?ID=2249
14 * Value to rebase. Must be either Number or 'percent' or null.
16 * See tests/straw-broom.html for demo.
19 /*global Dygraph:false */
24 Dygraph
.DataHandlers
.RebaseHandler
= function(baseOpt
) {
25 this.baseOpt
= baseOpt
;
28 var RebaseHandler
= Dygraph
.DataHandlers
.RebaseHandler
;
29 RebaseHandler
.prototype = new Dygraph
.DataHandlers
.DefaultHandler();
31 RebaseHandler
.rebase
= function(value
, initial
, base
) {
32 if (base
=== "percent") {
33 return (value
/ initial
- 1) * 100;
35 return value
* base
/ initial
;
38 RebaseHandler
.prototype.getExtremeYValues
= function(series
, dateWindow
, options
) {
39 var minY
= null, maxY
= null, y
;
40 var firstIdx
= 0, lastIdx
= series
.length
- 1;
41 var initial
= series
[firstIdx
][1];
43 for (var j
= firstIdx
; j
<= lastIdx
; j
++) {
45 y
= (this.baseOpt
=== "percent") ? 0 : this.baseOpt
;
47 y
= RebaseHandler
.rebase(series
[j
][1], initial
, this.baseOpt
);
49 if (y
=== null || isNaN(y
))
51 if (maxY
=== null || y
> maxY
) {
54 if (minY
=== null || y
< minY
) {
58 return [ minY
, maxY
];
61 RebaseHandler
.prototype.seriesToPoints
= function(series
, setName
, boundaryIdStart
){
64 var lastIdx
= series
.length
- 1;
65 var initial
= series
[firstIdx
][1]; // TODO: check for null
66 for (var i
= 0; i
<= lastIdx
; ++i
) {
69 var yval
= yraw
=== null ? null : Dygraph
.DataHandler
.parseFloat(yraw
);
72 yval
= (this.baseOpt
=== "percent") ? 0 : this.baseOpt
;
74 yval
= RebaseHandler
.rebase(yval
, initial
, this.baseOpt
);
80 xval
: Dygraph
.DataHandler
.parseFloat(item
[0]),
83 idx
: i
+ boundaryIdStart
87 this.onPointsCreated_(series
, points
);
91 Dygraph
.Plugins
.Rebase
= (function() {
92 var rebase
= function(baseOpt
) {
93 var isNum
= function(v
) {
94 return !isNaN(v
) && (typeof v
=== 'number' || {}.toString
.call(v
) === '[object Number]');
96 if (baseOpt
=== "percent" || isNum(baseOpt
)) {
97 this.baseOpt_
= baseOpt
;
103 rebase
.prototype.toString
= function() {
104 return "Rebase Plugin";
107 rebase
.prototype.activate
= function(g
) {
108 if (this.baseOpt_
=== null) {
112 predraw
: this.predraw
116 rebase
.prototype.predraw
= function(e
) {
119 if (this.baseOpt_
=== "percent") {
123 axisLabelFormatter
: function(y
) {
126 valueFormatter
: function(y
) {
127 return Math
.round(y
* 100) / 100 + '%';
134 g
.dataHandler_
= new Dygraph
.DataHandlers
.RebaseHandler(this.baseOpt_
);