minichart 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -19
- data/lib/minichart.rb +1 -0
- data/lib/minichart/base.rb +4 -2
- data/lib/minichart/meters/horizontal_bar_meter.rb +15 -42
- data/lib/minichart/meters/meter.rb +65 -0
- data/lib/minichart/meters/vertical_bar_meter.rb +72 -0
- data/lib/minichart/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7debe086fa2ba7994f1362fa53ab2d5da5cc61a0010f6f08d6ca125cc447bc27
|
4
|
+
data.tar.gz: 77dd9cf342830795872c284befe937cba7caa9c2372bdae2b8a42a44e6d4bb13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a331fa46640d9bfb32759a5aa160680cd684bd7f57f194e212f7b0d0f5c2af61f03f8b526702de0421dbe608fbc70c39df8c219967b8c2de9fe647f04822e42
|
7
|
+
data.tar.gz: 2fede85eef059f2ac6980ff9ba6249b1e7b716edb38eacfe514cebace11488e0cb1ca4520ff4d7a3bf58934e746e846130026c9c98f7bc41419c4216a68c8734
|
data/README.md
CHANGED
@@ -64,7 +64,7 @@ The objects returned from all the mini chart classes are [Victor::SVG][2] object
|
|
64
64
|
|
65
65
|
### Line Chart
|
66
66
|
|
67
|
-
<img src='examples/line_chart.svg' align='right'>
|
67
|
+
<img src='examples/line_chart.svg' align='right' width=300>
|
68
68
|
|
69
69
|
```ruby
|
70
70
|
LineChart.new data, height: 50, background: '#eee',
|
@@ -73,7 +73,7 @@ LineChart.new data, height: 50, background: '#eee',
|
|
73
73
|
|
74
74
|
### Bar Chart
|
75
75
|
|
76
|
-
<img src='examples/bar_chart.svg' align='right'>
|
76
|
+
<img src='examples/bar_chart.svg' align='right' width=300>
|
77
77
|
|
78
78
|
```ruby
|
79
79
|
BarChart.new data, height: 50, background: '#eee',
|
@@ -82,7 +82,7 @@ BarChart.new data, height: 50, background: '#eee',
|
|
82
82
|
|
83
83
|
### Area Chart
|
84
84
|
|
85
|
-
<img src='examples/area_chart.svg' align='right'>
|
85
|
+
<img src='examples/area_chart.svg' align='right' width=300>
|
86
86
|
|
87
87
|
```ruby
|
88
88
|
AreaChart.new data, height: 50, background: '#eee',
|
@@ -107,6 +107,25 @@ dual = HorizontalBarMeter.new 80,
|
|
107
107
|
|
108
108
|
Meter charts support [additional options](#meter-options).
|
109
109
|
|
110
|
+
### Vertical Bar Meter
|
111
|
+
|
112
|
+
<img src='examples/multiple_vertical_bars.svg' align='right'>
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
positive = VerticalBarMeter.new 70,
|
116
|
+
width: 20, height: 250, background: '#9f9', color: 'green'
|
117
|
+
|
118
|
+
negative = VerticalBarMeter.new -80,
|
119
|
+
width: 20, height: 250, background: '#f99', color: 'red'
|
120
|
+
|
121
|
+
dual = VerticalBarMeter.new 80,
|
122
|
+
width: 20, height: 250, background: '#99f', color: 'blue',
|
123
|
+
mode: :dual, zero_line: true
|
124
|
+
```
|
125
|
+
|
126
|
+
Meter charts support [additional options](#meter-options).
|
127
|
+
|
128
|
+
|
110
129
|
## Options
|
111
130
|
|
112
131
|
### Basic Options
|
@@ -117,28 +136,32 @@ All chart classes support a second hash argument for options
|
|
117
136
|
chart = LineChart.new data, options
|
118
137
|
```
|
119
138
|
|
120
|
-
| Option | Default
|
121
|
-
| -------------- |
|
122
|
-
| `background` | `"white"`
|
123
|
-
| `color` | `"#333"`
|
124
|
-
| `aspect_ratio` | `3`
|
125
|
-
| `height` | `100
|
126
|
-
| `width` | Calculated by `aspect_ratio
|
127
|
-
| `stroke` | `2`
|
128
|
-
| `style` | *None*
|
139
|
+
| Option | Default | Description |
|
140
|
+
| -------------- | ----------------------------- | ------------------------------------------------------------ |
|
141
|
+
| `background` | `"white"` | Chart background color |
|
142
|
+
| `color` | `"#333"` | Chart color |
|
143
|
+
| `aspect_ratio` | `3` | Set automatic width |
|
144
|
+
| `height` | `100`<sup>*</sup> | Chart height in pixels |
|
145
|
+
| `width` | Calculated by `aspect_ratio`<sup>*</sup> | Chart width in pixels |
|
146
|
+
| `stroke` | `2` | Line stroke width.<br />In `BarChart` determines the gap between bars |
|
147
|
+
| `style` | *None* | Style hash for the SVG |
|
129
148
|
|
149
|
+
_* Default dimensions are reversed for horizontal meters - `width` defaults to 100, and `height` is calculated automatically._
|
130
150
|
|
131
151
|
### Meter Options
|
132
152
|
|
133
153
|
Meter charts support these options in additon to the basic options:
|
134
154
|
|
135
|
-
| Option
|
136
|
-
|
|
137
|
-
| `mode`
|
138
|
-
| `max`
|
139
|
-
| `zero_line`
|
140
|
-
| `
|
141
|
-
| `zero_line_color`
|
155
|
+
| Option | Default | Description |
|
156
|
+
| -------------------------- | --------- | ------------------------------------------------------------ |
|
157
|
+
| `mode` | `:auto` | Display mode.<br />Can be `:positive`, `:negative`, `:auto` or `:dual` |
|
158
|
+
| `max` | `100` | The absolute maximum value.<br />This number should be positive even for negative charts |
|
159
|
+
| `zero_line` | `false` | If true, mark the zero line |
|
160
|
+
| `zero_line_size` | `6` | Size of the zero line |
|
161
|
+
| `zero_line_color` | `"black"` | Color of the zero line |
|
162
|
+
| `clipping_indicator` | `false` | If true, show when the value exceeds the range |
|
163
|
+
| `clipping_indicator_size` | `6` | Size of the clipping indicator |
|
164
|
+
| `clipping_indicator_color` | `"yelow"` | Color of the clipping indicator |
|
142
165
|
|
143
166
|
## Examples
|
144
167
|
|
data/lib/minichart.rb
CHANGED
@@ -6,6 +6,7 @@ require 'minichart/charts/line_chart'
|
|
6
6
|
require 'minichart/charts/bar_chart'
|
7
7
|
require 'minichart/charts/area_chart'
|
8
8
|
require 'minichart/meters/meter'
|
9
|
+
require 'minichart/meters/vertical_bar_meter'
|
9
10
|
require 'minichart/meters/horizontal_bar_meter'
|
10
11
|
|
11
12
|
require 'byebug' if ENV['BYEBUG']
|
data/lib/minichart/base.rb
CHANGED
@@ -7,8 +7,10 @@ module Minichart
|
|
7
7
|
def initialize(data, opts = {})
|
8
8
|
@data, @opts = data, opts
|
9
9
|
|
10
|
-
super height: height, width: width, style: style, viewBox: viewbox
|
11
|
-
|
10
|
+
# super height: height, width: width, style: style, viewBox: viewbox
|
11
|
+
super viewBox: viewbox, style: style
|
12
|
+
element :rect, x: 0, y: 0, width: width, height: height,
|
13
|
+
fill: background, stroke_width: 0
|
12
14
|
|
13
15
|
clip_path_id = IDGenerator.next
|
14
16
|
setup_clip_path clip_path_id
|
@@ -3,6 +3,7 @@ module Minichart
|
|
3
3
|
def build
|
4
4
|
draw_bar
|
5
5
|
draw_zero_line if zero_line
|
6
|
+
draw_clipping_indicator if clipping_indicator and clipping?
|
6
7
|
end
|
7
8
|
|
8
9
|
protected
|
@@ -20,62 +21,38 @@ module Minichart
|
|
20
21
|
element :rect, x: x, y: 0, height: height, width: bar_width, style: style
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
x = if mode == :
|
25
|
-
width-
|
26
|
-
elsif mode == :dual
|
27
|
-
width / 2 - zero_line_stroke / 2
|
24
|
+
def draw_clipping_indicator
|
25
|
+
x = if mode == :positive or (mode == :dual and value > 0)
|
26
|
+
width - clipping_indicator_size
|
28
27
|
else
|
29
28
|
0
|
30
29
|
end
|
31
30
|
|
32
31
|
element :rect, x: x, y: 0,
|
33
|
-
height: height, width:
|
34
|
-
fill:
|
32
|
+
height: height, width: clipping_indicator_size,
|
33
|
+
fill: clipping_indicator_color, stroke_width: 0
|
35
34
|
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
def draw_zero_line
|
38
|
+
x = if mode == :negative
|
39
|
+
width - zero_line_size
|
40
|
+
elsif mode == :dual
|
41
|
+
width / 2 - zero_line_size / 2
|
43
42
|
else
|
44
|
-
|
43
|
+
0
|
45
44
|
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def max
|
49
|
-
opts[:max] ||= 100
|
50
|
-
end
|
51
|
-
|
52
|
-
def zero_line
|
53
|
-
opts[:zero_line]
|
54
|
-
end
|
55
45
|
|
56
|
-
|
57
|
-
|
58
|
-
|
46
|
+
element :rect, x: x, y: 0,
|
47
|
+
height: height, width: zero_line_size,
|
48
|
+
fill: zero_line_color, stroke_width: 0
|
59
49
|
|
60
|
-
def zero_line_color
|
61
|
-
opts[:zero_line_color] || 'black'
|
62
50
|
end
|
63
51
|
|
64
52
|
def width_factor
|
65
53
|
width / max.to_f
|
66
54
|
end
|
67
55
|
|
68
|
-
def clamped_value
|
69
|
-
case mode
|
70
|
-
when :positive
|
71
|
-
value.clamp 0, max
|
72
|
-
when :negative
|
73
|
-
value.clamp -max, 0
|
74
|
-
when :dual
|
75
|
-
value.clamp -max, max
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
56
|
def bar_width
|
80
57
|
if mode == :dual
|
81
58
|
clamped_value.abs * width_factor * 0.5
|
@@ -83,9 +60,5 @@ module Minichart
|
|
83
60
|
clamped_value.abs * width_factor
|
84
61
|
end
|
85
62
|
end
|
86
|
-
|
87
|
-
def style
|
88
|
-
{ fill: color, stroke_width: stroke, stroke: background }
|
89
|
-
end
|
90
63
|
end
|
91
64
|
end
|
@@ -1,8 +1,73 @@
|
|
1
1
|
module Minichart
|
2
2
|
# Base class for charts with a single value
|
3
3
|
class Meter < Base
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
4
7
|
def value
|
5
8
|
data
|
6
9
|
end
|
10
|
+
|
11
|
+
def clipping?
|
12
|
+
value > max || value < -max
|
13
|
+
end
|
14
|
+
|
15
|
+
def mode
|
16
|
+
@mode ||= mode!
|
17
|
+
end
|
18
|
+
|
19
|
+
def mode!
|
20
|
+
opts[:mode] ||= :auto
|
21
|
+
|
22
|
+
if opts[:mode] == :auto
|
23
|
+
value >= 0 ? :positive : :negative
|
24
|
+
else
|
25
|
+
opts[:mode].to_sym
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def max
|
30
|
+
opts[:max] ||= 100
|
31
|
+
end
|
32
|
+
|
33
|
+
def zero_line
|
34
|
+
opts[:zero_line]
|
35
|
+
end
|
36
|
+
|
37
|
+
def zero_line_size
|
38
|
+
opts[:zero_line_size] ||= 6
|
39
|
+
end
|
40
|
+
|
41
|
+
def zero_line_color
|
42
|
+
opts[:zero_line_color] ||= 'black'
|
43
|
+
end
|
44
|
+
|
45
|
+
def clipping_indicator
|
46
|
+
opts[:clipping_indicator]
|
47
|
+
end
|
48
|
+
|
49
|
+
def clipping_indicator_size
|
50
|
+
opts[:clipping_indicator_size] ||= 6
|
51
|
+
end
|
52
|
+
|
53
|
+
def clipping_indicator_color
|
54
|
+
opts[:clipping_indicator_color] ||= 'yellow'
|
55
|
+
end
|
56
|
+
|
57
|
+
def clamped_value
|
58
|
+
case mode
|
59
|
+
when :positive
|
60
|
+
value.clamp 0, max
|
61
|
+
when :negative
|
62
|
+
value.clamp -max, 0
|
63
|
+
when :dual
|
64
|
+
value.clamp -max, max
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def style
|
69
|
+
{ fill: color, stroke_width: stroke, stroke: background }
|
70
|
+
end
|
71
|
+
|
7
72
|
end
|
8
73
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Minichart
|
2
|
+
class VerticalBarMeter < Meter
|
3
|
+
def build
|
4
|
+
draw_bar
|
5
|
+
draw_zero_line if zero_line
|
6
|
+
draw_clipping_indicator if clipping_indicator and clipping?
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def width
|
12
|
+
opts[:width] ||= 100
|
13
|
+
end
|
14
|
+
|
15
|
+
def height
|
16
|
+
opts[:height] ||= (aspect_ratio * width).round
|
17
|
+
end
|
18
|
+
|
19
|
+
def draw_bar
|
20
|
+
y = if mode == :positive
|
21
|
+
height - bar_height
|
22
|
+
elsif mode == :dual
|
23
|
+
middle = height * 0.5
|
24
|
+
value >= 0 ? middle - bar_height : middle
|
25
|
+
else
|
26
|
+
0
|
27
|
+
end
|
28
|
+
|
29
|
+
element :rect, x: 0, y: y, height: bar_height, width: width, style: style
|
30
|
+
end
|
31
|
+
|
32
|
+
def draw_clipping_indicator
|
33
|
+
y = if mode == :negative or (mode == :dual and value < 0)
|
34
|
+
height - clipping_indicator_size
|
35
|
+
else
|
36
|
+
0
|
37
|
+
end
|
38
|
+
|
39
|
+
element :rect, x: 0, y: y,
|
40
|
+
height: clipping_indicator_size, width: width,
|
41
|
+
fill: clipping_indicator_color, stroke_width: 0
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def draw_zero_line
|
46
|
+
y = if mode == :positive
|
47
|
+
height - zero_line_size
|
48
|
+
elsif mode == :dual
|
49
|
+
height / 2 - zero_line_size / 2
|
50
|
+
else
|
51
|
+
0
|
52
|
+
end
|
53
|
+
|
54
|
+
element :rect, x: 0, y: y,
|
55
|
+
height: zero_line_size, width: width,
|
56
|
+
fill: zero_line_color, stroke_width: 0
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def height_factor
|
61
|
+
height / max.to_f
|
62
|
+
end
|
63
|
+
|
64
|
+
def bar_height
|
65
|
+
if mode == :dual
|
66
|
+
clamped_value.abs * height_factor * 0.5
|
67
|
+
else
|
68
|
+
clamped_value.abs * height_factor
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/minichart/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.2
|
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-
|
11
|
+
date: 2020-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: victor
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/minichart/id_generator.rb
|
41
41
|
- lib/minichart/meters/horizontal_bar_meter.rb
|
42
42
|
- lib/minichart/meters/meter.rb
|
43
|
+
- lib/minichart/meters/vertical_bar_meter.rb
|
43
44
|
- lib/minichart/version.rb
|
44
45
|
homepage: https://github.com/DannyBen/minichart
|
45
46
|
licenses:
|
@@ -53,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
54
|
requirements:
|
54
55
|
- - ">="
|
55
56
|
- !ruby/object:Gem::Version
|
56
|
-
version: 2.
|
57
|
+
version: 2.5.0
|
57
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
59
|
requirements:
|
59
60
|
- - ">="
|