// Interactive report static ID
const IG_NAME = '#IR_STATIC_ID';
// This function expects the first column to be a string and the rest of the columns to be numbers.
// It will ignore any columns that have text.
$(function(){
// Get column values for all rows and place it in the cols array
var cols = []
var trs = $(IG_NAME + ' tr')
var data =$.each(trs , function(index, tr){
$.each($(tr).find("td").not(":first"), function(index, td){
cols[index] = cols[index] || [];
if(!isNaN($(td).text().replace(',',''))){
cols[index].push(parseInt($(td).text().replace(',',''), 10))
}
})
});
cols.forEach(function(col, index)
{
let columnData = new ColumnData(col);
$(IG_NAME + ' tr').find('td:eq('+(index+1)+')').each(function(i, td)
{
if(!isNaN($(td).text().replace(',','')))
{
columnData.createTooltip($(td));
}
})
})
})
class ColumnData {
constructor(col) {
this.max = Math.max.apply(null, col);
this.min = Math.min.apply(null, col);
this.total = col.reduce((a, b) => a + b, 0);
this.avg = (this.total/ col.length).toFixed(0);
this.col = col;
this.col.sort(function(a, b){return (b-a)});
}
valueAsNumber(cell) {
return parseInt((cell.text()).replace(',',''), 10);
}
ranking(cell)
{
return this.col.findIndex(rank => rank === this.valueAsNumber(cell)) +1;
}
maxDifference(cell)
{
return this.max - this.valueAsNumber(cell);
}
minDifference(cell)
{
return this.valueAsNumber(cell) - this.min;
}
percent(cell)
{
return ((this.valueAsNumber(cell)/this.total)*100).toFixed(0);
}
percentColour(cell)
{
if (this.valueAsNumber(cell) < this.avg)
{
return "red";
}
return "green";
}
createTooltip(cell)
{
cell.attr('title', '');
cell.tooltip({
content: "<table border='1'><tr>"+
//////////////////////////////////////////////
// HEADER row
//////////////////////////////////////////////
"<th class='celldata'>Value</th>"+
"<th class='celldata'>Rank</th>"+
"<th class='celldata'>% of total</th>"+
"<th class='celldata'>Avg</th>"+
"<th>Overal total</th>"+
"<th>Min value</th><th>> Min</th>"+
"<th>Max value</th><th>< Max</th></tr>"+
//////////////////////////////////////////////
// DATA row
//////////////////////////////////////////////
"<tr><td style='font-size: 150%;'><b>"+this.valueAsNumber(cell)+"</b></td>"+
"<td>"+this.ranking(cell)+"</td>"+
"<td>"+this.percent(cell)+"%</td>"+
"<td style='font-size: 100%;' class='"+this.percentColour(cell)+"'</td>"+this.avg+"</td>"+
"<td>"+this.total+"</td>"+
"<td>"+this.min+"</td><td>"+this.minDifference(cell)+"</td>"+
"<td>"+this.max+"</td><td>"+this.maxDifference(cell)+"</<td>"+
"</tr></table>"
});
}}
Any questions, post a comment and I will try and answer it.
Comments
Post a Comment