glimmer-libui-cc-graphs_and_charts 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bad5c6db427c320e3b452ee60e2b3af416604525c96e74c915b59c905d746b07
4
- data.tar.gz: 3659c73cd8a91a9da31c5cf2cb896b04130a7e1a4c58d2f2e92f7b841726de58
3
+ metadata.gz: c2a02d818ffec27e5d64eafa181c90e1362b62ea8f201018fe92705b0d95a664
4
+ data.tar.gz: 6de4a548a1c74d91cc389663d9917311bd66523402ba984ac05a250d8cff5baa
5
5
  SHA512:
6
- metadata.gz: 9ff88818e482120a5492d7bb2a789afc89d1a40efed3411d355afcc5db7737ef5f5a5e206a3a2737ccdcaf11a8ba01592a398878b1cd005153d1d3ead08d2728
7
- data.tar.gz: b487f6b49b979d6b3a72a2c174f8cd55c558037e2baf2997b6ddaf0a2b2e9ffa243d62ce48558966115a07bfaccf69802682a7d94b0af4043dad29da5e33e909
6
+ metadata.gz: 738a36dedf90e91774876d7cc41f545edb00ac03d65cd6bf89eaf30eac5374b4abfa591d007d971476a02dd854ca14a262a7fcda34fdc24ac531b17509ee0dcf
7
+ data.tar.gz: 2039bf32f7d8fb7930c24fda0eba1def6c30db968d452cb219a7ceab604bfed450b87b8d68ae061d75017ea59f6161d46b9fbebd8ab5581727116ebfd32fec82
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.1.7
4
+
5
+ - Scale `y` axis when using `values` option to fill up the vertical graph height if all y-axis values are smaller than `1`
6
+
3
7
  ## 0.1.6
4
8
 
5
9
  - 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.7 (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.7'
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
 
@@ -81,6 +128,61 @@ Basic Line Graph Example:
81
128
  require 'glimmer-dsl-libui'
82
129
  require 'glimmer/view/line_graph'
83
130
 
131
+ class BasicLineGraph
132
+ include Glimmer::LibUI::Application
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
+
84
186
  class BasicLineGraph
85
187
  include Glimmer::LibUI::Application
86
188
 
@@ -121,7 +223,7 @@ end
121
223
  BasicLineGraph.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.7
@@ -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.7 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.7".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-23"
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
@@ -184,8 +189,15 @@ module Glimmer
184
189
  @grid_marker_numbers ||= []
185
190
  @graph_stroke_marker_values ||= []
186
191
  @mod_values ||= []
192
+ if lines && lines.all? {|line| line[:values]} && !y_value_max_for_all_lines.nil? && y_value_max_for_all_lines > 1
193
+ return
194
+ end
187
195
  grid_marker_points.each_with_index do |marker_point, index|
188
- @grid_marker_number_values[index] ||= (grid_marker_points.size - index).to_i
196
+ @grid_marker_number_values[index] ||= begin
197
+ value = (grid_marker_points.size - index).to_i
198
+ 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 && grid_marker_points.size == 1
199
+ value
200
+ end
189
201
  grid_marker_number_value = @grid_marker_number_values[index]
190
202
  @grid_marker_numbers[index] ||= (grid_marker_number_value >= 1000) ? "#{grid_marker_number_value / 1000}K" : grid_marker_number_value.to_s
191
203
  grid_marker_number = @grid_marker_numbers[index]
@@ -215,7 +227,7 @@ module Glimmer
215
227
  stroke graph_stroke_marker_line
216
228
  }
217
229
  end
218
- if grid_marker_number_value % mod_value == comparison_value
230
+ if grid_marker_number_value % mod_value == comparison_value || grid_marker_number_value != grid_marker_number_value.to_i
219
231
  grid_marker_number_width = estimate_width_of_text(grid_marker_number, grid_marker_number_font)
220
232
  text(marker_point[:x] + 4 + 3, marker_point[:y] - 6, grid_marker_number_width) {
221
233
  string(grid_marker_number) {
@@ -229,10 +241,10 @@ module Glimmer
229
241
 
230
242
  def grid_marker_points
231
243
  if @grid_marker_points.nil?
232
- graph_max = [y_value_max_for_all_lines, 1].max
244
+ graph_y_max = [y_value_max_for_all_lines, y_value_max_for_all_lines.between?(0, 1) ? y_value_max_for_all_lines : 1].max
233
245
  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|
246
+ division_height = current_graph_height / graph_y_max
247
+ @grid_marker_points = graph_y_max.ceil.times.map do |marker_index|
236
248
  x = graph_padding_width
237
249
  y = graph_padding_height + marker_index * division_height
238
250
  {x: x, y: y}
@@ -280,10 +292,10 @@ module Glimmer
280
292
  if @points[graph_line].nil?
281
293
  y_values = graph_line[:y_values] || []
282
294
  y_values = y_values[0, max_visible_point_count(graph_line)]
283
- graph_max = [y_value_max_for_all_lines, 1].max
295
+ graph_y_max = [y_value_max_for_all_lines, 1].max
284
296
  points = y_values.each_with_index.map do |y_value, index|
285
297
  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))
298
+ y = ((height - graph_padding_height) - y_value * ((height - graph_padding_height * 2) / graph_y_max))
287
299
  {x: x, y: y, index: index, y_value: y_value}
288
300
  end
289
301
  @points[graph_line] = translate_points(graph_line, points)
@@ -300,9 +312,10 @@ module Glimmer
300
312
  x_value_range_for_all_lines
301
313
  points = values.each_with_index.map do |(x_value, y_value), index|
302
314
  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))
315
+ scaled_x_value = x_value_range_for_all_lines == 0 ? 0 : relative_x_value.to_f * x_resolution.to_f
316
+ scaled_y_value = y_value_max_for_all_lines == 0 ? 0 : y_value.to_f * y_resolution.to_f
317
+ x = width - graph_padding_width - scaled_x_value
318
+ y = height - graph_padding_height - scaled_y_value
306
319
  {x: x, y: y, index: index, x_value: x_value, y_value: y_value}
307
320
  end
308
321
  # Translation is not supported today
@@ -313,9 +326,15 @@ module Glimmer
313
326
  @points[graph_line]
314
327
  end
315
328
 
316
- # this is the multiplier that we must multiply by the relative_x_value
329
+ # this is the multiplier that we must multiply by the relative x value
317
330
  def x_resolution
318
- @x_resolution ||= drawable_width.to_f / x_value_range_for_all_lines.to_f
331
+ @x_resolution ||= width_drawable.to_f / x_value_range_for_all_lines.to_f
332
+ end
333
+
334
+ # this is the multiplier that we must multiply by the relative y value
335
+ def y_resolution
336
+ # TODO in the future, we will use the y range, but today, we assume it starts at 0
337
+ @y_resolution ||= height_drawable.to_f / y_value_max_for_all_lines.to_f
319
338
  end
320
339
 
321
340
  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.7
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-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer-dsl-libui