~lss-team/lilsoftstats/trunk

« back to all changes in this revision

Viewing changes to js/jquery/jquery.graphTable-0.2.js

  • Committer: Nick
  • Date: 2011-11-14 04:10:28 UTC
  • Revision ID: nick@little-apps.org-20111114041028-cvmpwq6z6hx3pkya
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(function($) { 
 
2
 
 
3
 $.fn.graphTable = function(_graphArgs,_flotArgs) {
 
4
 
 
5
    var args = {
 
6
 
 
7
      /* 
 
8
       * options for reading the table -- defaults will work in most cases except
 
9
       * you'll want to override the default args.series if your series are in columns 
 
10
       * 
 
11
       * note that anywhere the word "index" is used, the count starts from 0 at
 
12
       * the top left of the table 
 
13
       *
 
14
       */
 
15
      series: 'columns', // are the series in rows or columns?
 
16
      labels: 0, // index of the cell in the series row/column that contains the label for the series
 
17
      xaxis: 0, // index of the row/column (whatever args.series is) that contains the x values
 
18
      firstSeries: 1, // index of the row/column containing the first series
 
19
      lastSeries: null, // index of the row/column containing the last series; will use the last cell in the row/col if not set
 
20
      dataStart: 1, // index of the first cell in the series containing data
 
21
      dataEnd: null, // index of the last cell in the series containing data; will use the last cell in the row/col if not set
 
22
 
 
23
      /* graph size and position */
 
24
      position: 'after', // before the table, after the table, or replace the table
 
25
      width: null, // set to null to use the width of the table
 
26
      height: null, // set to null to use the height of the table
 
27
      min: 0, // defaults to minimum y value in the table
 
28
      max: 0, // defaults to maximum y value in the table
 
29
 
 
30
      /* data transformation before plotting */
 
31
      dataTransform: null, // function to run on cell contents before passing to flot; string -> string
 
32
      labelTransform: null, // function to run on cell contents before passing to flot; string -> string
 
33
      xaxisTransform: null, // function to run on cell contents before passing to flot; string -> string
 
34
 
 
35
      //extra info added by me
 
36
      colors: null
 
37
    }
 
38
 
 
39
    // override defaults with user args
 
40
    $.extend(true,args,_graphArgs);
 
41
    
 
42
    /* default to last cell in the row/col for 
 
43
     * lastSeries and dataEnd if they haven't been set yet */
 
44
 
 
45
    // index of the row/column containing the last series
 
46
    if (! args.lastSeries) {
 
47
      args.lastSeries = (args.series == 'columns') ? 
 
48
        $('tr',$(this)).eq(args.labels).find('th,td').length - 1 : 
 
49
        $('tr',$(this)).length - 1;  
 
50
    }
 
51
 
 
52
    // index of the last cell in the series containing data
 
53
    if (! args.dataEnd) {
 
54
      args.dataEnd = (args.series == 'rows') ? 
 
55
        $('tr',$(this)).eq(args.firstSeries).find('th,td').length - 1:
 
56
        $('tr',$(this)).length - 1;
 
57
    }
 
58
 
 
59
    return $(this).each(function() {
 
60
      // use local min/max for y of each graph, based on initial args
 
61
      var $table = $(this);
 
62
 
 
63
      // make sure the table is a table!
 
64
      if (! $table.is('table')) { return; }
 
65
 
 
66
      // if no height and width have been set, then set 
 
67
      // width and height based on the width and height of the table
 
68
      if (! args.width) { args.width = $table.width(); }
 
69
      if (! args.height) { args.height = $table.height(); }
 
70
 
 
71
      var min = args.min;
 
72
      var max = args.max;
 
73
      var $rows = $('tr',$table);
 
74
      var tableData = new Array();
 
75
 
 
76
      switch (args.series) {
 
77
        case 'rows':
 
78
 
 
79
          var $xaxisRow = $rows.eq(args.xaxis);
 
80
          
 
81
          // iterate over each of the rows in the series
 
82
          for (i=args.firstSeries;i<=args.lastSeries;i++) {
 
83
            var rowData = new Array();
 
84
 
 
85
            $dataRow = $('tr',$table).eq(i);
 
86
 
 
87
            // get the label for the whole row
 
88
            var label = $('th,td',$dataRow).eq(args.labels).text();
 
89
 
 
90
            if (args.labelTransform) { label = args.labelTransform(label); }
 
91
 
 
92
            for (j=args.dataStart;j<=args.dataEnd;j++) {
 
93
              var x = $('th,td',$xaxisRow).eq(j).text();
 
94
              var y = $('th,td',$dataRow).eq(j).text();
 
95
 
 
96
              if (args.dataTransform) { y = args.dataTransform(y); }
 
97
              if (args.xaxisTransform) { x = args.xaxisTransform(x); }
 
98
              
 
99
              test_x = parseFloat(x);
 
100
              test_y = parseFloat(y);
 
101
 
 
102
              if (test_y < min) { min = test_y; }
 
103
              else if (test_y > max) { max = test_y; }
 
104
 
 
105
              rowData[rowData.length] = [x,y];
 
106
            }
 
107
 
 
108
            tableData[tableData.length] = { label: label, data: rowData };
 
109
 
 
110
          }
 
111
 
 
112
          break;
 
113
 
 
114
 
 
115
        case 'columns':
 
116
          // iterate over each of the columns in the series
 
117
          var $labelRow = $rows.eq(args.labels);
 
118
 
 
119
          for (j=args.firstSeries;j<=args.lastSeries;j++) { // j designates the column
 
120
            var colData = new Array();
 
121
 
 
122
            var label = $labelRow.find('th,td').eq(j).text();
 
123
            if (args.labelTransform) { label = args.labelTransform(label); }
 
124
 
 
125
            for (i=args.dataStart;i<=args.dataEnd;i++) { // i designates the row
 
126
              $cell = $rows.eq(i).find('th,td').eq(j);
 
127
              var y = $cell.text();
 
128
              var x = $rows.eq(i).find('th,td').eq(args.xaxis).text();
 
129
 
 
130
              if (args.dataTransform) { y = args.dataTransform(y); }
 
131
              if (args.xaxisTransform) { x = args.xaxisTransform(x); }
 
132
              
 
133
              test_x = parseFloat(x);
 
134
              test_y = parseFloat(y);
 
135
 
 
136
              if (test_y < min) { min = test_y; }
 
137
              else if (test_y > max) { max = test_y; }
 
138
 
 
139
              colData[colData.length] = [x,y];
 
140
            }
 
141
 
 
142
              //changed/added this code
 
143
              var series = {label: label, data: colData };
 
144
              if (args.colors && args.colors[j-args.dataStart]){
 
145
                  series.color = args.colors[j-args.dataStart]
 
146
              }
 
147
              tableData[tableData.length] = series;
 
148
 
 
149
 
 
150
          }
 
151
 
 
152
          break;
 
153
      }
 
154
 
 
155
      switch (args.position) {
 
156
        case 'after':
 
157
      $div = $('<div class="flot-graph" />').insertAfter($table);
 
158
          break;
 
159
 
 
160
        case 'replace':
 
161
      $div = $('<div class="flot-graph" />').insertAfter($table);
 
162
          $table.remove();
 
163
          break;
 
164
 
 
165
        default:
 
166
      $div = $('<div class="flot-graph" />').insertBefore($table);
 
167
          break;
 
168
      }
 
169
 
 
170
      var flotArgs = { yaxis: { min: min, max: max }, title: 'foo' };
 
171
 
 
172
      $div.width(args.width).height(args.height);
 
173
      $.extend(true,flotArgs,_flotArgs);
 
174
      $.plot($div, tableData, flotArgs);
 
175
 
 
176
    });
 
177
  };
 
178
 
 
179
})(jQuery);
 
 
b'\\ No newline at end of file'