/
ui_comp_chart.go
executable file
·226 lines (179 loc) · 6.94 KB
/
ui_comp_chart.go
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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"strconv"
)
type UiLayoutChartItem struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Label string `json:"label"`
}
func UiLayoutChart_getBound(items []UiLayoutChartItem, bound_x0, bound_y0 bool) (UiLayoutChartItem, UiLayoutChartItem) {
var min, max UiLayoutChartItem
if len(items) > 0 {
//bound==false => can be in range 5-10(no 0)
min.X = OsTrnFloat(bound_x0, 0, items[0].X)
max.X = OsTrnFloat(bound_x0, 0, items[0].X)
min.Y = OsTrnFloat(bound_y0, 0, items[0].Y)
max.Y = OsTrnFloat(bound_y0, 0, items[0].Y)
}
for _, it := range items {
min.X = OsMinFloat(min.X, it.X)
min.Y = OsMinFloat(min.Y, it.Y)
max.X = OsMaxFloat(max.X, it.X)
max.Y = OsMaxFloat(max.Y, it.Y)
}
return min, max
}
func cellMargin(cell_left, cell_right, cell_top, cell_bottom float64, x, y, w, h float64, ui *Ui) (float64, float64, float64, float64) {
canvas := ui.GetCall().call.canvas
l := float64(ui.CellWidth(cell_left)) / float64(canvas.Size.X)
r := float64(ui.CellWidth(cell_right)) / float64(canvas.Size.X)
t := float64(ui.CellWidth(cell_top)) / float64(canvas.Size.Y)
b := float64(ui.CellWidth(cell_bottom)) / float64(canvas.Size.Y)
x = l + ((1 - l - r) * x)
y = t + ((1 - t - b) * y)
w = ((1 - l - r) * w)
h = ((1 - t - b) * h)
return x, y, w, h
}
func _UiLayoutChart_drawAxisX(min, max UiLayoutChartItem, left_margin, right_margin, top_margin, bottom_margin float64, cdAxis, cdAxisGrey OsCd, ui *Ui, drawAxis, drawValues bool, unit string) {
vx := max.X - min.X
vy := max.Y - min.Y
if drawAxis {
//0
y := (0 - min.Y) / vy
x, y, w, h := cellMargin(left_margin, right_margin, 0, bottom_margin, 0, 1-y, 1, 0, ui)
ui.Paint_line(0, 0, 1, 1, 0, x, y, x+w, y+h, cdAxis, 0.03)
}
if drawValues {
top_margin2 := float64(ui.GetCall().call.canvas.Size.Y)/float64(ui.win.Cell()) - bottom_margin
cx := float64(ui.CellWidth(2)) / float64(ui.GetCall().call.canvas.Size.X)
x, y, w, h := cellMargin(left_margin, right_margin, top_margin, bottom_margin, 0, 0, 0, 1, ui)
ui.Paint_line(0, 0, 1, 1, 0, x, y, x+w, y+h, cdAxisGrey, 0.03)
p := 0.0
for {
x, y, w, h := cellMargin(left_margin, right_margin, top_margin, bottom_margin, p, 0, 0, 1, ui)
ui.Paint_line(0, 0, 1, 1, 0, x, y, x+w, y+h, cdAxisGrey, 0.03)
if p == 0 || p == 1 || p+(cx*0.75) < 1 { //skip pre-last
x, y, w, h = cellMargin(left_margin, right_margin, top_margin2, 0, p-cx/2, 0, cx, 1, ui)
ui.Div_startEx(0, 0, 1, 1, x, y, w, h, fmt.Sprintf("axis_x%f", p))
ui.Div_colMax(0, 100)
ui.Div_row(0, bottom_margin)
ui.GetCall().call.data.scrollV.show = false
ui.GetCall().call.data.scrollH.show = false
ui.Comp_text(0, 0, 1, 1, strconv.FormatFloat(min.X+(vx*p), 'f', 2, 64)+OsTrnString(p == 1, unit, ""), 1)
//ui.Paint_rect(0, 0, 1, 1, 0, InitOsCdBlack(), 0.03)
ui.Div_end()
}
if p == 1 {
break
}
p += cx
if p > 1 {
p = 1
}
}
}
}
func _UiLayoutChart_drawAxisY(min, max UiLayoutChartItem, left_margin, right_margin, top_margin, bottom_margin float64, cdAxis, cdAxisGrey OsCd, ui *Ui, drawAxis, drawValues bool, unit string) {
vx := max.X - min.X
vy := max.Y - min.Y
if drawAxis {
//0
x := (0 - min.X) / vx
x, y, w, h := cellMargin(left_margin, 0, top_margin, bottom_margin, x, 0, 0, 1, ui)
ui.Paint_line(0, 0, 1, 1, 0, x, y, x+w, y+h, cdAxis, 0.03)
}
if drawValues {
right_margin2 := float64(ui.GetCall().call.canvas.Size.X)/float64(ui.win.Cell()) - left_margin
cy := float64(ui.CellWidth(2)) / float64(ui.GetCall().call.canvas.Size.Y)
p := 0.0
for {
x, y, w, h := cellMargin(left_margin, right_margin, top_margin, bottom_margin, 0, p, 1, 0, ui)
ui.Paint_line(0, 0, 1, 1, 0, x, y, x+w, y+h, cdAxisGrey, 0.03)
x, y, w, h = cellMargin(0, right_margin2, top_margin, bottom_margin, 0, p-cy/2, 1, cy, ui)
ui.Div_startEx(0, 0, 1, 1, x, y, w, h, fmt.Sprintf("axis_y%f", p))
ui.Div_colMax(0, 100)
ui.Div_rowMax(0, 100)
ui.GetCall().call.data.scrollV.show = false
ui.Comp_text(0, 0, 1, 1, strconv.FormatFloat(max.Y-(vy*p), 'f', 2, 64)+OsTrnString(p == 0, unit, ""), 1)
//ui.Paint_rect(0, 0, 1, 1, 0, InitOsCdBlack(), 0.03)
ui.Div_end()
if p == 1 {
break
}
p += cy
if p > 1 {
p = 1
}
}
}
}
func _UiLayoutChart_drawLines(items []UiLayoutChartItem, min, max UiLayoutChartItem, left_margin, right_margin, top_margin, bottom_margin float64, cd OsCd, rad float64, lineThick float64, ui *Ui) {
vx := max.X - min.X
vy := max.Y - min.Y
//lines
last_x := float64(0)
last_y := float64(0)
for i, it := range items {
x := (it.X - min.X) / vx
y := (it.Y - min.Y) / vy
x, y, _, _ = cellMargin(left_margin, right_margin, top_margin, bottom_margin, x, 1-y, 0, 0, ui)
if rad > 0 {
ui.Paint_circle(0, 0, 1, 1, 0, x, y, rad, cd, 0)
}
//ui.Paint_tooltip() //...
if i > 0 {
ui.Paint_line(0, 0, 1, 1, 0, last_x, last_y, x, y, cd, lineThick)
}
last_x = x
last_y = y
}
}
func _UiLayoutChart_drawColumns(items []UiLayoutChartItem, min, max UiLayoutChartItem, left_margin, right_margin, top_margin, bottom_margin float64, column_margin float64, cd OsCd, ui *Ui) {
//vx := max.X - min.X
vy := max.Y - min.Y
cmarx := float64(ui.CellWidth(column_margin)) / float64(ui.GetCall().call.canvas.Size.X)
_, _, cmar, _ := cellMargin(left_margin, right_margin, top_margin, bottom_margin, 0, 0, cmarx, 0, ui)
//values
jump_x := 1 / float64(len(items))
for i, it := range items {
x := float64(i) * jump_x
y := (it.Y - min.Y) / vy
x, y, w, h := cellMargin(left_margin, right_margin, top_margin, bottom_margin, x, 1, jump_x, -y, ui) //y is reverse!
ui.Paint_rect(x+cmar, y, w-2*cmar, h, 0, cd, 0)
}
}
func _UiLayoutChart_drawAxisXlabels(items []UiLayoutChartItem, left_margin, right_margin, top_margin, bottom_margin float64, ui *Ui) {
top_margin2 := float64(ui.GetCall().call.canvas.Size.Y)/float64(ui.win.Cell()) - bottom_margin
jump_x := 1 / float64(len(items))
next_x := 0.0
cell_x := float64(ui.CellWidth(2)) / float64(ui.GetCall().call.canvas.Size.X)
for i, it := range items {
x := float64(i) * jump_x
x, y, w, h := cellMargin(left_margin, right_margin, top_margin2, 0, x, 0, cell_x, 1, ui)
if x >= next_x {
ui.Div_startEx(0, 0, 1, 1, x, y, w, h, fmt.Sprintf("label%d", i))
ui.Div_colMax(0, 100)
ui.Div_row(0, bottom_margin)
ui.GetCall().call.data.scrollV.show = false
ui.Comp_text(0, 0, 1, 1, it.Label, 1)
//ui.Paint_rect(0, 0, 1, 1, 0, InitOsCdBlack(), 0.03)
ui.Div_end()
next_x = x + cell_x
}
}
}