gchart.lua

Wed, 24 Jun 2009 05:22:24 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Jun 2009 05:22:24 +0100
changeset 0
757c17d808a8
child 1
f930ba6a8923
permissions
-rw-r--r--

Initial commit

0
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 module(..., package.seeall);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local chart = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 chart.__index = chart;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local writers = {}; -- Table of functions which build the URL
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- Defaults
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 chart.base_url = "http://chart.apis.google.com/chart";
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 chart.width, chart.height = 320, 200;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 chart.marker_color = "4D89F9";
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 -- Helpers
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function urlencode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local typemap = { line = "lc", sparkline = "ls", plot = "lxy", bar = "bhs" };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function new_chart(type)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local chart_obj = {
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 type = typemap[type] or type or "lc";
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 series = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 axes = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 markers = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return setmetatable(chart_obj, chart);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Library methods --
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function set_base_url(url)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 chart.base_url = url;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 function set_default_size(width, height)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 chart.width, chart.height = width or 320, height or 200;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 ----- Chart methods -----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 ---- Base URL ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 function chart:set_base_url(url)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 self.base_url = url;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- No writer for base URL
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 ---- Chart type ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function chart:set_type(type)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 self.type = typemap[type] or type;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 -- No writer for type
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 ---- Data series ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 function chart:add_series(data)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 table.insert(self.series, data);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local ee_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.";
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local ee_len = #ee_string;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local function to_extended_encoding(value)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 value = tonumber(value);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if not value or value < 0 then return "__"; end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local div, rem = math.floor(value/ee_len)+1, math.floor(value % ee_len)+1;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return ee_string:sub(div, div)..ee_string:sub(rem, rem);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 function writers:data()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local data = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 for n, series in ipairs(self.series) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local encoded = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 for _, value in ipairs(series) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if self.scale and value > 0 then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 --value = value - (self.scale.min or 0);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 --print(string.format("4096/(%d-%d)/(%d-%d) = %f", self.scale.max, self.scale.min, value, self.scale.min), value);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 value = 4096/((self.scale.max-self.scale.min)/(value-self.scale.min));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 table.insert(encoded, to_extended_encoding(value));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 table.insert(data, table.concat(encoded));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return "chd=e:"..table.concat(data, ",");
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 ---- Scale ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 function chart:set_scale(min, max)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 self.scale = { min = min, max = max };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 ---- Size ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 function chart:set_size(width, height)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 self.width, self.height = width, height;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 function writers:size()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return "chs="..tostring(self.width).."x"..tostring(self.height);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 ---- Title ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 function chart:set_title(title)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 self.title = title;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 function writers:title()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 if self.title then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 return "chtt="..urlencode(tostring(self.title):gsub("\n", "|"));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 ---- Axes display ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local axismap = { bottom = "x", left = "y", top = "t", right = "r" };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 function chart:add_axis(which, options)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 table.insert(self.axes, { type = axismap[which], options = options });
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 function writers:axes()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local axes, ranges = {}, {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local labels, positions = {}, {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local styles = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 for index, axis in ipairs(self.axes) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 index = index - 1;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 table.insert(axes, axis.type);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if axis.options.range then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local range = axis.options.range;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 table.insert(ranges, index..","..(range.min or 0)..","..(range.max or 100)..(range.interval and (","..range.interval) or ""));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 if axis.options.labels then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if axis.options.labels[1] then -- A list of strings
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 table.insert(labels, index..":|"..table.concat(axis.options.labels, "|"));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 else -- Specifying positions too
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local label_list, position_list = {}, {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 for label, position in pairs(axis.options.labels) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 table.insert(label_list, label);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 table.insert(position_list, position);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 table.insert(labels, index..":|"..table.concat(label_list, "|"));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 table.insert(positions, index..","..table.concat(positions, ","));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 if axis.options.style then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 table.insert(styles, index..","..axis.options.style);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if axis.options.ticklength then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 table.insert(ticklengths, index..","..axis.options.ticklength);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local result = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 if next(axes) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 table.insert(result, "chxt="..urlencode(table.concat(axes, ",")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 if next(ranges) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 table.insert(result, "chxr="..urlencode(table.concat(ranges, ",")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if next(labels) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 table.insert(result, "chxl="..urlencode(table.concat(labels, "|")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 if next(positions) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 table.insert(result, "chxp="..urlencode(table.concat(positions, ",")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if next(styles) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 table.insert(result, "chxs="..urlencode(table.concat(styles, "|")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 if next(ticklengths)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 table.insert(result, "chxtc="..urlencode(table.concat(ticklengths, "|")));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 return table.concat(result, "&");
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 ---- Data points ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 function chart:add_marker(marker)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 table.insert(self.markers, marker);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 local marker_type_map = { flag = "f", text = "t", number = "N" };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 function writers:markers()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local result = { };
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 for _, marker in ipairs(self.markers) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 table.insert(result, urlencode(
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 (marker_type_map[marker.type] or "f")
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 ..(marker.label or "Label")..","
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 ..(marker.color or self.marker_color)..","
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 ..(marker.series or 0)..","
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 ..(marker.index or 0)..","
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 ..(marker.size or 11)..","
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 ..(marker.priority or 0)));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 if next(result) then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 return "chm="..table.concat(result, "%7c");
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 ---- Colours and fill ----
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 function chart:set_color(color)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 self.color = color;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 function chart:set_fill(fill_color)
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 self.fill = fill_color;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 function writers:color()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 if self.color then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 return "chco="..self.color;
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 function chart:url()
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 local url = self.base_url.."?cht="..self.type.."&";
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 local params = {};
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 for name, writer in pairs(writers) do
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 local ret = writer(self);
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if ret then
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 table.insert(params, tostring(ret));
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 return url..table.concat(params, "&");
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 end
757c17d808a8 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225

mercurial