-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhistogram.lua
262 lines (212 loc) · 6.4 KB
/
histogram.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env lua
local ui = require "lui"
local bit = require'bit'
local mainwin, histogram, handler, colorButton
local datapoints = {}
local currentPoint = nil
local xoffLeft = 20 --/* histogram margins */
local yoffTop = 20
local xoffRight = 20
local yoffBottom = 20
local pointRadius = 5
local function setSolidBrush(brush, color, alpha)
local component, r,g,b
r = bit.band(bit.rshift(color,16),0xFF)/255
g = bit.band(bit.rshift(color,8),0xFF)/255
b = bit.band(color, 0xFF)/255
brush:Type(ui.DrawBrushTypeSolid)
:Solid(r,g,b,alpha)
end
-- and some colors
-- names and values from https:--msdn.microsoft.com/en-us/library/windows/desktop/dd370907%28v=vs.85%29.aspx
local colorWhite = 0xFFFFFF
local colorBlack = 0x000000
local colorDodgerBlue = 0x1E90FF
local function pointLocations(width, height)
local xincr, yincr;
local i, n;
local xs,ys = {},{}
xincr = width / 9; -- 10 - 1 to make the last point be at the end
yincr = height / 100;
for i = 1,10 do
---- get the value of the point
n = datapoints[i]:Value()
---- because y=0 is the top but n=0 is the bottom, we need to flip
n = 100 - n
xs[i] = xincr * (i-1);
ys[i] = yincr * n;
end
return xs,ys
end
local function constructGraph(width, height, extend)
local xs,ys = pointLocations(width, height);
local path = ui.DrawNewPath(ui.DrawFillModeWinding);
path:NewFigure(xs[1], ys[1]);
for i=1,10 do
path:LineTo(xs[i], ys[i]);
end
path:LineTo(xs[10], ys[10]);
if extend then
path:LineTo(width, height);
path:LineTo(0, height);
path:CloseFigure();
end
path:End();
return path;
end
local function graphSize(clientWidth, clientHeight)
return clientWidth - xoffLeft - xoffRight,
clientHeight - yoffTop - yoffBottom
end
local function handlerDraw(a, area, p)
local path,brush,sp,m,graphWidth, graphHeight,graphR, graphG, graphB, graphA
-- fill the area with white
brush = ui.DrawNewBrush()
setSolidBrush(brush, colorWhite, 1.0)
path = ui.DrawNewPath(ui.DrawFillModeWinding);
path:AddRectangle(0, 0, p.AreaWidth, p.AreaHeight):End(path);
p.Context:Fill(path, brush);
path:Free()
-- figure out dimensions
graphWidth, graphHeight = graphSize(p.AreaWidth, p.AreaHeight);
-- clear sp to avoid passing garbage to uiDrawStroke()
-- for example, we don't use dashing
sp = ui.DrawNewStrokeParams();
-- make a stroke for both the axes and the histogram line
sp:Cap(ui.DrawLineCapFlat)
sp:Join(ui.DrawLineJoinMiter)
sp:Thickness(2)
sp:MiterLimit(ui.DrawDefaultMiterLimit)
-- draw the axes
brush = ui.DrawNewBrush()
setSolidBrush(brush, colorBlack, 1.0)
path = ui.DrawNewPath(ui.DrawFillModeWinding);
path:NewFigure(xoffLeft, yoffTop)
:LineTo(xoffLeft, yoffTop + graphHeight)
:LineTo(xoffLeft + graphWidth, yoffTop + graphHeight)
:End()
p.Context:Stroke(path, brush, sp);
path:Free()
-- now transform the coordinate space so (0, 0) is the top-left corner of the graph
m = ui.DrawNewMatrix()
m:SetIdentity():Translate(xoffLeft, yoffTop);
p.Context:Transform(m);
-- now get the color for the graph itself and set up the brush
graphR, graphG, graphB, graphA = colorButton:Color();
brush:Type(ui.DrawBrushTypeSolid);
-- we set brush->A below to different values for the fill and stroke
-- now create the fill for the graph below the graph line
path = constructGraph(graphWidth, graphHeight, true);
brush:Solid(graphR,graphG,graphB,graphA / 2)
p.Context:Fill(path, brush);
path:Free()
-- now draw the histogram line
path = constructGraph(graphWidth, graphHeight, false);
brush:Solid(graphR,graphG,graphB,graphA)
p.Context:Stroke(path, brush, sp);
path:Free()
-- now draw the point being hovered over
if (currentPoint) then
local xs, ys = pointLocations(graphWidth, graphHeight);
path = ui.DrawNewPath(ui.DrawFillModeWinding);
path:NewFigureWithArc(
xs[currentPoint], ys[currentPoint],
pointRadius,
0, 6.23, -- TODO pi
false);
path:End()
-- use the same brush as for the histogram lines
p.Context:Fill(path, brush);
path:Free()
currentPoint = nil
end
end
local function inPoint(x, y, xtest, ytest)
-- TODO switch to using a matrix
x = x - xoffLeft;
y = y - yoffTop;
return (x >= xtest - pointRadius) and
(x <= xtest + pointRadius) and
(y >= ytest - pointRadius) and
(y <= ytest + pointRadius);
end
local function handlerMouseEvent(a, area, e)
local graphWidth, graphHeight
local xs,ys = {}, {}
graphWidth, graphHeight = graphSize(e.AreaWidth, e.AreaHeight);
xs, ys = pointLocations(graphWidth, graphHeight);
for i=1,10 do
if (inPoint(e.X, e.Y, xs[i], ys[i])) then
currentPoint = i
break;
end
end
-- TODO only redraw the relevant area
histogram:QueueRedrawAll();
end
local function handlerMouseCrossed(ah, a, left)
-- do nothing
end
local function handlerDragBroken(ah, a)
-- do nothing
end
local function handlerKeyEvent(ah, a, e)
-- reject all keys
return 0;
end
local function onDatapointChanged(s, data)
histogram:QueueRedrawAll();
end
local function onColorChanged(b)
histogram:QueueRedrawAll();
end
local function onClosing(w)
ui.Quit()
return 1
end
local function shouldQuit()
ui.ControlDestroy(mainwin)
return true
end
local function uiMain()
local ret,err = ui.Init();
if (not ret) then
io.stderr:write(string.format("error initializing libui: %s", err));
return 1;
end
local hbox,vbox,brush;
handler = ui.DrawNewAreaHandler(
handlerDraw,
handlerMouseEvent,
handlerMouseCrossed,
handlerDragBroken,
handlerKeyEvent)
ui.OnShouldQuit(shouldQuit);
mainwin = ui.NewWindow("libui Histogram Example", 640, 480, true)
:Margined(true)
:OnClosing(onClosing);
hbox = ui.NewHorizontalBox():Padded(true)
mainwin:SetChild(hbox)
vbox = ui.NewVerticalBox():Padded(true)
hbox:Append(vbox,false)
math.randomseed(os.time())
for i=1,10 do
datapoints[i] = ui.NewSpinbox(0, 100)
:Value(math.random(0,100))
:OnChanged(onDatapointChanged)
vbox:Append(datapoints[i])
end
colorButton = ui.NewColorButton()
---- TODO inline these
brush = ui.DrawNewBrush()
setSolidBrush(brush, colorDodgerBlue, 1.0)
colorButton:OnChanged(onColorChanged):Color(brush:Solid())
vbox:Append(colorButton)
histogram = ui.NewArea(handler)
hbox:Append(histogram, true);
mainwin:Show()
ui.Main()
ui.Uninit()
return 0
end
uiMain()