# HG changeset patch # User Matthew Wild # Date 1245868661 -3600 # Node ID b4983e63811771763b3c8afcd1f3eab96a8d1709 # Parent e17867506327baa29f6caff913b57bdba956f01d Support for auto-scaling of data diff -r e17867506327 -r b4983e638117 gchart.lua --- 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 = {};