3
$.fn.graphTable = function(_graphArgs,_flotArgs) {
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
11
* note that anywhere the word "index" is used, the count starts from 0 at
12
* the top left of the table
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
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
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
35
//extra info added by me
39
// override defaults with user args
40
$.extend(true,args,_graphArgs);
42
/* default to last cell in the row/col for
43
* lastSeries and dataEnd if they haven't been set yet */
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;
52
// index of the last cell in the series containing data
54
args.dataEnd = (args.series == 'rows') ?
55
$('tr',$(this)).eq(args.firstSeries).find('th,td').length - 1:
56
$('tr',$(this)).length - 1;
59
return $(this).each(function() {
60
// use local min/max for y of each graph, based on initial args
63
// make sure the table is a table!
64
if (! $table.is('table')) { return; }
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(); }
73
var $rows = $('tr',$table);
74
var tableData = new Array();
76
switch (args.series) {
79
var $xaxisRow = $rows.eq(args.xaxis);
81
// iterate over each of the rows in the series
82
for (i=args.firstSeries;i<=args.lastSeries;i++) {
83
var rowData = new Array();
85
$dataRow = $('tr',$table).eq(i);
87
// get the label for the whole row
88
var label = $('th,td',$dataRow).eq(args.labels).text();
90
if (args.labelTransform) { label = args.labelTransform(label); }
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();
96
if (args.dataTransform) { y = args.dataTransform(y); }
97
if (args.xaxisTransform) { x = args.xaxisTransform(x); }
99
test_x = parseFloat(x);
100
test_y = parseFloat(y);
102
if (test_y < min) { min = test_y; }
103
else if (test_y > max) { max = test_y; }
105
rowData[rowData.length] = [x,y];
108
tableData[tableData.length] = { label: label, data: rowData };
116
// iterate over each of the columns in the series
117
var $labelRow = $rows.eq(args.labels);
119
for (j=args.firstSeries;j<=args.lastSeries;j++) { // j designates the column
120
var colData = new Array();
122
var label = $labelRow.find('th,td').eq(j).text();
123
if (args.labelTransform) { label = args.labelTransform(label); }
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();
130
if (args.dataTransform) { y = args.dataTransform(y); }
131
if (args.xaxisTransform) { x = args.xaxisTransform(x); }
133
test_x = parseFloat(x);
134
test_y = parseFloat(y);
136
if (test_y < min) { min = test_y; }
137
else if (test_y > max) { max = test_y; }
139
colData[colData.length] = [x,y];
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]
147
tableData[tableData.length] = series;
155
switch (args.position) {
157
$div = $('<div class="flot-graph" />').insertAfter($table);
161
$div = $('<div class="flot-graph" />').insertAfter($table);
166
$div = $('<div class="flot-graph" />').insertBefore($table);
170
var flotArgs = { yaxis: { min: min, max: max }, title: 'foo' };
172
$div.width(args.width).height(args.height);
173
$.extend(true,flotArgs,_flotArgs);
174
$.plot($div, tableData, flotArgs);
b'\\ No newline at end of file'