File: multicolumngroup.js

package info (click to toggle)
r-cran-dygraphs 1.1.1.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,232 kB
  • sloc: javascript: 10,787; sh: 19; makefile: 15
file content (81 lines) | stat: -rw-r--r-- 2,356 bytes parent folder | download | duplicates (3)
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
/**
 * Multi-column Bar Chart plotter is adapted from http://dygraphs.com/tests/plotters.html
 * 
 * Modified to apply only to a supplied group of sets
 */

  // Multiple column bar chart
function multiColumnGroupPlotter(e) {

  // BEGIN HEADER BLOCK
  // This first block can be copied to other plotters to capture the group 
  var g = e.dygraph;
  
  var group;
  var groupIdx = [];
  var sets = [];
  var allSets = e.allSeriesPoints;
  var minIdx = Infinity;
  var setName = e.setName;
  var setNames = g.getLabels().slice(1);
  
  var currGroup = g.attr_("group", setName);
  
  for (var setIdx = 0; setIdx < allSets.length; setIdx++) {
    // get the name and group of the current setIdx
    setName = setNames[setIdx];
    group = g.attr_("group", setName);

    if (group === currGroup) {
      //save the indv index and the points
      groupIdx.push(setIdx);
      sets.push(allSets[setIdx]);
      
      // capturing the min indx helps to ensure we don't render the plotter
      // multiple times
      if (setIdx < minIdx) minIdx = setIdx;
    }
  }
  
  // We'll employ the plotter only on the first of the group
  if (e.seriesIndex !== minIdx) return;
  // END HEADER BLOCK
 
  var ctx = e.drawingContext;
  var axis = g.attr_("axis", e.setName);
  var y_bottom = g.toDomYCoord(0, axis == "y2" ? 1 : 0);

  // Find the minimum separation between x-values.
  // This determines the bar width.
  var min_sep = Infinity;
  for (var j = 0; j < sets.length; j++) {
    var points = sets[j];
    for (var i = 1; i < points.length; i++) {
      var sep = points[i].canvasx - points[i - 1].canvasx;
      if (sep < min_sep) min_sep = sep;
    }
  }
  var bar_width = Math.floor(2.0 / 3 * min_sep);

  var fillColors = [];
  var strokeColors = g.getColors();
  for (var i = 0; i < groupIdx.length; i++) {
    fillColors.push(strokeColors[groupIdx[i]]);
  }

  for (var j = 0; j < sets.length; j++) {
    ctx.fillStyle = fillColors[j];
    ctx.strokeStyle = fillColors[j];
    for (var i = 0; i < sets[j].length; i++) {
      var p = sets[j][i];
      var center_x = p.canvasx;
      var x_left = center_x - (bar_width / 2) * (1 - j/(sets.length-1));

      ctx.fillRect(x_left, p.canvasy,
        bar_width/sets.length, y_bottom - p.canvasy);

      ctx.strokeRect(x_left, p.canvasy,
        bar_width/sets.length, y_bottom - p.canvasy);
    }
  }
}