minichart 0.1.1 → 0.2.0

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: c4d765607cf018435ce99cafa73e90043dffdb7ab2bc9916510fc17a7a8a62b5
4
- data.tar.gz: 4c523073e91db45609206898fd17dde2d8b968578bdbcf6ad0c82426d4611406
3
+ metadata.gz: 767c1bdbf55e5aedb689c5286535fb84d3c48cf2c1c054b01da97237dee3437a
4
+ data.tar.gz: ea0b462ccfd166eea1df2e2a4ad08f32060c713a06f1aea36170079bc90a3671
5
5
  SHA512:
6
- metadata.gz: 398cb0c87ffe4348868975c32049c306c3e529d03a0215da786e90af24c76df9c158dabf57435ed71db9f66638c1f5caaf4f221e0f4fc7f35958109011d43575
7
- data.tar.gz: 4d5592dd32f5350556859aff90cd82d0858ed2a7564cf68371e64b7d2cecc347af137ea8163bc7b7677b0567dcff1f042c33ac499afe106fba172ba38002cb9b
6
+ metadata.gz: 57524a378c6ce4df0ad08d4edc134630d3ae6ebbb35e68638f11926ab61f4c3b3f6e8e0861d49213de54d619d52df2cb74c4813e2349409e9e444b0ddca2cb44
7
+ data.tar.gz: 45820a40054e716bfe412d661ba814ea4fb8589b39145fbeff6a079841ea83990d2adbaf6733c328378627133eaaa5891f5e87d268f66abab69a412727cbc29a
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
- Minichart - SVG Chart Generator
2
- ==================================================
1
+ # Minichart - SVG Chart Generator
3
2
 
4
3
  ---
5
4
 
6
5
  Create SVG mini charts with Ruby
7
6
 
7
+ ![demo](examples/multiple.svg)
8
+
8
9
  ---
9
10
 
10
- Install
11
- --------------------------------------------------
11
+ ## Install
12
12
 
13
- ```
13
+ ```shell
14
14
  $ gem install minichart
15
15
  ```
16
16
 
@@ -20,20 +20,71 @@ Or with bundler:
20
20
  gem 'minichart'
21
21
  ```
22
22
 
23
- Example
24
- --------------------------------------------------
23
+ ## Usage
24
+
25
+ Require and optionally include the library:
25
26
 
26
27
  ```ruby
27
28
  require 'minichart'
28
29
  include Minichart
30
+ ```
31
+
32
+ Initialize a chart with data, and optional options:
33
+
34
+ ```ruby
35
+ data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]
36
+ line = LineChart.new data, color: 'blue'
37
+ bars = BarChart.new data, color: 'blue'
38
+ area = AreaChart.new data, color: 'blue'
39
+ ```
40
+
41
+ Get the full SVG output by calling `#render`:
42
+
43
+ ```ruby
44
+ puts area.render
45
+ #=> <?xml version="1.0" standalone="no"?>
46
+ # <svg> ... </svg>
47
+ ```
48
+
49
+ Save it to file, by calling `#save`:
50
+
51
+ ```ruby
52
+ area.save "my-chart.svg"
53
+ ```
54
+
55
+ Get its inner SVG string by calling `#to_s`:
56
+
57
+ ```ruby
58
+ puts area.to_s
59
+ #=> <polyline fill="blue" stroke="blue" stroke-width="2" points="..."/>
60
+ ```
61
+
29
62
 
30
- plot = LineChart.new
31
- plot.aspect_ratio = 2
32
- plot.data = [10, 30, 20, 40, 30]
33
- plot.save 'line'
63
+ The objects returned from all the mini chart classes are [Victor::SVG][2] objects, so they support all methods supported by it as well.
64
+
65
+ ## Options
66
+
67
+ All minichart classes support a second hash argument for options
68
+
69
+ ```ruby
70
+ chart = LineChart.new data, options
34
71
  ```
35
72
 
73
+ | Option | Default | Description |
74
+ | -------------- | ---------------------------- | ----------------------- |
75
+ | `background` | *None* | Chart background color |
76
+ | `color` | #333 | Chart color |
77
+ | `aspect_ratio` | 3 | Set automatic width |
78
+ | `height` | 100 | Chart height in pixels |
79
+ | `width` | Calculated by `aspect_ratio` | Chart width in pixels |
80
+ | `stroke` | 2 | Line stroke width |
81
+ | `style` | *None* | Style hash for the SVG |
82
+
83
+
84
+ ## Examples
85
+
36
86
  See more examples (code and SVG output) in the [examples folder][1].
37
87
 
38
88
 
39
89
  [1]: https://github.com/DannyBen/minichart/tree/master/examples#examples
90
+ [2]: https://github.com/DannyBen/victor
@@ -3,3 +3,5 @@ require 'minichart/chart'
3
3
  require 'minichart/line_chart'
4
4
  require 'minichart/bar_chart'
5
5
  require 'minichart/area_chart'
6
+
7
+ require 'byebug' if ENV['BYEBUG']
@@ -1,7 +1,7 @@
1
1
  module Minichart
2
2
  class AreaChart < Chart
3
3
  def build
4
- svg.polyline fill: color,
4
+ polyline fill: color,
5
5
  stroke: color,
6
6
  stroke_width: stroke,
7
7
  stroke_linejoin: :round,
@@ -4,7 +4,7 @@ module Minichart
4
4
  opts = { x_point_count: data.size }
5
5
 
6
6
  inverted_points(opts).each do |x, y|
7
- svg.rect bar_options x, y
7
+ rect bar_options x, y
8
8
  end
9
9
  end
10
10
 
@@ -1,21 +1,32 @@
1
1
  module Minichart
2
- class Chart
3
- attr_accessor :data, :aspect_ratio, :opts
2
+ class Chart < Victor::SVG
3
+ attr_reader :aspect_ratio, :background, :color, :data,
4
+ :height, :stroke, :style, :width
5
+
6
+ def initialize(data, opts = {})
7
+ @data = data
8
+ @background = opts[:background]
9
+ @aspect_ratio = opts[:aspect_ratio] || 3
10
+ @height = opts[:height] || 100
11
+ @width = opts[:width] || (aspect_ratio * height).round
12
+ @stroke = opts[:stroke] || 2
13
+ @style = opts[:style] || {}
14
+ @color = opts[:color] || '#333'
15
+
16
+ super height: height, width: width, style: style,
17
+ viewBox: "0 0 #{width} #{height}"
18
+
19
+ rect x: 0, y: 0, width: width, height: height, fill: background if background
4
20
 
5
- def initialize(opts={})
6
- @opts = opts
7
- end
8
-
9
- def save(name)
10
- svg.rect x: 0, y: 0, width: width, height: height, fill: background
11
21
  build
12
- svg.save name
13
22
  end
14
23
 
15
24
  def build
16
25
  raise NotImplementedError, "#build is not implemented"
17
26
  end
18
27
 
28
+ protected
29
+
19
30
  def inverted_points(opts={})
20
31
  normalized_points(opts).map { |point| [point[0], 1-point[1]] }
21
32
  end
@@ -35,33 +46,5 @@ module Minichart
35
46
 
36
47
  result
37
48
  end
38
-
39
- def width
40
- @width ||= (aspect_ratio * height).round
41
- end
42
-
43
- def height
44
- 100
45
- end
46
-
47
- def svg
48
- @svg ||= Victor::SVG.new viewBox: "0 0 #{width} #{height}", style: style
49
- end
50
-
51
- def style
52
- @opts[:style] ||= {}
53
- end
54
-
55
- def background
56
- @opts[:background] ||= '#eee'
57
- end
58
-
59
- def color
60
- @opts[:color] || '#333'
61
- end
62
-
63
- def stroke
64
- @opts[:stroke] ||= 2
65
- end
66
49
  end
67
50
  end
@@ -1,7 +1,7 @@
1
1
  module Minichart
2
2
  class LineChart < Chart
3
3
  def build
4
- svg.polyline fill: :none,
4
+ polyline fill: :none,
5
5
  stroke: color,
6
6
  stroke_width: stroke,
7
7
  stroke_linejoin: :round,
@@ -1,3 +1,3 @@
1
1
  module Minichart
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
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.1.1
4
+ version: 0.2.0
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: 2019-05-25 00:00:00.000000000 Z
11
+ date: 2020-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: victor
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '0.3'
27
27
  description: Generate mini charts with SVG
28
28
  email: db@dannyben.com
29
29
  executables: []
@@ -49,14 +49,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 2.0.0
52
+ version: 2.4.0
53
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ">="
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 3.0.3
59
+ rubygems_version: 3.1.2
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: SVG Mini Charts