chartism 0.1.0 → 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
  SHA1:
3
- metadata.gz: 536c7f984b360c963808c663c10ea669984bed85
4
- data.tar.gz: cd3c6c3d271c11f66404bbb073b5673f70d314b3
3
+ metadata.gz: a60a2d7e2d316b903c4772e40a7b37c790917483
4
+ data.tar.gz: 441edfba105c7a98fff5e1a923dc618782074abe
5
5
  SHA512:
6
- metadata.gz: afe5a49db3b51643a4e2b4329dc843f1218792c5c08e81d24a0400f958f3bae40801d5e21368fc178103ff653f29a703696f30fccb6c4fc0fa3bfab2a1d32b62
7
- data.tar.gz: 3ef2573abacd27712b0fd1e3479784963f1369e57f6a4d8c808b4828af9d25f93960c66e5281a933f9a55efdec3e0afe55e11a62d669d5e0eee7002735af53be
6
+ metadata.gz: 3fa06be843119b912fc97f95f80870378349bb45b0d5dc583194989fbe75e28816dc90d8e483658baa3317fbdea6d05f30193fbd7919dbee5e90f9b6a487db5e
7
+ data.tar.gz: 5e2bec67d895d6a102a4f63e7b13a011bc219581064b79913ef62edaa5fcdfaa98d5748285eaa1de98abacfa76d993305d11a4d128933b32afd90f8790647bbd
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Chartism
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/chartism`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Dependency Status](https://gemnasium.com/Darksecond/chartism.svg)](https://gemnasium.com/Darksecond/chartism)
4
+ [![Gem Version](https://badge.fury.io/rb/chartism.svg)](http://badge.fury.io/rb/chartism)
5
+ [![Code Climate](https://codeclimate.com/github/Darksecond/chartism/badges/gpa.svg)](https://codeclimate.com/github/Darksecond/chartism)
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ This library is designed for making charts with ease. It provides a clean DSL to describe charts.
8
+ It is base on the great chartist.js library.
6
9
 
7
10
  ## Installation
8
11
 
@@ -34,7 +37,46 @@ And add to your `application.css`
34
37
 
35
38
  ## Usage
36
39
 
37
- TODO: Write usage instructions here
40
+ An example Linechart Model
41
+
42
+ ```ruby
43
+ class ExampleLineChart
44
+ include Chartism::Line
45
+
46
+ def initialize
47
+ @area = true
48
+ end
49
+
50
+ options do
51
+ points false
52
+ smooth false
53
+
54
+ area do
55
+ @area
56
+ end
57
+ end
58
+
59
+ labels do
60
+ %w(Mon Tue Wed Thu Fri)
61
+ end
62
+
63
+ series do
64
+ [5, 2, 4, 2, 0]
65
+ end
66
+
67
+ series do
68
+ [6, 3, 1, 2, 4]
69
+ end
70
+ end
71
+ ```
72
+
73
+ Then use the following in a view:
74
+
75
+ ```erb
76
+ <%= chart ExampleLineChart.new, class: ['ct-perfect-fourth'] %>
77
+ ```
78
+
79
+ `ExampleLineChart.new` should preferbly be in a controller or other model.
38
80
 
39
81
  ## Development
40
82
 
@@ -44,7 +86,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
44
86
 
45
87
  ## Contributing
46
88
 
47
- 1. Fork it ( https://github.com/[my-github-username]/chartism/fork )
89
+ 1. Fork it ( https://github.com/darksecond/chartism/fork )
48
90
  2. Create your feature branch (`git checkout -b my-new-feature`)
49
91
  3. Commit your changes (`git commit -am 'Add some feature'`)
50
92
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,6 +1,11 @@
1
1
  require "chartism/version"
2
2
  require "chartism/engine" if defined?(Rails)
3
+ require 'chartism/options'
4
+ require 'chartism/line/options'
5
+ require 'chartism/pie/options'
6
+ require 'chartism/chart'
3
7
  require 'chartism/line'
8
+ require 'chartism/pie'
4
9
 
5
10
  module Chartism
6
11
  end
@@ -0,0 +1,55 @@
1
+ module Chartism
2
+ module Chart
3
+ def define_option method_name
4
+ define_method method_name do
5
+ value = self.class.send(method_name)
6
+ if value.is_a?(Proc)
7
+ instance_eval &value
8
+ else
9
+ value
10
+ end
11
+ end
12
+ end
13
+
14
+ def define_array_option method_name
15
+ define_method method_name do
16
+ values = Array(self.class.send(method_name))
17
+ values.map do |value|
18
+ if value.is_a?(Proc)
19
+ instance_eval &value
20
+ else
21
+ value
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ def define_option method_name
29
+ define_method method_name do |value=nil, &block|
30
+ instance_variable_set "@#{method_name}", value unless value.nil?
31
+ instance_variable_set "@#{method_name}", block if block
32
+ instance_variable_get "@#{method_name}"
33
+ end
34
+ end
35
+
36
+ def define_block_option method_name, default=nil
37
+ define_method method_name do |&block|
38
+ instance_variable_set "@#{method_name}", block if block
39
+ instance_variable_get("@#{method_name}") || default
40
+ end
41
+ end
42
+
43
+ def define_array_option method_name
44
+ define_method method_name do |value=nil, &block|
45
+ array = instance_variable_get "@#{method_name}"
46
+ array = instance_variable_set("@#{method_name}", []) unless array
47
+
48
+ array << value unless value.nil?
49
+ array << block if block
50
+ array
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -3,11 +3,12 @@ require 'securerandom'
3
3
  module Chartism
4
4
  module Helper
5
5
  # Options include:
6
+ # - id
6
7
  # - class
7
8
  def chart chartism, options={}
8
9
  data_json = chartism.data.to_json
9
10
  options_json = chartism.options.to_json
10
- id = "chartism-line-#{SecureRandom.hex(6)}"
11
+ id = options.fetch :id, "chartism-#{SecureRandom.hex(6)}"
11
12
  classes = ( ['ct-chart'] + Array(options[:class]) ).join(' ')
12
13
 
13
14
  %Q[
@@ -16,10 +17,18 @@ module Chartism
16
17
  (function() {
17
18
  var data = #{data_json};
18
19
  var options = #{options_json};
19
- new Chartist.Line("##{id}", data, options);
20
+ new Chartist.#{chart_type(chartism)}("##{id}", data, options);
20
21
  })();
21
22
  </script>
22
23
  ].html_safe
23
24
  end
25
+
26
+ private
27
+
28
+ def chart_type chartism
29
+ return 'Line' if chartism.class.include? Chartism::Line
30
+ return 'Pie' if chartism.class.include? Chartism::Pie
31
+ fail 'unknown Chartism'
32
+ end
24
33
  end
25
34
  end
@@ -1,58 +1,23 @@
1
1
  require 'docile'
2
- require 'chartism/line/options'
3
2
 
4
3
  module Chartism
5
4
  module Line
6
- def self.included(base)
7
- base.extend(ClassMethods)
5
+ extend Chart
6
+
7
+ def self.included base
8
+ base.extend ClassMethods
8
9
  end
9
10
 
10
11
  module ClassMethods
11
- def labels value=nil, &block
12
- if block_given?
13
- @labels = block
14
- elsif value
15
- @labels = value
16
- end
17
-
18
- @labels
19
- end
20
-
21
- def series value=nil, &block
22
- @series ||= []
23
-
24
- if block_given?
25
- @series << block
26
- elsif value
27
- @series << value
28
- end
12
+ extend Chart::ClassMethods
29
13
 
30
- @series
31
- end
32
-
33
- def options &block
34
- @options = block if block_given?
35
- @options
36
- end
37
- end
38
-
39
- def labels
40
- if self.class.labels.is_a?(Proc)
41
- instance_eval(&self.class.labels)
42
- else
43
- self.class.labels
44
- end
14
+ define_option :labels
15
+ define_array_option :series
16
+ define_block_option :options, ->{}
45
17
  end
46
18
 
47
- def series
48
- Array(self.class.series).map do |serie|
49
- if serie.is_a?(Proc)
50
- instance_eval(&serie)
51
- else
52
- serie
53
- end
54
- end
55
- end
19
+ define_option :labels
20
+ define_array_option :series
56
21
 
57
22
  def data
58
23
  {
@@ -62,7 +27,7 @@ module Chartism
62
27
  end
63
28
 
64
29
  def options
65
- Docile.dsl_eval(Options.new, &(self.class.options || ->{}) ).options
30
+ Docile.dsl_eval(Line::Options.new, &self.class.options).options
66
31
  end
67
32
  end
68
33
  end
@@ -1,32 +1,13 @@
1
1
  module Chartism
2
2
  module Line
3
3
  class Options
4
- def initialize
5
- @options = {
6
- showPoint: true,
7
- lineSmooth: true,
8
- showArea: false
9
- }
10
- end
4
+ extend Chartism::Options
11
5
 
12
- def points value=nil, &block
13
- @options[:showPoint] = value
14
- @options[:showPoint] = instance_eval(&block) if block_given?
15
- end
16
-
17
- def smooth value=nil, &block
18
- @options[:lineSmooth] = value unless value.nil?
19
- @options[:lineSmooth] = instance_eval(&block) if block_given?
20
- end
21
-
22
- def area value=nil, &block
23
- @options[:showArea] = value unless value.nil?
24
- @options[:showArea] = instance_eval(&block) if block_given?
25
- end
26
-
27
- def options
28
- @options
29
- end
6
+ define_option :points, :showPoint
7
+ define_option :smooth, :lineSmooth
8
+ define_option :area, :showArea
9
+ define_option :line, :showLine
10
+ define_option :full_width, :fullWidth
30
11
  end
31
12
  end
32
13
  end
@@ -0,0 +1,19 @@
1
+ module Chartism
2
+ module Options
3
+ def self.extended base
4
+ base.include InstanceMethods
5
+ end
6
+
7
+ module InstanceMethods
8
+ attr_reader :options
9
+ end
10
+
11
+ def define_option method_name, option_name
12
+ define_method method_name do |value=nil, &block|
13
+ @options ||= {}
14
+ @options[option_name] = value unless value.nil?
15
+ @options[option_name] = instance_eval(&block) if block
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ require 'docile'
2
+
3
+ module Chartism
4
+ module Pie
5
+ extend Chart
6
+
7
+ def self.included base
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ extend Chart::ClassMethods
13
+
14
+ define_option :labels
15
+ define_option :series
16
+ define_block_option :options, ->{}
17
+ end
18
+
19
+ define_option :labels
20
+ define_option :series
21
+
22
+ def data
23
+ {
24
+ labels: labels,
25
+ series: series
26
+ }
27
+ end
28
+
29
+ def options
30
+ Docile.dsl_eval(Pie::Options.new, &self.class.options).options
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module Chartism
2
+ module Pie
3
+ class Options
4
+ extend Chartism::Options
5
+
6
+ define_option :donut, :donut
7
+ define_option :width, :donutWidth
8
+ define_option :labels, :showLabel
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Chartism
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chartism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Peters
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-20 00:00:00.000000000 Z
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -70,10 +70,14 @@ files:
70
70
  - bin/setup
71
71
  - chartism.gemspec
72
72
  - lib/chartism.rb
73
+ - lib/chartism/chart.rb
73
74
  - lib/chartism/engine.rb
74
75
  - lib/chartism/helper.rb
75
76
  - lib/chartism/line.rb
76
77
  - lib/chartism/line/options.rb
78
+ - lib/chartism/options.rb
79
+ - lib/chartism/pie.rb
80
+ - lib/chartism/pie/options.rb
77
81
  - lib/chartism/version.rb
78
82
  - vendor/assets/javascripts/chartist.js
79
83
  - vendor/assets/stylesheets/chartist.min.css