-rw-r--r-- | demos/plot.lua | 14 | ||||
-rw-r--r-- | graph-init.lua | 38 |
diff --git a/demos/plot.lua b/demos/plot.lua index a2e7a0e3..b5756fee 100644 --- a/demos/plot.lua +++ b/demos/plot.lua @@ -112,10 +112,24 @@ local function demo_plot() demo4() end +local function barplot_demo() + local t = {{'ode', 14, 17, 8}, {'integ', 21, 19, 7}, {'nlfit', 8,12,6}} + local p = barplot(t) + p.xtitle = 'Test' + p.ytitle = 'Execution time, ms' + p.title = 'Benchmark results' + return p +end + return {'Plotting', { { name= 'plot', f = demo_plot, description = 'Various kind of plots' }, + { + name= 'barplot', + f = barplot_demo, + description = 'Bar Plot example' + }, }} diff --git a/graph-init.lua b/graph-init.lua index 5b875016..f2f837c2 100644 --- a/graph-init.lua +++ b/graph-init.lua @@ -1,7 +1,7 @@ local bit = require 'bit' -local floor = math.floor +local floor, pi = math.floor, math.pi local bor, band, lshift, rshift = bit.bor, bit.band, bit.lshift, bit.rshift @@ -118,6 +118,34 @@ function graph.ibars(f) return b end +function graph.barplot(t) + local nr, nc = #t, #t[1] - 1 + local pad = 0.1 + local dx = (1-2*pad)/nc + local cat = {} + local p = graph.plot() + p.pad = true + + for k = 1, nr do + local row = t[k] + local label = row[1] + cat[#cat+1] = k - 0.5 + cat[#cat+1] = label + for j = 1, nc do + local x, y = (k-1) + pad + (j-1)*dx, row[j+1] + local rect = graph.rect(x, 0, x+dx, y) + p:add(rect, graph.webcolor(j)) + p:addline(rect, 'black') + end + end + + p:set_categories('x', cat) + p.xlab_angle = pi/2 + + p:show() + return p +end + function graph.segment(x1, y1, x2, y2) local p = graph.path(x1, y1) p:line_to(x2, y2) @@ -175,12 +203,20 @@ graph.color = { } local bcolors = {'red', 'blue', 'green', 'magenta', 'cyan', 'yellow'} +local wcolors = {0x3366ff, 0xcc0000, 0xcc33ff, 0xff3366, 0xffcc33, 0x66ff33, 0x33ffcc} +-- local wcolors = {0x6699ff, 0xcc66ff, 0xff6699, 0xffcc66, 0x99ff66, 0x66ffcc} +-- local wcolors = {0x003399, 0x660099, 0x990033, 0x996600, 0x339900, 0x009966} function graph.rainbow(n) local p = #bcolors return graph.color[bcolors[(n-1) % p + 1]] end +function graph.webcolor(n) + local p = #wcolors + return lshift(wcolors[(n-1) % p + 1], 8) + 0xff +end + local color_schema = { bluish = {0.91, 0.898, 0.85, 0.345, 0.145, 0.6}, redyellow = {1, 1, 0, 1, 0, 0}, |