chart_bibz 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6df699587ec922c251c5c46d1a9b07dfec59e7af8f555df7b9e89543bd5f62ae
4
+ data.tar.gz: 1aec799a23b9c11c098a4e0b0cbd2f7fd3eec856828bce253a8b276ed0a715e4
5
+ SHA512:
6
+ metadata.gz: c4e53911af3059f3afbcb0d57212a8526734872b50025d05cc30b9522882f6414f6c60907b9e69bc318dc3d770fa7c5e67efc22e814948c2bc8fec4a6a6ee7d7
7
+ data.tar.gz: 97078b7ebe824c85587fcc9ef96b0392be653b09fe0a79b19107186afc1c1b109d03195d820bd8e4d517e8253849a82daed55da1b87133c5b85592eabd99a576
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Thomas HUMMEL
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ ![Chart Bibz logo](https://raw.githubusercontent.com/thooams/chart_bibz/main/chart-bibz-logo.gif)
2
+
3
+ # Chart Bibz
4
+ Use Chartjs with ruby. Generate your chart in one ruby line.
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/chart_bibz.svg)](https://badge.fury.io/rb/chart_bibz)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/7e8e319e9f7197593733/maintainability)](https://codeclimate.com/github/thooams/chart_bibz/maintainability)
8
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/7e8e319e9f7197593733/test_coverage)](https://codeclimate.com/github/thooams/chart_bibz/test_coverage)
9
+ [![security](https://hakiri.io/github/thooams/chart_bibz/main.svg)](https://hakiri.io/github/thooams/chart_bibz/main)
10
+ [![Ci](https://github.com/thooams/chart_bibz/workflows/CI/badge.svg)](https://github.com/thooams/chart_bibz/actions)
11
+ [![Linter](https://github.com/thooams/chart_bibz/workflows/Linter/badge.svg)](https://github.com/thooams/chart_bibz/actions)
12
+ [![Inline docs](http://inch-ci.org/github/thooams/chart_bibz.svg?branch=master)](http://inch-ci.org/github/thooams/chart_bibz)
13
+
14
+
15
+ ## Usage
16
+ How to use the plugin.
17
+
18
+ ```ruby
19
+ # @param data [Hash] Data
20
+ # @param options [Hash] The chart type and the chartjs options
21
+ # @param html_options [Hash] The canvas html options
22
+ # @return [String] The Canvas Html
23
+ chart data, options, html_options
24
+ ```
25
+
26
+ ## Example
27
+ ```ruby
28
+ data = {
29
+ datasets: [{
30
+ barPercentage: 0.5,
31
+ barThickness: 6,
32
+ maxBarThickness: 8,
33
+ minBarLength: 2,
34
+ data: [10, 20, 30, 40, 50, 60, 70]
35
+ }]
36
+ }
37
+ options = { type: :bar, scales: { yAxes: [{ ticks: { beginAtZero: true } }] } }
38
+
39
+ chart data
40
+ # or
41
+ chart data, options, { id: "my-chart", width: 100, height: 520 }
42
+ ```
43
+
44
+ ## Installation
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'chart_bibz'
49
+ ```
50
+
51
+ And then execute:
52
+ ```bash
53
+ $ bundle
54
+ $ yarn add chart.js
55
+ ```
56
+
57
+ Or install it yourself as:
58
+ ```bash
59
+ $ gem install chart_bibz
60
+ ```
61
+
62
+ Add this line in your js file, by example app/javascript/packs/application.js
63
+ ```js
64
+ ...
65
+ document.querySelectorAll(".chart-bibz").forEach(function(canvasEl){
66
+ let type = canvasEl.getAttribute("data-cb-type")
67
+ let options = JSON.parse(canvasEl.getAttribute("data-cb-options"))
68
+ let data = JSON.parse(canvasEl.getAttribute("data-cb-data"))
69
+ let ctx = canvasEl.getContext('2d')
70
+
71
+ new Chart(ctx, { type: type, data: data, options: options })
72
+ })
73
+ ...
74
+ ```
75
+
76
+ That's it !
77
+
78
+ ## License
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'ChartBibz'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'chart_bibz/railtie'
4
+
5
+ module ChartBibz
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :ViewComponents
9
+ autoload :Helpers
10
+ end
11
+
12
+ require 'chart_bibz/rails/engine'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module Helpers
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :ChartHelper
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module Helpers
5
+ module ChartHelper # rubocop:disable Style/Documentation
6
+ def chart(data = {}, options = {}, html_options = {})
7
+ ChartBibz::ViewComponents::ChartViewComponent.new(data, options, html_options).render
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module Rails
5
+ class Engine < ::Rails::Engine # rubocop:disable Style/Documentation
6
+ initializer 'chart_bibz.helpers' do
7
+ ActionView::Base.include ChartBibz::Helpers::ChartHelper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module ViewComponents
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :ApplicationViewComponent
8
+ autoload :CanvasViewComponent
9
+ autoload :ChartViewComponent
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module ViewComponents
5
+ class ApplicationViewComponent # rubocop:disable Style/Documentation
6
+ include ActionView::Helpers::TagHelper
7
+ include ActionView::Helpers::CaptureHelper
8
+ include ActionView::Helpers::TextHelper
9
+ include ActionView::Helpers::TranslationHelper
10
+ attr_accessor :output_buffer
11
+
12
+ protected
13
+
14
+ def join_classes(*classes)
15
+ klasses = Array(classes).flatten.map(&:to_s).compact.uniq.reject(&:blank?)
16
+ klasses.empty? ? nil : klasses
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module ViewComponents
5
+ # Generate the canvas view through the render method
6
+ class CanvasViewComponent < ApplicationViewComponent
7
+ # Constants
8
+ WIDTH = 400
9
+ HEIGHT = 400
10
+
11
+ # @param args [Hash] The html options
12
+ # @return [void]
13
+ def initialize(args = {})
14
+ @args = args
15
+ end
16
+
17
+ # Generate the html canvas
18
+ # @return [String] The html canvas
19
+ def render
20
+ tag.canvas(**html_options)
21
+ end
22
+
23
+ # @return [String] The canvas html id
24
+ def id
25
+ html_options[:id]
26
+ end
27
+
28
+ private
29
+
30
+ # @return [String] The canvas html attributes
31
+ def html_options
32
+ @html_options ||= base_html_options.merge(@args).merge(class: join_classes('chart-bibz', @args[:class]))
33
+ end
34
+
35
+ def base_html_options
36
+ { id: "chart-#{Random.uuid}", width: WIDTH, height: HEIGHT, role: 'img' }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChartBibz
4
+ module ViewComponents
5
+ # Generate the chart view through the render method
6
+ class ChartViewComponent < ApplicationViewComponent
7
+ # @see CanvasViewComponent#id
8
+ delegate :id, to: :canvas
9
+
10
+ # @param data [Hash] the chartjs data
11
+ # @param options [Hash] the chartjs options
12
+ # @option options [Symbol] :type The chart type [:bar, :line, ...]
13
+ #
14
+ # @param html_options [Hash] the html_options of the canvas
15
+ # @option html_options [String] :id The id of the canvas
16
+ # @option html_options [String] :class The class of the canvas
17
+ #
18
+ # @return [void]
19
+ def initialize(data = {}, options = {}, html_options = {})
20
+ @data = data
21
+ @options = options
22
+ @html_options = html_options
23
+ end
24
+
25
+ # @return [String] The canvas html
26
+ delegate :render, to: :canvas
27
+
28
+ # @see CanvasViewComponent
29
+ # @return [Object] The canvas object
30
+ def canvas
31
+ @canvas ||= CanvasViewComponent.new(new_html_options)
32
+ end
33
+
34
+ private
35
+
36
+ # @return [Hash] The new html options with the data attributes
37
+ def new_html_options
38
+ @html_options['data-cb-data'] = @data.to_json
39
+ @html_options['data-cb-type'] = @options.delete(:type) || :bar
40
+ @html_options['data-cb-options'] = @options.to_json
41
+ @html_options['aria-label'] = aria_label
42
+ @html_options
43
+ end
44
+
45
+ def aria_label
46
+ @options[:title].nil? ? 'Chart' : "Chart of #{@options[:title]}"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :chart_bibz do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chart_bibz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas HUMMEL
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: overcommit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.3
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 6.0.3.4
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 6.0.3
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 6.0.3.4
47
+ - !ruby/object:Gem::Dependency
48
+ name: rubocop
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop-ast
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop-minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop-performance
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rails
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: simplecov
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: steep
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ description: ChartJs with ruby.
146
+ email:
147
+ - thomas@hummel.link
148
+ executables: []
149
+ extensions: []
150
+ extra_rdoc_files: []
151
+ files:
152
+ - MIT-LICENSE
153
+ - README.md
154
+ - Rakefile
155
+ - lib/chart_bibz.rb
156
+ - lib/chart_bibz/helpers.rb
157
+ - lib/chart_bibz/helpers/chart_helper.rb
158
+ - lib/chart_bibz/rails/engine.rb
159
+ - lib/chart_bibz/railtie.rb
160
+ - lib/chart_bibz/version.rb
161
+ - lib/chart_bibz/view_components.rb
162
+ - lib/chart_bibz/view_components/application_view_component.rb
163
+ - lib/chart_bibz/view_components/canvas_view_component.rb
164
+ - lib/chart_bibz/view_components/chart_view_component.rb
165
+ - lib/tasks/chart_bibz_tasks.rake
166
+ homepage: https://github.com/thooams/chart_bibz
167
+ licenses:
168
+ - MIT
169
+ metadata: {}
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: 3.0.0
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubygems_version: 3.2.3
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: ChartJs with ruby.
189
+ test_files: []