chartism 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +46 -4
- data/lib/chartism.rb +5 -0
- data/lib/chartism/chart.rb +55 -0
- data/lib/chartism/helper.rb +11 -2
- data/lib/chartism/line.rb +11 -46
- data/lib/chartism/line/options.rb +6 -25
- data/lib/chartism/options.rb +19 -0
- data/lib/chartism/pie.rb +33 -0
- data/lib/chartism/pie/options.rb +11 -0
- data/lib/chartism/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a60a2d7e2d316b903c4772e40a7b37c790917483
|
4
|
+
data.tar.gz: 441edfba105c7a98fff5e1a923dc618782074abe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fa06be843119b912fc97f95f80870378349bb45b0d5dc583194989fbe75e28816dc90d8e483658baa3317fbdea6d05f30193fbd7919dbee5e90f9b6a487db5e
|
7
|
+
data.tar.gz: 5e2bec67d895d6a102a4f63e7b13a011bc219581064b79913ef62edaa5fcdfaa98d5748285eaa1de98abacfa76d993305d11a4d128933b32afd90f8790647bbd
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# Chartism
|
2
2
|
|
3
|
-
|
3
|
+
[](https://gemnasium.com/Darksecond/chartism)
|
4
|
+
[](http://badge.fury.io/rb/chartism)
|
5
|
+
[](https://codeclimate.com/github/Darksecond/chartism)
|
4
6
|
|
5
|
-
|
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
|
-
|
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/
|
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`)
|
data/lib/chartism.rb
CHANGED
@@ -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
|
data/lib/chartism/helper.rb
CHANGED
@@ -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
|
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
|
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
|
data/lib/chartism/line.rb
CHANGED
@@ -1,58 +1,23 @@
|
|
1
1
|
require 'docile'
|
2
|
-
require 'chartism/line/options'
|
3
2
|
|
4
3
|
module Chartism
|
5
4
|
module Line
|
6
|
-
|
7
|
-
|
5
|
+
extend Chart
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
8
9
|
end
|
9
10
|
|
10
11
|
module ClassMethods
|
11
|
-
|
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
|
-
|
31
|
-
|
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
|
-
|
48
|
-
|
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, &
|
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
|
-
|
5
|
-
@options = {
|
6
|
-
showPoint: true,
|
7
|
-
lineSmooth: true,
|
8
|
-
showArea: false
|
9
|
-
}
|
10
|
-
end
|
4
|
+
extend Chartism::Options
|
11
5
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
data/lib/chartism/pie.rb
ADDED
@@ -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
|
data/lib/chartism/version.rb
CHANGED
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.
|
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-
|
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
|