glimmer-libui-cc-graphs_and_charts 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bad5c6db427c320e3b452ee60e2b3af416604525c96e74c915b59c905d746b07
4
- data.tar.gz: 3659c73cd8a91a9da31c5cf2cb896b04130a7e1a4c58d2f2e92f7b841726de58
3
+ metadata.gz: e5c81413f717dac087558fbed16937659603f9bb6a4a2b849cadf31bade1750c
4
+ data.tar.gz: c76f09ab3cff5d2486c45c59841ed1bacff95ad6d7b63033e01965318adef73e
5
5
  SHA512:
6
- metadata.gz: 9ff88818e482120a5492d7bb2a789afc89d1a40efed3411d355afcc5db7737ef5f5a5e206a3a2737ccdcaf11a8ba01592a398878b1cd005153d1d3ead08d2728
7
- data.tar.gz: b487f6b49b979d6b3a72a2c174f8cd55c558037e2baf2997b6ddaf0a2b2e9ffa243d62ce48558966115a07bfaccf69802682a7d94b0af4043dad29da5e33e909
6
+ metadata.gz: 82239ec1af1f0186761ac787f94d2f5756abc9cc17ea67dbaf5f38f9b27c35cc834c947f46df19309bee5f425d9a764a0ed335163bcc764a2e84b44d6ebf7b4a
7
+ data.tar.gz: 65c1a640f16352f4ec249bff010824872b4c7bea47d818d7264fc4c2b9c57ab40307f32fa0f22c449944d3d7ab51cd07d4ae513e12255d2a7da9de4cb6e46fe3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.1.8
4
+
5
+ - Fix the display of grid marker lines when passing `values` with non-Integer y-axis values (especially max y-axis value being non-Integer). It now shows highest grid marker having a non-Integer value while keeping smaller values as Integer (e.g. 1, 2, 3, 3.75).
6
+
7
+ ## 0.1.7
8
+
9
+ - Scale `y` axis when using `values` option to fill up the vertical graph height if all y-axis values are smaller than `1`
10
+
3
11
  ## 0.1.6
4
12
 
5
13
  - Support passing line `values` as a `Hash` map of x-axis values to y-axis values instead of using the combination of `y_values`, `x_value_start`, and `x_interval_in_seconds`
data/README.md CHANGED
@@ -1,18 +1,18 @@
1
- # Graphs and Charts 0.1.6 (Alpha)
1
+ # Graphs and Charts 0.1.8 (Alpha)
2
2
  ## [Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui) Custom Controls
3
3
  [![Gem Version](https://badge.fury.io/rb/glimmer-libui-cc-graphs_and_charts.svg)](http://badge.fury.io/rb/glimmer-libui-cc-graphs_and_charts)
4
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
5
 
6
6
  Graphs and Charts (Custom Controls) for [Glimmer DSL for LibUI](https://github.com/AndyObtiva/glimmer-dsl-libui)
7
7
 
8
- ![line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph.png)
8
+ ![line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph-relative.png)
9
9
 
10
10
  ## Setup
11
11
 
12
12
  Add this line to Bundler `Gemfile`:
13
13
 
14
14
  ```ruby
15
- gem 'glimmer-libui-cc-graphs_and_charts', '~> 0.1.6'
15
+ gem 'glimmer-libui-cc-graphs_and_charts', '~> 0.1.8'
16
16
  ```
17
17
 
18
18
  Run:
@@ -42,6 +42,53 @@ require 'glimmer/view/line_graph'
42
42
  This makes the `line_graph` [Glimmer DSL for LibUI Custom Control](https://github.com/AndyObtiva/glimmer-dsl-libui#custom-components) available in the Glimmer GUI DSL.
43
43
  You can then nest `line_graph` under `window` or some container like `vertical_box`. By the way, `line_graph` is implemented on top of the [`area` Glimmer DSL for LibUI control](https://github.com/AndyObtiva/glimmer-dsl-libui#area-api).
44
44
 
45
+ Note that you can use in absolute mode or relative mode for determining x-axis values starting from newest point to oldest point along the time x-axis:
46
+ - Absolute Mode: pass `values` which maps x-axis values to y-axis values
47
+ - Relative Mode: pass `y_values`, `x_value_start`, and `x_interval_in_seconds` (x-axis values are calculated automatically in a uniform way from `x_value_start` deducting `x_interval_in_seconds`)
48
+
49
+ **Absolute Mode:**
50
+
51
+ It supports any `Numeric` y-axis values in addition to `Time` x-axis values.
52
+
53
+ ```ruby
54
+ @line_graph = line_graph(
55
+ width: 900,
56
+ height: 300,
57
+ lines: [
58
+ {
59
+ name: 'Stock 1',
60
+ stroke: [163, 40, 39, thickness: 2],
61
+ values: {
62
+ Time.new(2030, 12, 1) => 80,
63
+ Time.new(2030, 12, 2) => 36,
64
+ Time.new(2030, 12, 4) => 10,
65
+ Time.new(2030, 12, 5) => 60,
66
+ Time.new(2030, 12, 6) => 20,
67
+ },
68
+ x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
69
+ },
70
+ {
71
+ name: 'Stock 2',
72
+ stroke: [47, 109, 104, thickness: 2],
73
+ values: {
74
+ Time.new(2030, 12, 1) => 62,
75
+ Time.new(2030, 12, 2) => 0,
76
+ Time.new(2030, 12, 3) => 90,
77
+ Time.new(2030, 12, 5) => 0,
78
+ Time.new(2030, 12, 7) => 17,
79
+ },
80
+ x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
81
+ },
82
+ ],
83
+ )
84
+ ```
85
+
86
+ ![basic line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph.png)
87
+
88
+ **Relative Mode:**
89
+
90
+ Currently, it only supports `Integer` y-axis values in addition to `Time` x-axis values.
91
+
45
92
  ```ruby
46
93
  line_graph(
47
94
  width: 900,
@@ -69,11 +116,11 @@ line_graph(
69
116
  )
70
117
  ```
71
118
 
72
- ![basic line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph.png)
119
+ ![basic line graph relative](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph-relative.png)
73
120
 
74
121
  Look into [lib/glimmer/view/line_graph.rb](/lib/glimmer/view/line_graph.rb) to learn about all supported options.
75
122
 
76
- Basic Line Graph Example:
123
+ **Basic Line Graph Example:**
77
124
 
78
125
  [examples/graphs_and_charts/basic_line_graph.rb](/examples/graphs_and_charts/basic_line_graph.rb)
79
126
 
@@ -84,12 +131,67 @@ require 'glimmer/view/line_graph'
84
131
  class BasicLineGraph
85
132
  include Glimmer::LibUI::Application
86
133
 
134
+ body {
135
+ window('Basic Line Graph', 900, 300) { |main_window|
136
+ @line_graph = line_graph(
137
+ width: 900,
138
+ height: 300,
139
+ lines: [
140
+ {
141
+ name: 'Stock 1',
142
+ stroke: [163, 40, 39, thickness: 2],
143
+ values: {
144
+ Time.new(2030, 12, 1) => 80,
145
+ Time.new(2030, 12, 2) => 36,
146
+ Time.new(2030, 12, 4) => 10,
147
+ Time.new(2030, 12, 5) => 60,
148
+ Time.new(2030, 12, 6) => 20,
149
+ },
150
+ x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
151
+ },
152
+ {
153
+ name: 'Stock 2',
154
+ stroke: [47, 109, 104, thickness: 2],
155
+ values: {
156
+ Time.new(2030, 12, 1) => 62,
157
+ Time.new(2030, 12, 2) => 0,
158
+ Time.new(2030, 12, 3) => 90,
159
+ Time.new(2030, 12, 5) => 0,
160
+ Time.new(2030, 12, 7) => 17,
161
+ },
162
+ x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
163
+ },
164
+ ],
165
+ )
166
+
167
+ on_content_size_changed do
168
+ @line_graph.width = main_window.content_size[0]
169
+ @line_graph.height = main_window.content_size[1]
170
+ end
171
+ }
172
+ }
173
+ end
174
+
175
+ BasicLineGraph.launch
176
+ ```
177
+
178
+ ![basic line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph.png)
179
+
180
+ **Basic Line Graph Relative Example:**
181
+
182
+ ```ruby
183
+ require 'glimmer-dsl-libui'
184
+ require 'glimmer/view/line_graph'
185
+
186
+ class BasicLineGraphRelative
187
+ include Glimmer::LibUI::Application
188
+
87
189
  before_body do
88
190
  @start_time = Time.now
89
191
  end
90
192
 
91
193
  body {
92
- window('Basic Line Graph', 900, 330) {
194
+ window('Basic Line Graph Relative', 900, 330) {
93
195
  line_graph(
94
196
  width: 900,
95
197
  height: 300,
@@ -118,10 +220,10 @@ class BasicLineGraph
118
220
  }
119
221
  end
120
222
 
121
- BasicLineGraph.launch
223
+ BasicLineGraphRelative.launch
122
224
  ```
123
225
 
124
- ![basic line graph](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph.png)
226
+ ![basic line graph relative](/screenshots/glimmer-libui-cc-graphs_and_charts-mac-basic-line-graph-relative.png)
125
227
 
126
228
  Contributing to glimmer-libui-cc-graphs_and_charts
127
229
  ------------------------------------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.8
@@ -17,11 +17,11 @@ class BasicLineGraph
17
17
  name: 'Stock 1',
18
18
  stroke: [163, 40, 39, thickness: 2],
19
19
  values: {
20
- Time.new(2030, 12, 1) => 80,
21
- Time.new(2030, 12, 2) => 36,
22
- Time.new(2030, 12, 4) => 10,
23
- Time.new(2030, 12, 5) => 60,
24
- Time.new(2030, 12, 6) => 20,
20
+ Time.new(2030, 12, 1) => 2,
21
+ Time.new(2030, 12, 2) => 2.5,
22
+ Time.new(2030, 12, 4) => 1,
23
+ Time.new(2030, 12, 5) => 0,
24
+ Time.new(2030, 12, 6) => 2,
25
25
  },
26
26
  x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
27
27
  },
@@ -29,11 +29,11 @@ class BasicLineGraph
29
29
  name: 'Stock 2',
30
30
  stroke: [47, 109, 104, thickness: 2],
31
31
  values: {
32
- Time.new(2030, 12, 1) => 62,
32
+ Time.new(2030, 12, 1) => 2,
33
33
  Time.new(2030, 12, 2) => 0,
34
- Time.new(2030, 12, 3) => 90,
35
- Time.new(2030, 12, 5) => 0,
36
- Time.new(2030, 12, 7) => 17,
34
+ Time.new(2030, 12, 3) => 2.5,
35
+ Time.new(2030, 12, 5) => 1,
36
+ Time.new(2030, 12, 7) => 2,
37
37
  },
38
38
  x_value_format: -> (time) {time.strftime("%a %d %b %Y %T GMT")},
39
39
  },
@@ -4,7 +4,7 @@ $LOAD_PATH.prepend(File.expand_path(File.join(__dir__, '..', '..', 'lib'))) if F
4
4
  require 'glimmer-dsl-libui'
5
5
  require 'glimmer/view/line_graph'
6
6
 
7
- class BasicLineGraph
7
+ class BasicLineGraphRelative
8
8
  include Glimmer::LibUI::Application
9
9
 
10
10
  before_body do
@@ -12,7 +12,7 @@ class BasicLineGraph
12
12
  end
13
13
 
14
14
  body {
15
- window('Basic Line Graph', 900, 330) { |main_window|
15
+ window('Basic Line Graph Relative', 900, 330) { |main_window|
16
16
  @line_graph = line_graph(
17
17
  width: 900,
18
18
  height: 300,
@@ -46,4 +46,4 @@ class BasicLineGraph
46
46
  }
47
47
  end
48
48
 
49
- BasicLineGraph.launch
49
+ BasicLineGraphRelative.launch
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: glimmer-libui-cc-graphs_and_charts 0.1.6 ruby lib
5
+ # stub: glimmer-libui-cc-graphs_and_charts 0.1.8 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "glimmer-libui-cc-graphs_and_charts".freeze
9
- s.version = "0.1.6".freeze
9
+ s.version = "0.1.8".freeze
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Andy Maleh".freeze]
14
- s.date = "2023-12-22"
14
+ s.date = "2023-12-26"
15
15
  s.description = "Graphs and Charts (Custom Controls) for Glimmer DSL for LibUI, like Line Graph.".freeze
16
16
  s.email = "andy.am@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -132,6 +132,7 @@ module Glimmer
132
132
  @graph_point_distance_per_line = nil
133
133
  @y_value_max_for_all_lines = nil
134
134
  @x_resolution = nil
135
+ @y_resolution = nil
135
136
  @x_value_range_for_all_lines = nil
136
137
  @x_value_min_for_all_lines = nil
137
138
  @x_value_max_for_all_lines = nil
@@ -153,15 +154,19 @@ module Glimmer
153
154
 
154
155
  @graph_point_distance_per_line ||= lines.inject({}) do |hash, line|
155
156
  value = (width - 2.0*graph_padding_width - graph_grid_marker_padding_width) / (line[:y_values].size - 1).to_f
156
- value = [value, drawable_width].min
157
+ value = [value, width_drawable].min
157
158
  hash.merge(line => value)
158
159
  end
159
160
  end
160
161
 
161
- def drawable_width
162
+ def width_drawable
162
163
  width - 2.0*graph_padding_width - graph_grid_marker_padding_width
163
164
  end
164
165
 
166
+ def height_drawable
167
+ height - 2.0*graph_padding_height
168
+ end
169
+
165
170
  def graph_point_distance_for_line(line)
166
171
  @graph_point_distance_per_line&.[](line) || graph_point_distance
167
172
  end
@@ -185,7 +190,11 @@ module Glimmer
185
190
  @graph_stroke_marker_values ||= []
186
191
  @mod_values ||= []
187
192
  grid_marker_points.each_with_index do |marker_point, index|
188
- @grid_marker_number_values[index] ||= (grid_marker_points.size - index).to_i
193
+ @grid_marker_number_values[index] ||= begin
194
+ value = (grid_marker_points.size - index).to_i
195
+ value = y_value_max_for_all_lines if !y_value_max_for_all_lines.nil? && y_value_max_for_all_lines.to_i != y_value_max_for_all_lines && index == 0
196
+ value
197
+ end
189
198
  grid_marker_number_value = @grid_marker_number_values[index]
190
199
  @grid_marker_numbers[index] ||= (grid_marker_number_value >= 1000) ? "#{grid_marker_number_value / 1000}K" : grid_marker_number_value.to_s
191
200
  grid_marker_number = @grid_marker_numbers[index]
@@ -215,7 +224,7 @@ module Glimmer
215
224
  stroke graph_stroke_marker_line
216
225
  }
217
226
  end
218
- if grid_marker_number_value % mod_value == comparison_value
227
+ if grid_marker_number_value % mod_value == comparison_value || grid_marker_number_value != grid_marker_number_value.to_i
219
228
  grid_marker_number_width = estimate_width_of_text(grid_marker_number, grid_marker_number_font)
220
229
  text(marker_point[:x] + 4 + 3, marker_point[:y] - 6, grid_marker_number_width) {
221
230
  string(grid_marker_number) {
@@ -229,15 +238,31 @@ module Glimmer
229
238
 
230
239
  def grid_marker_points
231
240
  if @grid_marker_points.nil?
232
- graph_max = [y_value_max_for_all_lines, 1].max
233
- current_graph_height = (height - graph_padding_height * 2)
234
- division_height = current_graph_height / graph_max
235
- @grid_marker_points = graph_max.to_i.times.map do |marker_index|
236
- x = graph_padding_width
237
- y = graph_padding_height + marker_index * division_height
238
- {x: x, y: y}
241
+ if lines[0]&.[](:y_values)
242
+ graph_y_max = [y_value_max_for_all_lines, 1].max
243
+ current_graph_height = (height - graph_padding_height * 2)
244
+ division_height = current_graph_height / graph_y_max
245
+ @grid_marker_points = graph_y_max.to_i.times.map do |marker_index|
246
+ x = graph_padding_width
247
+ y = graph_padding_height + marker_index * division_height
248
+ {x: x, y: y}
249
+ end
250
+ else
251
+ graph_y_max = y_value_max_for_all_lines
252
+ y_value_count = graph_y_max.ceil
253
+ @grid_marker_points = y_value_count.times.map do |marker_index|
254
+ x = graph_padding_width
255
+ y_value = y_value_count - marker_index
256
+ if marker_index == 0 && graph_y_max.ceil != graph_y_max.to_i
257
+ y_value = graph_y_max
258
+ end
259
+ scaled_y_value = y_value.to_f * y_resolution.to_f
260
+ y = height - graph_padding_height - scaled_y_value
261
+ {x: x, y: y}
262
+ end
239
263
  end
240
264
  end
265
+
241
266
  @grid_marker_points
242
267
  end
243
268
 
@@ -280,10 +305,10 @@ module Glimmer
280
305
  if @points[graph_line].nil?
281
306
  y_values = graph_line[:y_values] || []
282
307
  y_values = y_values[0, max_visible_point_count(graph_line)]
283
- graph_max = [y_value_max_for_all_lines, 1].max
308
+ graph_y_max = [y_value_max_for_all_lines, 1].max
284
309
  points = y_values.each_with_index.map do |y_value, index|
285
310
  x = width - graph_padding_width - (index * graph_point_distance_for_line(graph_line))
286
- y = ((height - graph_padding_height) - y_value * ((height - graph_padding_height * 2) / graph_max))
311
+ y = ((height - graph_padding_height) - y_value * ((height - graph_padding_height * 2) / graph_y_max))
287
312
  {x: x, y: y, index: index, y_value: y_value}
288
313
  end
289
314
  @points[graph_line] = translate_points(graph_line, points)
@@ -300,9 +325,10 @@ module Glimmer
300
325
  x_value_range_for_all_lines
301
326
  points = values.each_with_index.map do |(x_value, y_value), index|
302
327
  relative_x_value = x_value - x_value_min_for_all_lines
303
- stretched_x_value = x_value_range_for_all_lines == 0 ? 0 : relative_x_value.to_f * x_resolution.to_f
304
- x = width - graph_padding_width - stretched_x_value
305
- y = ((height - graph_padding_height) - y_value * ((height - graph_padding_height * 2) / graph_y_max))
328
+ scaled_x_value = x_value_range_for_all_lines == 0 ? 0 : relative_x_value.to_f * x_resolution.to_f
329
+ scaled_y_value = y_value_max_for_all_lines == 0 ? 0 : y_value.to_f * y_resolution.to_f
330
+ x = width - graph_padding_width - scaled_x_value
331
+ y = height - graph_padding_height - scaled_y_value
306
332
  {x: x, y: y, index: index, x_value: x_value, y_value: y_value}
307
333
  end
308
334
  # Translation is not supported today
@@ -313,9 +339,15 @@ module Glimmer
313
339
  @points[graph_line]
314
340
  end
315
341
 
316
- # this is the multiplier that we must multiply by the relative_x_value
342
+ # this is the multiplier that we must multiply by the relative x value
317
343
  def x_resolution
318
- @x_resolution ||= drawable_width.to_f / x_value_range_for_all_lines.to_f
344
+ @x_resolution ||= width_drawable.to_f / x_value_range_for_all_lines.to_f
345
+ end
346
+
347
+ # this is the multiplier that we must multiply by the relative y value
348
+ def y_resolution
349
+ # TODO in the future, we will use the y range, but today, we assume it starts at 0
350
+ @y_resolution ||= height_drawable.to_f / y_value_max_for_all_lines.to_f
319
351
  end
320
352
 
321
353
  def x_value_range_for_all_lines
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-libui-cc-graphs_and_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-22 00:00:00.000000000 Z
11
+ date: 2023-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer-dsl-libui