author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年07月28日 22:04:58 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年07月28日 22:04:58 +0200 |
commit | 707e54e3350e458530270d9cbd0767771cf6386f (patch) | |
tree | eb3ac932c16c081dbb76d65662b2b5395276e07d /graph-init.lua | |
parent | cf077cb0817f498b8f1bb3a52baf8f89dc919a64 (diff) | |
parent | 78702ab307e9d05493cb980a25f19d340d5644a0 (diff) | |
download | gsl-shell-707e54e3350e458530270d9cbd0767771cf6386f.tar.gz |
-rw-r--r-- | graph-init.lua | 70 |
diff --git a/graph-init.lua b/graph-init.lua index fb7b0df2..fe7d1558 100644 --- a/graph-init.lua +++ b/graph-init.lua @@ -118,6 +118,7 @@ end function graph.barplot(t) local nr, nc = #t, #t[1] - 1 + local legend_text = t.legend local pad = 0.1 local dx = (1-2*pad)/nc local cat = {} @@ -134,6 +135,13 @@ function graph.barplot(t) local rect = graph.rect(x, 0, x+dx, y) p:add(rect, graph.webcolor(j)) end + + end + + if legend_text then + for j = 1, nc do + p:legend(legend_text[j], graph.webcolor(j), 'square') + end end p:set_categories('x', cat) @@ -282,34 +290,48 @@ end local function legend_symbol(sym, dx, dy) if sym == 'square' then return graph.rect(5+dx, 5+dy, 15+dx, 15+dy) - elseif sym == 'circle' then - return graph.ellipse(10+dx, 10+dy, 5, 5) elseif sym == 'line' then - return graph.segment(2+dx, 10+dy, 18+dx, 10+dy), {'stroke'} + return graph.segment(2+dx, 10+dy, 18+dx, 10+dy), {{'stroke'}} else - error('invalid legend symbol: ' .. sym) + return graph.marker(10+dx, 10+dy, sym, 8) end end -function graph.legend(entries) - local n = #entries - local p = graph.plot() - p.units, p.clip = false, false - for k= 1, n do - local text, color, symspec, trans = unpack(entries[k]) - local y = (k-1) * 20 - local sym, symtr = legend_symbol(symspec, 0, y) - local tr - if symtr then - tr = { symtr } - if trans then - for j, xtr in ipairs(trans) do tr[#tr+1] = xtr end - end - else - tr = trans - end - p:add(sym, color, tr) - p:add(graph.textshape(25, y + 6, text, 10), 'black') +local function plot_legend(self, text, color, symspec, trans) + local lg = self:get_legend() + local env = debug.getfenv(self) + + if not lg then + lg = graph.plot() + lg.units = false + self:set_legend(lg) end - return p + + local k = env.__lg_count or 0 + local y = -k * 20 + + local sym, symtr = legend_symbol(symspec, 0, y) + + local tr = (trans and trans or symtr) + + lg:add(sym, color, tr) + lg:add(graph.textshape(25, y + 6, text, 14), 'black') + + env.__lg_count = k+1 + self:update() +end + +local function redirect_plot() + local reg = debug.getregistry() + local mt = reg['GSL.plot'] + local plot_index = mt.__index + + local function index_redirect(t, k) + if k == 'legend' then return plot_legend end + return plot_index(t, k) + end + + mt.__index = index_redirect end + +redirect_plot() |