Support for auto-scaling of data

Wed, 24 Jun 2009 19:37:41 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Jun 2009 19:37:41 +0100
changeset 5
b4983e638117
parent 4
e17867506327
child 6
8d4be5429414

Support for auto-scaling of data

gchart.lua file | annotate | diff | comparison | revisions
--- a/gchart.lua	Wed Jun 24 19:36:34 2009 +0100
+++ b/gchart.lua	Wed Jun 24 19:37:41 2009 +0100
@@ -10,6 +10,7 @@
 chart.base_url = "http://chart.apis.google.com/chart";
 chart.width, chart.height = 320, 200;
 chart.marker_color = "4D89F9";
+chart.auto_scale_factor = 0.25;
 
 -- Helpers
 local function urlencode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
@@ -21,6 +22,7 @@
 			series = {};
 			axes = {};
 			markers = {};
+			scale = { min = true, max = true };
 		};
 	
 	return setmetatable(chart_obj, chart);
@@ -67,6 +69,27 @@
 end
 
 function writers:data()
+
+	if self.scale then
+		local autoscale_min, autoscale_max = (self.scale.min == true), (self.scale.max == true);
+		if autoscale_min or autoscale_max then
+			local min, max;
+			for n, series in ipairs(self.series) do
+				min, max = min or series[1], max or series[1];
+				for _, value in ipairs(series) do
+					if autoscale_min and value < min then min = value; end
+					if autoscale_max and value > max then max = value; end
+				end
+			end
+			if autoscale_min then
+				self.scale.min = min*(1-self.auto_scale_factor);
+			end
+			if autoscale_max then
+				self.scale.max = max*(1+self.auto_scale_factor);
+			end
+		end
+	end
+
 	local data = {};
 	for n, series in ipairs(self.series) do
 		local encoded = {};

mercurial