Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Options for sort & logarithmic scale

  • Loading branch information...
commit fde2e645fbbcfc7724260bae65aaf9ed9de47efb 1 parent dd4ebd2
@jaspervdj authored
Showing with 82 additions and 9 deletions.
  1. +66 −9 criterion-to-html.js
  2. +16 −0 src/Criterion/ToHtml/Html.hs
View
75 criterion-to-html.js
@@ -1,18 +1,67 @@
/* Create an element showing a group of results */
-function createResults(name, results) {
+function createResults(group) {
+ /* If attributes are not set, we should set them */
+ group.sort = group.sort ? true : false;
+ group.log = group.log ? true : false;
+
var div = $(document.createElement('div'));
- div.append($(document.createElement('h2')).html(name));
+ div.append($(document.createElement('h2')).html(group.name));
+
+ var controls = $(document.createElement('div')).addClass('controls');
+ div.append(controls);
+
+ /* Create a box and label to sort the results */
+ var sortBox = $(document.createElement('input'))
+ .attr('type', 'checkbox')
+ .attr('id', 'sort-' + sanitizeName(group.name))
+ .attr('checked', group.sort);
+ var sortLabel = $(document.createElement('label'))
+ .attr('for', 'sort-' + sanitizeName(group.name))
+ .html('Sort results');
+ controls.append(sortLabel);
+ controls.append(sortBox);
+
+ /* When the sort box is changed, we need to reset this div */
+ sortBox.click(function() {
+ group.sort = sortBox.attr('checked');
+ div.replaceWith(createResults(group));
+ });
+
+ /* Create a box and label to log the results */
+ var logBox = $(document.createElement('input'))
+ .attr('type', 'checkbox')
+ .attr('id', 'log-' + sanitizeName(group.name))
+ .attr('checked', group.log);
+ var logLabel = $(document.createElement('label'))
+ .attr('for', 'log-' + sanitizeName(group.name))
+ .html('Logarithmic scale');
+ controls.append(logLabel);
+ controls.append(logBox);
+
+ /* When the log box is changed, we need to reset this div */
+ logBox.click(function() {
+ group.log = logBox.attr('checked');
+ div.replaceWith(createResults(group));
+ });
+ /* Create an array copy in order to sort them */
+ var results = group.results.slice(0);
+ prepareResults(results, group.sort, group.log);
for(i in results) div.append(createResult(results[i]));
return div;
}
+/* Sanitize a name to something we can use as ID */
+function sanitizeName(name) {
+ return name.toLowerCase().replace(/[^a-z0-9]/, '-');
+}
+
/* Create an element showing a given result */
function createResult(result) {
var div = $(document.createElement('div'));
var canvas = document.createElement('canvas');
- $(canvas).attr('width', '500px').attr('height', '30px');
+ $(canvas).attr('width', '600px').attr('height', '30px');
div.append(canvas);
var ctx = canvas.getContext('2d');
@@ -45,18 +94,26 @@ function barColor(normalizedMean) {
return 'rgb(' + round(r) + ', ' + round(g) + ', ' + round(b) + ')';
}
-/* Calculate normalized means */
-function normalizeMeans(results) {
+/* Prepare results */
+function prepareResults(results, sort, log) {
+ /* Sort if necessary */
+ if(sort) {
+ results.sort(function(r1, r2) { return r1.mean - r2.mean; });
+ }
+
+ /* Modifier function */
+ var f = function(x) { return x; };
+ if(log) f = function (x) { return Math.log(1 + x); };
+
var max = 0;
- for(i in results) if(results[i].mean > max) max = results[i].mean;
- for(i in results) results[i].normalizedMean = results[i].mean / max;
+ for(i in results) if(results[i].mean > max) max = f(results[i].mean);
+ for(i in results) results[i].normalizedMean = f(results[i].mean) / max;
}
/* Main handler, create the page */
$(function() {
for(i in criterionResults) {
var group = criterionResults[i];
- normalizeMeans(group.results);
- $('body').append(createResults(group.name, group.results));
+ $('body').append(createResults(group));
}
});
View
16 src/Criterion/ToHtml/Html.hs
@@ -30,4 +30,20 @@ report js results = H.docTypeHtml $ do
unsafeLazyByteString $ encode results
";"
H.script ! A.type_ "text/javascript" $ unsafeByteString js
+ H.style ! A.type_ "text/css" $ do
+ "html {"
+ " font-size: 16px;"
+ " font-family: sans-serif;"
+ "}"
+ "body {"
+ " width: 600px;"
+ " margin: 0px auto 0px auto;"
+ "}"
+ "div.controls {"
+ " text-align: center;"
+ " margin-bottom: 10px;"
+ "}"
+ "div.controls input {"
+ " margin-right: 20px;"
+ "}"
H.body mempty
Please sign in to comment.
Something went wrong with that request. Please try again.