src/pyams_skin/resources/js/ext/flot/jquery.flot.threshold.js
changeset 566 a1707c607eec
parent 565 318533413200
child 567 bca1726b1d85
equal deleted inserted replaced
565:318533413200 566:a1707c607eec
     1 /* Flot plugin for thresholding data.
       
     2 
       
     3 Copyright (c) 2007-2014 IOLA and Ole Laursen.
       
     4 Licensed under the MIT license.
       
     5 
       
     6 The plugin supports these options:
       
     7 
       
     8     series: {
       
     9         threshold: {
       
    10             below: number
       
    11             color: colorspec
       
    12         }
       
    13     }
       
    14 
       
    15 It can also be applied to a single series, like this:
       
    16 
       
    17     $.plot( $("#placeholder"), [{
       
    18         data: [ ... ],
       
    19         threshold: { ... }
       
    20     }])
       
    21 
       
    22 An array can be passed for multiple thresholding, like this:
       
    23 
       
    24     threshold: [{
       
    25         below: number1
       
    26         color: color1
       
    27     },{
       
    28         below: number2
       
    29         color: color2
       
    30     }]
       
    31 
       
    32 These multiple threshold objects can be passed in any order since they are
       
    33 sorted by the processing function.
       
    34 
       
    35 The data points below "below" are drawn with the specified color. This makes
       
    36 it easy to mark points below 0, e.g. for budget data.
       
    37 
       
    38 Internally, the plugin works by splitting the data into two series, above and
       
    39 below the threshold. The extra series below the threshold will have its label
       
    40 cleared and the special "originSeries" attribute set to the original series.
       
    41 You may need to check for this in hover events.
       
    42 
       
    43 */
       
    44 
       
    45 (function ($) {
       
    46     var options = {
       
    47         series: { threshold: null } // or { below: number, color: color spec}
       
    48     };
       
    49 
       
    50     function init(plot) {
       
    51         function thresholdData(plot, s, datapoints, below, color) {
       
    52             var ps = datapoints.pointsize, i, x, y, p, prevp,
       
    53                 thresholded = $.extend({}, s); // note: shallow copy
       
    54 
       
    55             thresholded.datapoints = { points: [], pointsize: ps, format: datapoints.format };
       
    56             thresholded.label = null;
       
    57             thresholded.color = color;
       
    58             thresholded.threshold = null;
       
    59             thresholded.originSeries = s;
       
    60             thresholded.data = [];
       
    61 
       
    62             var origpoints = datapoints.points,
       
    63                 addCrossingPoints = s.lines.show;
       
    64 
       
    65             var threspoints = [];
       
    66             var newpoints = [];
       
    67             var m;
       
    68 
       
    69             for (i = 0; i < origpoints.length; i += ps) {
       
    70                 x = origpoints[i];
       
    71                 y = origpoints[i + 1];
       
    72 
       
    73                 prevp = p;
       
    74                 if (y < below) p = threspoints;
       
    75                 else p = newpoints;
       
    76 
       
    77                 if (addCrossingPoints && prevp !== p &&
       
    78                     x !== null && i > 0 &&
       
    79                     origpoints[i - ps] != null) {
       
    80                     var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
       
    81                     prevp.push(interx);
       
    82                     prevp.push(below);
       
    83                     for (m = 2; m < ps; ++m) {
       
    84                         prevp.push(origpoints[i + m]);
       
    85                     }
       
    86 
       
    87                     p.push(null); // start new segment
       
    88                     p.push(null);
       
    89                     for (m = 2; m < ps; ++m) {
       
    90                         p.push(origpoints[i + m]);
       
    91                     }
       
    92 
       
    93                     p.push(interx);
       
    94                     p.push(below);
       
    95                     for (m = 2; m < ps; ++m) {
       
    96                         p.push(origpoints[i + m]);
       
    97                     }
       
    98                 }
       
    99 
       
   100                 p.push(x);
       
   101                 p.push(y);
       
   102                 for (m = 2; m < ps; ++m) {
       
   103                     p.push(origpoints[i + m]);
       
   104                 }
       
   105             }
       
   106 
       
   107             datapoints.points = newpoints;
       
   108             thresholded.datapoints.points = threspoints;
       
   109 
       
   110             if (thresholded.datapoints.points.length > 0) {
       
   111                 var origIndex = $.inArray(s, plot.getData());
       
   112                 // Insert newly-generated series right after original one (to prevent it from becoming top-most)
       
   113                 plot.getData().splice(origIndex + 1, 0, thresholded);
       
   114             }
       
   115 
       
   116             // FIXME: there are probably some edge cases left in bars
       
   117         }
       
   118 
       
   119         function processThresholds(plot, s, datapoints) {
       
   120             if (!s.threshold) return;
       
   121             if (s.threshold instanceof Array) {
       
   122                 s.threshold.sort(function(a, b) {
       
   123                     return a.below - b.below;
       
   124                 });
       
   125 
       
   126                 $(s.threshold).each(function(i, th) {
       
   127                     thresholdData(plot, s, datapoints, th.below, th.color);
       
   128                 });
       
   129             } else {
       
   130                 thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color);
       
   131             }
       
   132         }
       
   133 
       
   134         plot.hooks.processDatapoints.push(processThresholds);
       
   135     }
       
   136 
       
   137     $.plot.plugins.push({
       
   138         init: init,
       
   139         options: options,
       
   140         name: 'threshold',
       
   141         version: '1.2'
       
   142     });
       
   143 })(jQuery);