glimmer-libui-cc-graphs_and_charts 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +87 -0
- data/VERSION +1 -0
- data/lib/glimmer/view/line_graph.rb +410 -0
- data/lib/glimmer-libui-cc-graphs_and_charts.rb +4 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c79188349650bf2223b5885ceaff00675197d6f833af8bffbfc15f1dbaaa2a5
|
4
|
+
data.tar.gz: a9d9f4732bdf4b81e20721cdfda4ffab550e6bd82785884db0ccf8f2f43a6d99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b5db2acfba0997e784840523b3e09154daea02fc78f1eb3726a3fbb50b876fc86aa483eea4f4af44c4eda804087d9c651d9081b81faa362b3fca252c76a8cb97
|
7
|
+
data.tar.gz: f6eba9bdc99d3bd30a17e53cbc81345bddb67c2573a6e51f26af546d56eb5d740e066a582cbaacec31e9f1ef00432f620f50dde58b9c6b83e9994ecf41e9fdcb
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2023 Andy Maleh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# Graphs and Charts 0.1.0
|
2
|
+
## [Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui) Custom Controls
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/glimmer-libui.svg)](http://badge.fury.io/rb/glimmer-dsl-libui)
|
4
|
+
[![Join the chat at https://gitter.im/AndyObtiva/glimmer](https://badges.gitter.im/AndyObtiva/glimmer.svg)](https://gitter.im/AndyObtiva/glimmer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
5
|
+
|
6
|
+
Graphs and Charts (Custom Controls) for [Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui)
|
7
|
+
|
8
|
+
![line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-line-graph.png)
|
9
|
+
|
10
|
+
## Setup
|
11
|
+
|
12
|
+
Add this line to Bundler `Gemfile`:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'glimmer-libui-cc-graphs_and_charts', '~> 0.1.0'
|
16
|
+
```
|
17
|
+
|
18
|
+
Run:
|
19
|
+
|
20
|
+
```
|
21
|
+
bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Line Graph
|
27
|
+
|
28
|
+
Add this line to your Ruby file:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'glimmer/view/line_graph'
|
32
|
+
```
|
33
|
+
|
34
|
+
Example Glimmer GUI DSL code that can be nested under `window` or some container like `vertical_box`:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
line_graph(
|
38
|
+
width: 900,
|
39
|
+
height: 300,
|
40
|
+
lines: [
|
41
|
+
{
|
42
|
+
name: 'Failed',
|
43
|
+
stroke: [163, 40, 39, thickness: 2],
|
44
|
+
x_value_start: Time.now,
|
45
|
+
x_interval_in_seconds: 2,
|
46
|
+
x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
|
47
|
+
y_values: [36, 0, 60, 0, 0, 16, 0, 36, 0, 0]
|
48
|
+
},
|
49
|
+
{
|
50
|
+
name: 'Processed',
|
51
|
+
stroke: [47, 109, 104, thickness: 2],
|
52
|
+
x_value_start: Time.now,
|
53
|
+
x_interval_in_seconds: 2,
|
54
|
+
x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
|
55
|
+
y_values: [62, 0, 90, 0, 0, 27, 0, 56, 0, 0]
|
56
|
+
},
|
57
|
+
],
|
58
|
+
display_attributes_on_hover: true,
|
59
|
+
)
|
60
|
+
```
|
61
|
+
|
62
|
+
![line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-line-graph.png)
|
63
|
+
|
64
|
+
Contributing to glimmer-libui-cc-graphs_and_charts
|
65
|
+
------------------------------------------
|
66
|
+
|
67
|
+
- Check out the latest master to make sure the feature hasn't been
|
68
|
+
implemented or the bug hasn't been fixed yet.
|
69
|
+
- Check out the issue tracker to make sure someone already hasn't
|
70
|
+
requested it and/or contributed it.
|
71
|
+
- Fork the project.
|
72
|
+
- Start a feature/bugfix branch.
|
73
|
+
- Commit and push until you are happy with your contribution.
|
74
|
+
- Make sure to add tests for it. This is important so I don't break it
|
75
|
+
in a future version unintentionally.
|
76
|
+
- Please try not to mess with the Rakefile, version, or history. If
|
77
|
+
you want to have your own version, or is otherwise necessary, that
|
78
|
+
is fine, but please isolate to its own commit so I can cherry-pick
|
79
|
+
around it.
|
80
|
+
|
81
|
+
Copyright
|
82
|
+
---------
|
83
|
+
|
84
|
+
[MIT](LICENSE.txt)
|
85
|
+
|
86
|
+
Copyright (c) 2023 Andy Maleh. See
|
87
|
+
[LICENSE.txt](LICENSE.txt) for further details.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,410 @@
|
|
1
|
+
module Glimmer
|
2
|
+
module View
|
3
|
+
# General-Purpose Line Graph Custom Control
|
4
|
+
class LineGraph
|
5
|
+
include Glimmer::LibUI::CustomControl
|
6
|
+
|
7
|
+
DEFAULT_GRAPH_PADDING_WIDTH = 5.0
|
8
|
+
DEFAULT_GRAPH_PADDING_HEIGHT = 5.0
|
9
|
+
DEFAULT_GRAPH_GRID_MARKER_PADDING_WIDTH = 30.0
|
10
|
+
DEFAULT_GRAPH_POINT_DISTANCE = 15.0
|
11
|
+
|
12
|
+
DEFAULT_GRAPH_STROKE_GRID = [185, 184, 185]
|
13
|
+
DEFAULT_GRAPH_STROKE_MARKER = [185, 184, 185]
|
14
|
+
DEFAULT_GRAPH_STROKE_MARKER_LINE = [217, 217, 217, thickness: 1, dashes: [1, 1]]
|
15
|
+
DEFAULT_GRAPH_STROKE_PERIODIC_LINE = [121, 121, 121, thickness: 1, dashes: [1, 1]]
|
16
|
+
DEFAULT_GRAPH_STROKE_HOVER_LINE = [133, 133, 133]
|
17
|
+
|
18
|
+
DEFAULT_GRAPH_COLOR_MARKER_TEXT = [96, 96, 96]
|
19
|
+
DEFAULT_GRAPH_COLOR_PERIOD_TEXT = [163, 40, 39]
|
20
|
+
|
21
|
+
DEFAULT_GRAPH_FONT_MARKER_TEXT = {family: "Arial", size: 14}
|
22
|
+
|
23
|
+
DEFAULT_GRAPH_STATUS_HEIGHT = 30.0
|
24
|
+
|
25
|
+
DAY_IN_SECONDS = 60*60*24
|
26
|
+
|
27
|
+
option :width, default: 600
|
28
|
+
option :height, default: 200
|
29
|
+
|
30
|
+
# Hash or Array of Hash's like:
|
31
|
+
# {
|
32
|
+
# name: 'Attribute Name',
|
33
|
+
# stroke: [28, 34, 89, thickness: 3],
|
34
|
+
# x_value_start: Time.now,
|
35
|
+
# x_interval_in_seconds: 2,
|
36
|
+
# x_value_format: ->(time) {time.strftime('%s')},
|
37
|
+
# y_values: [...]
|
38
|
+
# }
|
39
|
+
option :lines, default: []
|
40
|
+
|
41
|
+
option :graph_padding_width, default: DEFAULT_GRAPH_PADDING_WIDTH
|
42
|
+
option :graph_padding_height, default: DEFAULT_GRAPH_PADDING_HEIGHT
|
43
|
+
option :graph_grid_marker_padding_width, default: DEFAULT_GRAPH_GRID_MARKER_PADDING_WIDTH
|
44
|
+
option :graph_point_distance, default: DEFAULT_GRAPH_POINT_DISTANCE
|
45
|
+
|
46
|
+
option :graph_stroke_grid, default: DEFAULT_GRAPH_STROKE_GRID
|
47
|
+
option :graph_stroke_marker, default: DEFAULT_GRAPH_STROKE_MARKER
|
48
|
+
option :graph_stroke_marker_line, default: DEFAULT_GRAPH_STROKE_MARKER_LINE
|
49
|
+
option :graph_stroke_periodic_line, default: DEFAULT_GRAPH_STROKE_PERIODIC_LINE
|
50
|
+
option :graph_stroke_hover_line, default: DEFAULT_GRAPH_STROKE_HOVER_LINE
|
51
|
+
|
52
|
+
option :graph_color_marker_text, default: DEFAULT_GRAPH_COLOR_MARKER_TEXT
|
53
|
+
option :graph_color_period_text, default: DEFAULT_GRAPH_COLOR_PERIOD_TEXT
|
54
|
+
|
55
|
+
option :graph_font_marker_text, default: DEFAULT_GRAPH_FONT_MARKER_TEXT
|
56
|
+
|
57
|
+
option :graph_status_height, default: DEFAULT_GRAPH_STATUS_HEIGHT
|
58
|
+
|
59
|
+
option :display_attributes_on_hover, default: false
|
60
|
+
|
61
|
+
before_body do
|
62
|
+
self.lines = [lines] if lines.is_a?(Hash)
|
63
|
+
end
|
64
|
+
|
65
|
+
after_body do
|
66
|
+
observe(self, :lines) do
|
67
|
+
clear_drawing_cache
|
68
|
+
body_root.queue_redraw_all
|
69
|
+
end
|
70
|
+
observe(self, :width) do
|
71
|
+
clear_drawing_cache
|
72
|
+
end
|
73
|
+
observe(self, :height) do
|
74
|
+
clear_drawing_cache
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
body {
|
79
|
+
area { |graph_area|
|
80
|
+
on_draw do
|
81
|
+
calculate_dynamic_options
|
82
|
+
graph_background
|
83
|
+
grid_lines
|
84
|
+
all_line_graphs
|
85
|
+
periodic_lines
|
86
|
+
hover_stats
|
87
|
+
end
|
88
|
+
|
89
|
+
on_mouse_moved do |event|
|
90
|
+
@hover_point = {x: event[:x], y: event[:y]}
|
91
|
+
|
92
|
+
if @hover_point && lines && lines[0] && @points && @points[lines[0]] && !@points[lines[0]].empty?
|
93
|
+
x = @hover_point[:x]
|
94
|
+
closest_point_index = @points[lines[0]].each_with_index.min_by { |point, index| (point[:x] - x).abs }[1]
|
95
|
+
if closest_point_index != @closest_point_index
|
96
|
+
@closest_point_index = closest_point_index
|
97
|
+
graph_area.queue_redraw_all
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
on_mouse_exited do |outside|
|
103
|
+
if !@hover_point.nil?
|
104
|
+
@hover_point = nil
|
105
|
+
@closest_point_index = nil
|
106
|
+
graph_area.queue_redraw_all
|
107
|
+
end
|
108
|
+
end
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def clear_drawing_cache
|
115
|
+
@graph_point_distance_per_line = nil
|
116
|
+
@y_value_max_for_all_lines = nil
|
117
|
+
@grid_marker_points = nil
|
118
|
+
@points = nil
|
119
|
+
end
|
120
|
+
|
121
|
+
def calculate_dynamic_options
|
122
|
+
calculate_graph_point_distance_per_line
|
123
|
+
end
|
124
|
+
|
125
|
+
def calculate_graph_point_distance_per_line
|
126
|
+
return unless graph_point_distance == :width_divided_by_point_count
|
127
|
+
|
128
|
+
@graph_point_distance_per_line = lines.inject({}) do |hash, line|
|
129
|
+
hash.merge(line => (width - 2.0*graph_padding_width - graph_grid_marker_padding_width) / (line[:y_values].size - 1).to_f)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def graph_point_distance_for_line(line)
|
134
|
+
@graph_point_distance_per_line&.[](line) || graph_point_distance
|
135
|
+
end
|
136
|
+
|
137
|
+
def graph_background
|
138
|
+
rectangle(0, 0, width, height + (display_attributes_on_hover ? graph_status_height : 0)) {
|
139
|
+
fill 255, 255, 255
|
140
|
+
}
|
141
|
+
end
|
142
|
+
|
143
|
+
def grid_lines
|
144
|
+
line(graph_padding_width, graph_padding_height, graph_padding_width, height - graph_padding_height) {
|
145
|
+
stroke graph_stroke_grid
|
146
|
+
}
|
147
|
+
line(graph_padding_width, height - graph_padding_height, width - graph_padding_width, height - graph_padding_height) {
|
148
|
+
stroke graph_stroke_grid
|
149
|
+
}
|
150
|
+
grid_marker_points.each_with_index do |marker_point, index|
|
151
|
+
grid_marker_number_value = (grid_marker_points.size - index).to_i
|
152
|
+
grid_marker_number = (grid_marker_number_value >= 1000) ? "#{grid_marker_number_value / 1000}K" : grid_marker_number_value.to_s
|
153
|
+
graph_stroke_marker_value = Glimmer::LibUI.interpret_color(graph_stroke_marker)
|
154
|
+
graph_stroke_marker_value[:thickness] = (index != grid_marker_points.size - 1 ? 2 : 1) if graph_stroke_marker_value[:thickness].nil?
|
155
|
+
mod_value = (2 * ((grid_marker_points.size / max_marker_count) + 1))
|
156
|
+
comparison_value = (mod_value > 2) ? 0 : 1
|
157
|
+
if mod_value > 2
|
158
|
+
if grid_marker_number_value % mod_value == comparison_value
|
159
|
+
line(marker_point[:x], marker_point[:y], marker_point[:x] + 4, marker_point[:y]) {
|
160
|
+
stroke graph_stroke_marker_value
|
161
|
+
}
|
162
|
+
end
|
163
|
+
else
|
164
|
+
line(marker_point[:x], marker_point[:y], marker_point[:x] + 4, marker_point[:y]) {
|
165
|
+
stroke graph_stroke_marker_value
|
166
|
+
}
|
167
|
+
end
|
168
|
+
if grid_marker_number_value % mod_value == comparison_value && grid_marker_number_value != grid_marker_points.size
|
169
|
+
line(marker_point[:x], marker_point[:y], marker_point[:x] + width - graph_padding_width, marker_point[:y]) {
|
170
|
+
stroke graph_stroke_marker_line
|
171
|
+
}
|
172
|
+
end
|
173
|
+
if grid_marker_number_value % mod_value == comparison_value
|
174
|
+
grid_marker_number_font = graph_font_marker_text.merge(size: 11)
|
175
|
+
grid_marker_number_width = estimate_width_of_text(grid_marker_number, grid_marker_number_font)
|
176
|
+
text(marker_point[:x] + 4 + 3, marker_point[:y] - 6, grid_marker_number_width) {
|
177
|
+
string(grid_marker_number) {
|
178
|
+
font grid_marker_number_font
|
179
|
+
color graph_color_marker_text
|
180
|
+
}
|
181
|
+
}
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def grid_marker_points
|
187
|
+
if @grid_marker_points.nil?
|
188
|
+
graph_max = [y_value_max_for_all_lines, 1].max
|
189
|
+
current_graph_height = (height - graph_padding_height * 2)
|
190
|
+
division_height = current_graph_height / graph_max
|
191
|
+
@grid_marker_points = graph_max.to_i.times.map do |marker_index|
|
192
|
+
x = graph_padding_width
|
193
|
+
y = graph_padding_height + marker_index * division_height
|
194
|
+
{x: x, y: y}
|
195
|
+
end
|
196
|
+
end
|
197
|
+
@grid_marker_points
|
198
|
+
end
|
199
|
+
|
200
|
+
def max_marker_count
|
201
|
+
[(0.15*height).to_i, 1].max
|
202
|
+
end
|
203
|
+
|
204
|
+
def all_line_graphs
|
205
|
+
lines.each { |graph_line| single_line_graph(graph_line) }
|
206
|
+
end
|
207
|
+
|
208
|
+
def single_line_graph(graph_line)
|
209
|
+
last_point = nil
|
210
|
+
points = calculate_points(graph_line)
|
211
|
+
points.each do |point|
|
212
|
+
if last_point
|
213
|
+
line(last_point[:x], last_point[:y], point[:x], point[:y]) {
|
214
|
+
stroke graph_line[:stroke]
|
215
|
+
}
|
216
|
+
end
|
217
|
+
last_point = point
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def calculate_points(graph_line)
|
222
|
+
@points ||= {}
|
223
|
+
if @points[graph_line].nil?
|
224
|
+
y_values = graph_line[:y_values] || []
|
225
|
+
y_values = y_values[0, max_visible_point_count(graph_line)]
|
226
|
+
graph_max = [y_value_max_for_all_lines, 1].max
|
227
|
+
points = y_values.each_with_index.map do |y_value, index|
|
228
|
+
x = width - graph_padding_width - (index * graph_point_distance_for_line(graph_line))
|
229
|
+
y = ((height - graph_padding_height) - y_value * ((height - graph_padding_height * 2) / graph_max))
|
230
|
+
{x: x, y: y, index: index, y_value: y_value}
|
231
|
+
end
|
232
|
+
@points[graph_line] = translate_points(graph_line, points)
|
233
|
+
end
|
234
|
+
@points[graph_line]
|
235
|
+
end
|
236
|
+
|
237
|
+
def y_value_max_for_all_lines
|
238
|
+
if @y_value_max_for_all_lines.nil?
|
239
|
+
all_y_values = lines.map { |line| line[:y_values] }.reduce(:+)
|
240
|
+
@y_value_max_for_all_lines = all_y_values.max.to_f
|
241
|
+
end
|
242
|
+
@y_value_max_for_all_lines
|
243
|
+
end
|
244
|
+
|
245
|
+
def translate_points(graph_line, points)
|
246
|
+
max_job_count_before_translation = ((width / graph_point_distance_for_line(graph_line)).to_i + 1)
|
247
|
+
x_translation = [(points.size - max_job_count_before_translation) * graph_point_distance_for_line(graph_line), 0].max
|
248
|
+
if x_translation > 0
|
249
|
+
points.each do |point|
|
250
|
+
# need to check if point[:x] is present because if the user shrinks the window, we drop points
|
251
|
+
point[:x] = point[:x] - x_translation if point[:x]
|
252
|
+
end
|
253
|
+
end
|
254
|
+
points
|
255
|
+
end
|
256
|
+
|
257
|
+
def max_visible_point_count(graph_line) = (width / graph_point_distance_for_line(graph_line)).to_i + 1
|
258
|
+
|
259
|
+
def periodic_lines
|
260
|
+
return unless lines && lines[0] && lines[0][:x_interval_in_seconds] && lines[0][:x_interval_in_seconds] == DAY_IN_SECONDS
|
261
|
+
day_count = lines[0][:y_values].size
|
262
|
+
case day_count
|
263
|
+
when ..7
|
264
|
+
@points[lines[0]].each do |point|
|
265
|
+
line(point[:x], graph_padding_height, point[:x], height - graph_padding_height) {
|
266
|
+
stroke graph_stroke_periodic_line
|
267
|
+
}
|
268
|
+
day = calculated_x_value(point[:index]).strftime("%e")
|
269
|
+
font_size = graph_font_marker_text[:size]
|
270
|
+
text(point[:x], height - graph_padding_height - font_size*1.4, font_size*2) {
|
271
|
+
string(day) {
|
272
|
+
font graph_font_marker_text
|
273
|
+
color graph_color_period_text
|
274
|
+
}
|
275
|
+
}
|
276
|
+
end
|
277
|
+
when ..30
|
278
|
+
@points[lines[0]].each_with_index do |point, index|
|
279
|
+
day_number = index + 1
|
280
|
+
if day_number % 7 == 0
|
281
|
+
line(point[:x], graph_padding_height, point[:x], height - graph_padding_height) {
|
282
|
+
stroke graph_stroke_periodic_line
|
283
|
+
}
|
284
|
+
date = calculated_x_value(point[:index]).strftime("%b %e")
|
285
|
+
font_size = graph_font_marker_text[:size]
|
286
|
+
text(point[:x] + 4, height - graph_padding_height - font_size*1.4, font_size*6) {
|
287
|
+
string(date) {
|
288
|
+
font graph_font_marker_text
|
289
|
+
color graph_color_period_text
|
290
|
+
}
|
291
|
+
}
|
292
|
+
end
|
293
|
+
end
|
294
|
+
else
|
295
|
+
@points[lines[0]].each do |point|
|
296
|
+
if calculated_x_value(point[:index]).strftime("%d") == "01"
|
297
|
+
line(point[:x], graph_padding_height, point[:x], height - graph_padding_height) {
|
298
|
+
stroke graph_stroke_periodic_line
|
299
|
+
}
|
300
|
+
date = calculated_x_value(point[:index]).strftime("%b")
|
301
|
+
font_size = graph_font_marker_text[:size]
|
302
|
+
text(point[:x] + 4, height - graph_padding_height - font_size*1.4, font_size*6) {
|
303
|
+
string(date) {
|
304
|
+
font graph_font_marker_text
|
305
|
+
color graph_color_period_text
|
306
|
+
}
|
307
|
+
}
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def hover_stats
|
314
|
+
return unless display_attributes_on_hover
|
315
|
+
|
316
|
+
require "bigdecimal"
|
317
|
+
require "perfect_shape/point"
|
318
|
+
|
319
|
+
if @hover_point && lines && lines[0] && @points && @points[lines[0]] && !@points[lines[0]].empty?
|
320
|
+
x = @hover_point[:x]
|
321
|
+
closest_points = lines.map { |line| @points[line][@closest_point_index] }
|
322
|
+
closest_x = closest_points[0]&.[](:x)
|
323
|
+
closest_x_distance = PerfectShape::Point.point_distance(x.to_f, 0, closest_x.to_f, 0)
|
324
|
+
# Today, we make the assumption that all lines have points along the same x-axis values
|
325
|
+
# TODO In the future, we can support different x values along different lines
|
326
|
+
if closest_x_distance < graph_point_distance_for_line(lines[0])
|
327
|
+
line(closest_x, graph_padding_height, closest_x, height - graph_padding_height) {
|
328
|
+
stroke graph_stroke_hover_line
|
329
|
+
}
|
330
|
+
closest_points.each_with_index do |closest_point, index|
|
331
|
+
circle(closest_point[:x], closest_point[:y], 4) {
|
332
|
+
fill lines[index][:stroke]
|
333
|
+
}
|
334
|
+
circle(closest_point[:x], closest_point[:y], 2) {
|
335
|
+
fill :white
|
336
|
+
}
|
337
|
+
end
|
338
|
+
text_label = formatted_x_value(@closest_point_index)
|
339
|
+
text_label_width = estimate_width_of_text(text_label, DEFAULT_GRAPH_FONT_MARKER_TEXT)
|
340
|
+
closest_point_texts = lines.map { |line| "#{line[:name]}: #{line[:y_values][@closest_point_index]}" }
|
341
|
+
closest_point_text_widths = closest_point_texts.map do |text|
|
342
|
+
estimate_width_of_text(text, graph_font_marker_text)
|
343
|
+
end
|
344
|
+
square_size = 12.0
|
345
|
+
square_to_label_padding = 10.0
|
346
|
+
label_padding = 10.0
|
347
|
+
text_label_x = width - graph_padding_width - text_label_width - label_padding -
|
348
|
+
(lines.size*(square_size + square_to_label_padding) + (lines.size - 1)*label_padding + closest_point_text_widths.sum)
|
349
|
+
text_label_y = height + graph_padding_height
|
350
|
+
|
351
|
+
text(text_label_x, text_label_y, text_label_width) {
|
352
|
+
string(text_label) {
|
353
|
+
font DEFAULT_GRAPH_FONT_MARKER_TEXT
|
354
|
+
color graph_color_marker_text
|
355
|
+
}
|
356
|
+
}
|
357
|
+
|
358
|
+
relative_x = text_label_x + text_label_width
|
359
|
+
lines.size.times do |index|
|
360
|
+
square_x = relative_x + label_padding
|
361
|
+
|
362
|
+
square(square_x, text_label_y + 2, square_size) {
|
363
|
+
fill lines[index][:stroke]
|
364
|
+
}
|
365
|
+
|
366
|
+
attribute_label_x = square_x + square_size + square_to_label_padding
|
367
|
+
attribute_text = closest_point_texts[index]
|
368
|
+
attribute_text_width = closest_point_text_widths[index]
|
369
|
+
relative_x = attribute_label_x + attribute_text_width
|
370
|
+
|
371
|
+
text(attribute_label_x, text_label_y, attribute_text_width) {
|
372
|
+
string(attribute_text) {
|
373
|
+
font graph_font_marker_text
|
374
|
+
color graph_color_marker_text
|
375
|
+
}
|
376
|
+
}
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
def formatted_x_value(x_value_index)
|
383
|
+
# Today, we make the assumption that all lines have points along the same x-axis values
|
384
|
+
# TODO In the future, we can support different x values along different lines
|
385
|
+
graph_line = lines[0]
|
386
|
+
x_value_format = graph_line[:x_value_format] || :to_s
|
387
|
+
x_value = calculated_x_value(x_value_index)
|
388
|
+
if (x_value_format.is_a?(Symbol) || x_value_format.is_a?(String))
|
389
|
+
x_value.send(x_value_format)
|
390
|
+
else
|
391
|
+
x_value_format.call(x_value)
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
def calculated_x_value(x_value_index)
|
396
|
+
# Today, we make the assumption that all lines have points along the same x-axis values
|
397
|
+
# TODO In the future, we can support different x values along different lines
|
398
|
+
graph_line = lines[0]
|
399
|
+
graph_line[:x_value_start] - (graph_line[:x_interval_in_seconds] * x_value_index)
|
400
|
+
end
|
401
|
+
|
402
|
+
def estimate_width_of_text(text_string, font_properties)
|
403
|
+
font_size = font_properties[:size] || 16
|
404
|
+
estimated_font_width = 0.6 * font_size
|
405
|
+
text_string.chars.size * estimated_font_width
|
406
|
+
end
|
407
|
+
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glimmer-libui-cc-graphs_and_charts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Maleh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: glimmer-dsl-libui
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.11'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.5.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: juwelier
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.4.9
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.4.9
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Graphs And Charts - Glimmer DSL for LibUI Custom Controls
|
70
|
+
email: andy.am@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.md
|
76
|
+
files:
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- VERSION
|
80
|
+
- lib/glimmer-libui-cc-graphs_and_charts.rb
|
81
|
+
- lib/glimmer/view/line_graph.rb
|
82
|
+
homepage: http://github.com/AndyObtiva/glimmer-libui-cc-graphs_and_charts
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.4.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Graphs And Charts - Glimmer DSL for LibUI Custom Controls
|
105
|
+
test_files: []
|