minichart 0.3.0 → 0.3.1

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: 6109cfc57cb909c2ba0a9df4e184bc4319220399b774ff2098b728bf4a41f57e
4
- data.tar.gz: 32cd8ff3c3260d08f81106c3ac74037e36990d4601eb8ff7e40c8840d44b3ca3
3
+ metadata.gz: 133646d3a37d8994e640a03eebd3a631f608e1decfd7c5666271dc07c4acbbc0
4
+ data.tar.gz: b4dd484bd550a26b8ef2f7be811a79a1cefe79dd0c6b13e5ebbe5d13a2bc1f63
5
5
  SHA512:
6
- metadata.gz: 63e01d19bd4539ccf1283a98bee8f941a7e270ee0f1a2956aed20ecbab21661e7923420588c28f0aa90027cfc6fadc88f5f90b5776ff8dfa3523c9faf85c03ba
7
- data.tar.gz: 1861ffff3d852e85fbf9b8c334d091f6dd40ce823086ae4215edb95c96f20a5f9e79e4da5caaf51b35956a8da92ca9de054ee35aae67c84884c02ad5f59af628
6
+ metadata.gz: 3f7e5d813fc8cccf069494ef14ac4b4c0db59147b3f1467e064b33d411833387da6589484a7bbba578846e632161a944063ce87f5f1225ee020629473b89ec2f
7
+ data.tar.gz: 37cc2233dae92905239c27ee4ae70c6d18b15b9fd0813f0ee9d6d99a894553cb501da3f3f321dbbcfa032d1e2c0e4442bba72fe87d42c7c0c5aebc06560366f6
data/README.md CHANGED
@@ -16,6 +16,8 @@ Create SVG mini charts with Ruby
16
16
  * [Area Chart](#area-chart)
17
17
  * [Horizontal Bar Meter](#horizontal-bar-meter)
18
18
  * [Vertical Bar Meter](#vertical-bar-meter)
19
+ * [Horizontal Status Leds](#horizontal-status-leds)
20
+ * [Vertical Status Leds](#vertical-status-leds)
19
21
  * [Configuration](#configuration)
20
22
  * [Class-level default options](#class-level-default-options)
21
23
  * [Instance initialization options](#instance-initialization-options)
@@ -23,6 +25,7 @@ Create SVG mini charts with Ruby
23
25
  * [Options Reference](#options-reference)
24
26
  * [Basic Options](#basic-options)
25
27
  * [Meter Options](#meter-options)
28
+ * [Leds Options](#leds-options)
26
29
  * [Examples](#examples)
27
30
 
28
31
 
@@ -87,8 +90,8 @@ The objects returned from all the mini chart classes are [Victor::SVG][2] object
87
90
  <img src='examples/line_chart.svg' align='right' width=300>
88
91
 
89
92
  ```ruby
90
- LineChart.new data, height: 50, background: '#eee',
91
- aspect_ratio: 5, color: 'green'
93
+ LineChart.new [10, 30, 20, 40, 30], background: '#eee',
94
+ height: 50, width: 250, color: 'green'
92
95
  ```
93
96
 
94
97
  ### Bar Chart
@@ -96,8 +99,8 @@ LineChart.new data, height: 50, background: '#eee',
96
99
  <img src='examples/bar_chart.svg' align='right' width=300>
97
100
 
98
101
  ```ruby
99
- BarChart.new data, height: 50, background: '#eee',
100
- aspect_ratio: 5, color: 'green'
102
+ BarChart.new [10, 30, 20, 40, 30], background: '#eee',
103
+ height: 50, width: 250, color: 'green'
101
104
  ```
102
105
 
103
106
  ### Area Chart
@@ -105,8 +108,8 @@ BarChart.new data, height: 50, background: '#eee',
105
108
  <img src='examples/area_chart.svg' align='right' width=300>
106
109
 
107
110
  ```ruby
108
- AreaChart.new data, height: 50, background: '#eee',
109
- aspect_ratio: 5, color: 'green'
111
+ AreaChart.new [10, 30, 20, 40, 30], background: '#eee',
112
+ height: 50, width: 250, color: 'green'
110
113
  ```
111
114
 
112
115
  ### Horizontal Bar Meter
@@ -122,7 +125,7 @@ negative = HorizontalBarMeter.new -80,
122
125
 
123
126
  dual = HorizontalBarMeter.new 80,
124
127
  height: 20, width: 250, background: '#99f', color: 'blue',
125
- mode: :dual, zero_line: true
128
+ mode: :dual, notches: [0]
126
129
  ```
127
130
 
128
131
  Meter charts support [additional options](#meter-options).
@@ -140,11 +143,33 @@ negative = VerticalBarMeter.new -80,
140
143
 
141
144
  dual = VerticalBarMeter.new 80,
142
145
  width: 20, height: 250, background: '#99f', color: 'blue',
143
- mode: :dual, zero_line: true
146
+ mode: :dual, notches: [0]
144
147
  ```
145
148
 
146
149
  Meter charts support [additional options](#meter-options).
147
150
 
151
+ ### Horizontal Status Leds
152
+
153
+ <img src='examples/horizontal_status_leds.svg' align='right' width=150>
154
+
155
+ ```ruby
156
+ HorizontalStatusLeds.new [1,1,-1,0,1,1,1,1,1,-1,-1,1],
157
+ background: '#ccc'
158
+ ```
159
+
160
+ Led charts support [additional options](#leds-options).
161
+
162
+ ### Vertical Status Leds
163
+
164
+ <img src='examples/vertical_status_leds.svg' align='right' width=25>
165
+
166
+ ```ruby
167
+ VerticalStatusLeds.new [1,1,1,1,-1,1,-1,1,0,1],
168
+ background: '#ccc'
169
+ ```
170
+
171
+ Led charts support [additional options](#leds-options).
172
+
148
173
 
149
174
  ## Configuration
150
175
 
@@ -211,6 +236,10 @@ Line stroke width. This has a different effect in different chart types.
211
236
 
212
237
  CSS Style hash to apply to the entire SVG.
213
238
 
239
+ #### padding
240
+
241
+ Chart padding in pixels.
242
+
214
243
 
215
244
  ### Meter Options
216
245
 
@@ -253,6 +282,23 @@ Thickness of the clipping indicator.
253
282
 
254
283
  Color of the clipping indicator.
255
284
 
285
+ ### Leds Options
286
+
287
+ Leds charts support these options in additon to the basic options (excluding
288
+ the `color` option):
289
+
290
+ #### positive_color
291
+
292
+ Color to use when the value is greater than 0.
293
+
294
+ #### negative_color
295
+
296
+ Color to use when the value is less than 0.
297
+
298
+ #### neutral_color
299
+
300
+ Color to use when the value is 0 or nil.
301
+
256
302
  ## Examples
257
303
 
258
304
  See more examples (code and SVG output) in the [examples folder][1].
@@ -8,5 +8,8 @@ require 'minichart/charts/area_chart'
8
8
  require 'minichart/meters/meter'
9
9
  require 'minichart/meters/vertical_bar_meter'
10
10
  require 'minichart/meters/horizontal_bar_meter'
11
+ require 'minichart/leds/leds'
12
+ require 'minichart/leds/horizontal_status_leds'
13
+ require 'minichart/leds/vertical_status_leds'
11
14
 
12
15
  require 'byebug' if ENV['BYEBUG']
@@ -11,7 +11,8 @@ module Minichart
11
11
  width: 300,
12
12
  stroke: 2,
13
13
  style: {},
14
- color: '#66f'
14
+ color: '#66f',
15
+ padding: 10,
15
16
  }
16
17
  end
17
18
 
@@ -33,7 +34,7 @@ module Minichart
33
34
 
34
35
  super viewBox: viewbox, style: options[:style]
35
36
  element :rect, x: 0, y: 0,
36
- width: options[:width], height: options[:height],
37
+ width: full_width, height: full_height,
37
38
  fill: options[:background], stroke_width: 0
38
39
 
39
40
  clip_path_id = IDGenerator.next
@@ -47,13 +48,21 @@ module Minichart
47
48
  def setup_clip_path(id)
48
49
  element :defs do
49
50
  element :clipPath, id: id do
50
- element :rect, width: options[:width], height: options[:height]
51
+ element :rect, width: full_width, height: full_height
51
52
  end
52
53
  end
53
54
  end
54
55
 
55
56
  def viewbox
56
- "0 0 #{options[:width]} #{options[:height]}"
57
+ "0 0 #{full_width} #{full_height}"
58
+ end
59
+
60
+ def full_height
61
+ options[:height] + options[:padding] * 2
62
+ end
63
+
64
+ def full_width
65
+ options[:width] + options[:padding] * 2
57
66
  end
58
67
 
59
68
  def build
@@ -5,21 +5,25 @@ module Minichart
5
5
  stroke: options[:color],
6
6
  stroke_width: options[:stroke],
7
7
  stroke_linejoin: :round,
8
+ stroke_linecap: :round,
8
9
  points: points
9
10
  end
10
11
 
11
12
  protected
12
13
 
13
14
  def points
14
- result = ["0,#{options[:height]}"]
15
+ first_point = "#{options[:padding]},#{options[:height] + options[:padding]}"
16
+ result = [first_point]
15
17
 
16
18
  inverted_points.each do |point|
17
- x = options[:width] *point[0]
18
- y = options[:height] * point[1]
19
+ x = options[:width] * point[0] + options[:padding]
20
+ y = options[:height] * point[1] + options[:padding]
19
21
  result << "#{x},#{y}"
20
22
  end
21
23
 
22
- result << "#{options[:width]},#{options[:height]}"
24
+ result << "#{options[:width] + options[:padding]},#{options[:height] + options[:padding]}"
25
+ result << first_point
26
+
23
27
 
24
28
  result
25
29
  end
@@ -15,10 +15,11 @@ module Minichart
15
15
  end
16
16
 
17
17
  def bar_options(x, y)
18
- y = y * options[:height]
19
- bar_height = options[:height] - y
18
+ y = y * options[:height] + options[:padding]
19
+
20
+ bar_height = options[:height] - y + options[:padding]
20
21
  {
21
- x: x * options[:width],
22
+ x: x * options[:width] + options[:padding],
22
23
  y: y,
23
24
  width: bar_width,
24
25
  height: bar_height,
@@ -5,6 +5,7 @@ module Minichart
5
5
  stroke: options[:color],
6
6
  stroke_width: options[:stroke],
7
7
  stroke_linejoin: :round,
8
+ stroke_linecap: :round,
8
9
  points: points
9
10
  end
10
11
 
@@ -13,8 +14,8 @@ module Minichart
13
14
  def points
14
15
  result = []
15
16
  inverted_points.each do |point|
16
- x = options[:width] * point[0]
17
- y = options[:height] * point[1]
17
+ x = options[:width] * point[0] + options[:padding]
18
+ y = options[:height] * point[1] + options[:padding]
18
19
  result << "#{x},#{y}"
19
20
  end
20
21
  result
@@ -0,0 +1,43 @@
1
+ module Minichart
2
+ class HorizontalStatusLeds < Leds
3
+ class << self
4
+ def defaults
5
+ leds_defaults.merge width: 300, height: 50
6
+ end
7
+ end
8
+
9
+ def build
10
+ data.each_with_index do |value, i|
11
+ element :rect, bar_options(value, i)
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def bar_width
18
+ @bar_width ||= options[:width] / data.size
19
+ end
20
+
21
+ def bar_options(value, i)
22
+ color = if value == 0 or !value
23
+ :neutral_color
24
+ elsif value > 0
25
+ :positive_color
26
+ else
27
+ :negative_color
28
+ end
29
+
30
+ {
31
+ x: (options[:padding] + i * bar_width),
32
+ y: options[:padding],
33
+ width: bar_width,
34
+ height: options[:height],
35
+ style: {
36
+ fill: options[color],
37
+ stroke_width: options[:stroke],
38
+ stroke: options[:background]
39
+ }
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ module Minichart
2
+ # Base class for led charts
3
+ class Leds < Base
4
+
5
+ class << self
6
+ def leds_defaults
7
+ @meter_defaults ||= {
8
+ positive_color: '#6f6',
9
+ negative_color: '#f66',
10
+ neutral_color: '#eee',
11
+ padding: 2,
12
+ }
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ module Minichart
2
+ class VerticalStatusLeds < Leds
3
+ class << self
4
+ def defaults
5
+ leds_defaults.merge width: 50, height: 300
6
+ end
7
+ end
8
+
9
+ def build
10
+ data.each_with_index do |value, i|
11
+ element :rect, bar_options(value, i)
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def points
18
+ @points ||= data.size
19
+ end
20
+
21
+ def bar_height
22
+ @bar_height ||= options[:height] / points
23
+ end
24
+
25
+ def bar_options(value, i)
26
+ y = options[:padding] + (points - i - 1) * bar_height
27
+
28
+ color = if value == 0 or !value
29
+ :neutral_color
30
+ elsif value > 0
31
+ :positive_color
32
+ else
33
+ :negative_color
34
+ end
35
+
36
+ {
37
+ x: options[:padding],
38
+ y: y,
39
+ width: options[:width],
40
+ height: bar_height,
41
+ style: {
42
+ fill: options[color],
43
+ stroke_width: options[:stroke],
44
+ stroke: options[:background]
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
@@ -19,7 +19,7 @@ module Minichart
19
19
  x2 = x_for clamped_value
20
20
  x = [x1, x2].min
21
21
 
22
- element :rect, x: x, y: 0, height: options[:height],
22
+ element :rect, x: x, y: options[:padding], height: options[:height],
23
23
  width: bar_width, style: style
24
24
  end
25
25
 
@@ -44,7 +44,8 @@ module Minichart
44
44
  def draw_vertical_line(target_value, color:, stroke:)
45
45
  x = x_for target_value
46
46
 
47
- element :line, x1: x, x2: x, y1: 0, y2: options[:height],
47
+ element :line, x1: x, x2: x,
48
+ y1: options[:padding], y2: options[:height] + options[:padding],
48
49
  stroke: color, stroke_width: stroke
49
50
  end
50
51
 
@@ -65,15 +66,15 @@ module Minichart
65
66
  end
66
67
 
67
68
  def x_for(target_value)
68
- result = target_value.abs / options[:max].to_f * options[:width]
69
+ result = target_value.abs / options[:max].to_f * options[:width] + options[:padding]
69
70
 
70
71
  case mode
71
72
  when :positive
72
73
  result
73
74
  when :negative
74
- options[:width] - result
75
+ full_width - result
75
76
  when :dual
76
- target_value / options[:max].to_f * half_width + half_width
77
+ target_value / options[:max].to_f * half_width + half_width + options[:padding]
77
78
  end
78
79
  end
79
80
  end
@@ -5,19 +5,18 @@ module Minichart
5
5
  class << self
6
6
  def meter_defaults
7
7
  @meter_defaults ||= {
8
- height: 50,
9
8
  max: 100,
10
9
  notches: [],
11
- notch_thickness: 10,
10
+ notch_thickness: 4,
12
11
  notch_color: 'black',
13
12
  clipping_indicator: false,
14
- clipping_indicator_thickness: 20,
13
+ clipping_indicator_thickness: 4,
15
14
  clipping_indicator_color: 'yellow',
15
+ padding: 2,
16
16
  }
17
17
  end
18
18
  end
19
19
 
20
-
21
20
  protected
22
21
 
23
22
  def value
@@ -19,7 +19,7 @@ module Minichart
19
19
  y2 = y_for clamped_value
20
20
  y = [y1, y2].min
21
21
 
22
- element :rect, x: 0, y: y, height: bar_height,
22
+ element :rect, x: options[:padding], y: y, height: bar_height,
23
23
  width: options[:width], style: style
24
24
  end
25
25
 
@@ -44,7 +44,8 @@ module Minichart
44
44
  def draw_horizontal_line(target_value, color:, stroke:)
45
45
  y = y_for target_value
46
46
 
47
- element :line, x1: 0, x2: options[:width], y1: y, y2: y,
47
+ element :line, x1: options[:padding], x2: options[:width] + options[:padding],
48
+ y1: y, y2: y,
48
49
  stroke: color, stroke_width: stroke
49
50
  end
50
51
 
@@ -65,15 +66,15 @@ module Minichart
65
66
  end
66
67
 
67
68
  def y_for(target_value)
68
- result = target_value.abs / options[:max].to_f * options[:height]
69
+ result = target_value.abs / options[:max].to_f * options[:height] + options[:padding]
69
70
 
70
71
  case mode
71
72
  when :positive
72
- options[:height] - result
73
+ full_height - result
73
74
  when :negative
74
75
  result
75
76
  when :dual
76
- options[:height] - (target_value / options[:max].to_f * half_height + half_height)
77
+ options[:height] - (target_value / options[:max].to_f * half_height + half_height) + options[:padding]
77
78
  end
78
79
  end
79
80
  end
@@ -1,3 +1,3 @@
1
1
  module Minichart
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minichart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-30 00:00:00.000000000 Z
11
+ date: 2020-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: victor
@@ -38,6 +38,9 @@ files:
38
38
  - lib/minichart/charts/chart.rb
39
39
  - lib/minichart/charts/line_chart.rb
40
40
  - lib/minichart/id_generator.rb
41
+ - lib/minichart/leds/horizontal_status_leds.rb
42
+ - lib/minichart/leds/leds.rb
43
+ - lib/minichart/leds/vertical_status_leds.rb
41
44
  - lib/minichart/meters/horizontal_bar_meter.rb
42
45
  - lib/minichart/meters/meter.rb
43
46
  - lib/minichart/meters/vertical_bar_meter.rb