Skip to content

Instantly share code, notes, and snippets.

@phoebebright
Last active May 23, 2021 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoebebright/52a233ab617b9b4ed26139df29a52947 to your computer and use it in GitHub Desktop.
Save phoebebright/52a233ab617b9b4ed26139df29a52947 to your computer and use it in GitHub Desktop.
GardenAmendments
<html>
<head>
<script type="text/javascript" src="./tablesorter/js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="./tablesorter/js/jquery.tablesorter.widgets.js"></script>
<style>
.tablesorter .tablesorter-filter-row td:nth-child(4) .tablesorter-filter {
width: 50px;
}
.tablesorter .tablesorter-filter-row td:nth-child(5) .tablesorter-filter {
width: 50px;
}
.tablesorter .tablesorter-filter-row td:nth-child(6) .tablesorter-filter {
width: 50px;
}
</style>
</head>
<body>
<h2>Browse Law Firm Prices</h2>
<hr />
<div id="grid"></div>
<script>
$(document).ready(function() {
d3.csv('./plantjuice.csv', function (error,data) {
function tabulate(data, columns) {
var table = d3.select('#grid').append('table')
var thead = table.append('thead')
var tbody = table.append('tbody');
// append the header row
thead.append('tr')
.selectAll('th')
.data(columns).enter()
.append('th')
.text(function (column) { return column; });
// create a row for each object in the data
var rows = tbody.selectAll('tr')
.data(data)
.enter()
.append('tr');
// create a cell in each row for each column
var cells = rows.selectAll('td')
.data(function (row) {
return columns.map(function (column) {
return {column: column, value: row[column]};
});
})
.enter()
.append('td')
.text(function (d) { return d.value; });
return table;
}
// render the table(s)
tabulate(data, ['date', 'close']); // 2 column table
});
});
</script>
<script>
function build_table(json, cols, num_cols, tableid) {
// get domain of each column
domain = new Array();
for (var i=0;i < num_cols.length; i++) {
// NOTE: assumes values in d are numeric
domain[num_cols[i]] = extent(json, function(d) {
return d[num_cols[i]];
});
}
// not in IE8
//var show_framework = $.inArray(cols, "framework_panel");
//
// for view prices when updating batches
var show_city = (cols[2] != "hourly");
var show_org = (cols[2] != "hourly");
var show_framework = (cols[2] != "hourly");
// build table
var body = $(tableid+" tbody");
$.each(json, function (i, d) {
var tr = '<tr class="org_row" data-org="'+d.org_id+'">';
var td = '<td class="name" style="background-color: white;">' + d.organisation + '</td>';
if (show_framework) {
td = td + '<td class="name" style="background-color: white;">' + d.framework_panel + '</td>';
}
td = td + '<td class="name" style="background-color: white;">' + d.city + '</td>';
td = td + '<td class="number" style="background-color: ' + getcolour(d.tp, domain['tp'][0], domain['tp'][1]) + '";>' + d.tp + '</td>';
td = td + '<td class="number" style="background-color: ' + getcolour(d.solicitor, domain['solicitor'][0], domain['solicitor'][1]) + '";>' + d.solicitor + '</td>';
td = td + '<td class="number" style="background-color: ' + getcolour(d.partner, domain['partner'][0], domain['partner'][1]) + '";>' + d.partner + '</td>';
var row = tr + td + '</tr>';
body.append(row);
});
// add the onclick for org now that everything is initialised
$(".org_row").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
var thisrow = $(this);
var org_id = thisrow.data("org");
// toggle selection of organisation on clicking on row
if (thisrow.hasClass("orgrowselected")) {
deleteOrg(org_id);
} else {
addingOrg(org_id);
}
});
if (show_framework) {
if (tableid == "#lawfirm_prices") {
var filter_rows = { 1: true };
} else {
var filter_rows = { 0: true, 3: true };
}
} else {
if (tableid == "#lawfirm_prices") {
var filter_rows = {};
} else {
var filter_rows = { 0: true };
}
}
$(tableid).tablesorter({
widgets: [ "filter"],
widthFixed : false,
widgetOptions : {
filter_functions : filter_rows,
filter_searchDelay : 1200
}
});
$("#cleanfilters").on("click", function() {
$('.choosers').trigger('filterReset');
});
}
function getcolour(curval, mn, mx) {
/*
http://www.jnathanson.com/blog/client/jquery/heatcolor/index.cfm
set colour based on this values position between min and max
*/
// handle edge case of min = max
if (mn == mx) {
mn = mn - 1;
}
// value between 1 and 0
var position = (curval - mn) / (mx - mn);
// this adds 0.5 at the top to get red, and limits the bottom at x= 1.7 to get purple
var shft = 0.5*position + 1.7*(1-position);
// scale will be multiplied by the cos(x) + 1
// (value from 0 to 2) so it comes up to a max of 255
var scale = 128;
// period is 2Pi
var period = 2*Math.PI;
// x is place along x axis of cosine wave
var x = shft + position * period;
var r = process( Math.floor((Math.cos(x) + 1) * scale) );
var g = process( Math.floor((Math.cos(x+Math.PI/2) + 1) * scale) );
var b = process( Math.floor((Math.cos(x+Math.PI) + 1) * scale) );
return '#' + r + g + b;
}
function process( num ) {
// adjust lightness
var n = Math.floor( num + 0.75 * (256 - num));
// turn to hex
var s = n.toString(16);
// if no first char, prepend 0
s = s.length == 1 ? '0' + s : s;
return s;
}
function deleteOrg(org_id) {
// will be defined in commission template if called from there
if(typeof delOrg == 'function') {
delOrg(org_id)
}
return true;
}
function addingOrg(org_id) {
// will be defined in commission template if called from there
if(typeof addOrg == 'function') {
addOrg(org_id)
}
return true;
}
function tidy_chamber_data(csv){
var data = $.csv.toObjects(csv);
var cleaned = Array();
$.each(data, function(i, d) {
var clean = new Array();
clean.org_id = parseInt(d.org_id);
clean.organisation = d.organisation;
clean.city = d.city;
clean.framework_panel = d.framework_panel;
clean.level_of_court = d.level_of_court;
clean.seniority = d.seniority;
clean.hourly = parseInt(d.hourly);
clean.brief1 = parseInt(d.brief1);
clean.brief34 = parseInt(d.brief34);
clean.brief56 = parseInt(d.brief56);
clean.briefh = parseInt(d.briefh);
clean.dirsconfs = parseInt(d.dirsconfs);
clean.refresher1 = parseInt(d.refresher1);
clean.refresher34 = parseInt(d.refresher34);
clean.refresher56 = parseInt(d.refresher56);
clean.daily_uplift = parseInt(d.daily_uplift);
clean.update__published = d.update__published
cleaned.push(clean);
})
return cleaned;
}
function tidy_lawfirm_data(csv){
var data = $.csv.toObjects(csv);
var cleaned = Array();
$.each(data, function(i, d) {
var clean = new Array();
clean.org_id = parseInt(d.org_id);
clean.organisation = d.organisation;
clean.city = d.city;
clean.framework_panel = d.framework_panel;
clean.tp = parseInt(d.tp);
clean.solicitor = parseInt(d.solicitor);
clean.partner = parseInt(d.partner);
//clean.update__published = d.update__published
cleaned.push(clean);
})
return cleaned;
}
function load_lawfirms(callback) {
/* TODO: should be able to save the tidied data as an array but stringify creating a list of blank lists - sigh.
so saving the csv and undoing it each time. All this for IE8 aghhhh */
/* callback is used to call show_lawfirms() after the table is built */
$("#grid").hide();
var jqxhr = $.get("./plantjuice.csv")
.done(function(csv) {
var data = tidy_lawfirm_data(csv);
// show table
var cols = ['organisation','framework_panel','city','tp','solicitor','partner'];
var num_cols = ['tp','solicitor', 'partner'];
build_table(data, cols, num_cols, "#lawfirm_prices");
if (callback) {
callback();
}
})
.fail(function() {
alert("Unable to load lawfirm pricing. Please try again and if you get this message again, please report to support");
});
}
function load_chambers(panel_id, callback) {
$("#chamber_prices").hide();
$('#chamber_prices > tbody').empty();
// load all
if (panel_id == 0) {
var jqxhr = $.get("/commissioning/api/get_prices_csv/C/")
.done(function(csv) {
var data = tidy_chamber_data(csv);
// show table
var cols = ['level_of_court', 'seniority','organisation','framework_panel','city',
'hourly','dirsconfs', 'briefh','brief1','refresher1','brief34','refresher34','brief56',
'refresher56', 'daily_uplift'];
var num_cols = ['hourly','dirsconfs', 'briefh','brief1','refresher1','brief34','refresher34','brief56',
'refresher56', 'daily_uplift'];
build_table(data, cols, num_cols, "#chamber_prices");
if (callback) {
callback();
}
})
.fail(function() {
alert("Unable to load chamber pricing. Please try again and if you get this message again, please report to support");
});
} else {
var jqxhr = $.get("/commissioning/get_prices/C/"+panel_id)
.done(function(json) {
// show table
var cols = ['level_of_court', 'seniority','organisation','city',
'hourly','dirsconfs', 'briefh','brief1','refresher1','brief34','refresher34','brief56',
'refresher56', 'daily_uplift'];
var num_cols = ['hourly','dirsconfs', 'briefh','brief1','refresher1','brief34','refresher34','brief56',
'refresher56', 'daily_uplift'];
build_table(json, cols, num_cols, "#chamber_prices");
if (callback) {
callback();
}
})
.fail(function() {
alert("Unable to load chamber pricing. Please try again and if you get this message again, please report to support");
});
}
}
function load_panels(org_type, callback) {
var jqxhr = $.get("/commissioning/panels?id="+org_type)
.done(function(json) {
$.each(json, function (i, d) {
$('#id_panel').append(
$('<option></option>').val(d.id).html(d.name)
);
});
if (callback) {
callback(parseInt(json[0].id));
}
})
.fail(function(jqXHR, textStatus ) {
alert("Unable to load framework panels. Please try again and if you get this message again, please report to support");
});
}
function show_lawfirms() {
// apply styles to reduce width of numeric cols as this version of tablesorter wants to make all cols equal size
$(".tablesorter .tablesorter-filter-row td:nth-child(4) .tablesorter-filter").css({
width : "50px"
});
$(".tablesorter .tablesorter-filter-row td:nth-child(5) .tablesorter-filter").css({
width : "50px"
});
$(".tablesorter .tablesorter-filter-row td:nth-child(6) .tablesorter-filter").css({
width : "50px"
});
$("#chamber_prices").hide();
$("#lawfirm_prices").show();
$("#id_org_type").val("L");
}
function show_chambers() {
$(".tablesorter .tablesorter-filter-row td:nth-child(2) .tablesorter-filter").css({
width : "40px"
});
$(".tablesorter .tablesorter-filter-row td:nth-child(1n+6) .tablesorter-filter").css({
width : "40px"
});
$("#chamber_prices").show();
$("#lawfirm_prices").hide();
$("#id_org_type").val("C");
}
// functions hacked from d3 as unable to use d3 due to high use of IE8
// I know it is theorectically possible to use d3 with IE8 but I haven't succeeded
// PHB hack to ignore 0 values
function extent(array, f) {
var i = -1,
n = array.length,
a,
b,
c;
if (arguments.length === 1) {
while (++i < n && !((a = c = array[i]) != null && a <= a)) a = c = undefined;
while (++i < n) if ((b = array[i]) != null) {
if (a > b) a = b;
if (c < b) c = b;
}
} else {
while (++i < n && !((a = c = f.call(array, array[i], i)) != null && a <= a)) a = undefined;
while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
// PHB ignore 0 values
if (b>0) {
if (a > b) a = b;
if (c < b) c = b;
}
}
}
return [a, c];
};
</script>
</body>
</html>
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 8 columns, instead of 9. in line 1.
plant type, Cl, S, P, Ca, MgK,Na,B
Dandelion,1340,33.5,128,143,53.4,485,3.25,0.44
Nettle,1050,70.17,35.34,86.1,141,376,0.55,1.37
Quack Grass Root,932,6.32,38.04,98,22,97,1.46,0.12
Sassafras,250,38.7,61.2,245,56,710,2,0.67
Peach Fruit,35,5.19,77.06,64.1,21.9,329.7,2.03,0.21
Blueberry Fruit,14,6.91,36.4,73.83,21.85,183.3,1.87,0.08
Apple Fruit,90,13.01,36.7,95.72,37.02,497.5,6.98,0.32
Horsetail,300,56.79,42.1,358.1,90.92,876.5,1.11,0.22
Lamb’s quarter,65,11.35,153.8,4.5,19.55,903.4,1.29,0.21
Chickweed,250,18.88,205.2,6.84,13.52,1277,54.47,1.62
Dill,275,46.24,155.2,167.5,55.35,1157,4.89,0.21
Comfrey,80,8.32,270.8,31.52,34.15,1025,0.58,0.4
Catnip,125,13.38,43.49,126.6,40.56,331.8,0.78,0.09
Mugwort,200,13.97,62.77,113.4,35.3,16.54,0.88,0.05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment