changeset 36:524184c2f524 draft

Uploaded
author guerler
date Fri, 09 May 2014 00:59:08 -0400
parents f294f5c9608c
children eafc7121f553
files histogram.r
diffstat 1 files changed, 27 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/histogram.r	Fri May 09 00:58:55 2014 -0400
+++ b/histogram.r	Fri May 09 00:59:08 2014 -0400
@@ -1,8 +1,17 @@
-# utilities
-boundary <- function(x, increment) {
+# binsize
+min_binsize = 10
+
+# lower boundary
+lowerboundary <- function(x, increment) {
     return (floor(x / increment) * increment)
 }
 
+# upper boundary
+upperboundary <- function(x, increment) {
+    return (ceiling(x / increment) * increment)
+}
+
+# round to decimals
 roundup <- function(x) {
     return (sign(x) * 10^ceiling(log10(abs(x))))
 }
@@ -10,6 +19,9 @@
 # wrapper
 wrapper <- function(table, columns, options) {
 
+    # get binsize
+    binsize = max(as.integer(options$binsize), min_binsize)
+    
     # initialize output list
     l <- list()
 
@@ -28,14 +40,24 @@
     min_value <- min(unlist(m))
     max_value <- max(unlist(m))
     
+    # identify range
+    diff <- max_value - min_value
+    
     # identify increment
-    increment <- roundup((max_value - min_value) / 10)
+    increment <- roundup(diff / binsize)
     
     # fix min value
-    min_value <- boundary(min_value, increment)
+    min_value <- lowerboundary(min_value, increment)
+    max_value <- upperboundary(max_value, increment)
+    
+    # update range
+    diff <- max_value - min_value
+    
+    # fix bin size
+    binsize = round(diff / increment)
     
     # fix max value
-    max_value <- min_value + increment * 10
+    max_value <- min_value + binsize * increment
     
     # check if single bin is enough
     if (min_value == max_value) {
@@ -70,7 +92,6 @@
         l <- append(l, list(hist_data$counts))
     }
     
-
     # return
     return (l)
 }