gchart.lua

changeset 5
b4983e638117
parent 4
e17867506327
child 6
8d4be5429414
equal deleted inserted replaced
4:e17867506327 5:b4983e638117
8 8
9 -- Defaults 9 -- Defaults
10 chart.base_url = "http://chart.apis.google.com/chart"; 10 chart.base_url = "http://chart.apis.google.com/chart";
11 chart.width, chart.height = 320, 200; 11 chart.width, chart.height = 320, 200;
12 chart.marker_color = "4D89F9"; 12 chart.marker_color = "4D89F9";
13 chart.auto_scale_factor = 0.25;
13 14
14 -- Helpers 15 -- Helpers
15 local function urlencode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end 16 local function urlencode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
16 local typemap = { line = "lc", sparkline = "ls", plot = "lxy", bar = "bvs" }; 17 local typemap = { line = "lc", sparkline = "ls", plot = "lxy", bar = "bvs" };
17 18
19 local chart_obj = { 20 local chart_obj = {
20 type = typemap[type] or type or "lc"; 21 type = typemap[type] or type or "lc";
21 series = {}; 22 series = {};
22 axes = {}; 23 axes = {};
23 markers = {}; 24 markers = {};
25 scale = { min = true, max = true };
24 }; 26 };
25 27
26 return setmetatable(chart_obj, chart); 28 return setmetatable(chart_obj, chart);
27 end 29 end
28 30
65 local div, rem = math.floor(value/ee_len)+1, math.floor(value % ee_len)+1; 67 local div, rem = math.floor(value/ee_len)+1, math.floor(value % ee_len)+1;
66 return ee_string:sub(div, div)..ee_string:sub(rem, rem); 68 return ee_string:sub(div, div)..ee_string:sub(rem, rem);
67 end 69 end
68 70
69 function writers:data() 71 function writers:data()
72
73 if self.scale then
74 local autoscale_min, autoscale_max = (self.scale.min == true), (self.scale.max == true);
75 if autoscale_min or autoscale_max then
76 local min, max;
77 for n, series in ipairs(self.series) do
78 min, max = min or series[1], max or series[1];
79 for _, value in ipairs(series) do
80 if autoscale_min and value < min then min = value; end
81 if autoscale_max and value > max then max = value; end
82 end
83 end
84 if autoscale_min then
85 self.scale.min = min*(1-self.auto_scale_factor);
86 end
87 if autoscale_max then
88 self.scale.max = max*(1+self.auto_scale_factor);
89 end
90 end
91 end
92
70 local data = {}; 93 local data = {};
71 for n, series in ipairs(self.series) do 94 for n, series in ipairs(self.series) do
72 local encoded = {}; 95 local encoded = {};
73 for _, value in ipairs(series) do 96 for _, value in ipairs(series) do
74 if self.scale and value > 0 then 97 if self.scale and value > 0 then

mercurial