Class: MyChart::Proto
- Inherits:
-
Object
show all
- Defined in:
- lib/my_chart/proto.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(grouped_data, opt = {}) ⇒ Proto
Returns a new instance of Proto.
26
27
28
29
30
31
32
33
|
# File 'lib/my_chart/proto.rb', line 26
def initialize grouped_data, opt={}
raise Exception, "#{type} has no z axis" if grouped_data.kind_of? MyChart::XYZ and no_z_axis?
@id = opt[:id]
@grouped_data = grouped_data
@width = opt[:w]
@height = opt[:h]
@name = opt[:name]
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
8
9
10
|
# File 'lib/my_chart/proto.rb', line 8
def id
@id
end
|
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/my_chart/proto.rb', line 8
def name
@name
end
|
Class Method Details
.no_z_axis ⇒ Object
12
13
14
15
16
|
# File 'lib/my_chart/proto.rb', line 12
def no_z_axis
define_method :no_z_axis? do
true
end
end
|
.same_color_on_x ⇒ Object
18
19
20
21
22
|
# File 'lib/my_chart/proto.rb', line 18
def same_color_on_x
define_method :styled_datasets do
diff_color_on_z
end
end
|
Instance Method Details
#concrete_options ⇒ Object
84
85
86
|
# File 'lib/my_chart/proto.rb', line 84
def concrete_options
{}
end
|
#concrete_style ⇒ Object
92
93
94
|
# File 'lib/my_chart/proto.rb', line 92
def concrete_style
{}
end
|
#concrete_type ⇒ Object
77
78
|
# File 'lib/my_chart/proto.rb', line 77
def concrete_type
end
|
#datasets ⇒ Object
39
40
41
|
# File 'lib/my_chart/proto.rb', line 39
def datasets
@grouped_data.datasets
end
|
#default_html ⇒ Object
61
62
63
|
# File 'lib/my_chart/proto.rb', line 61
def default_html
"<div class='my_chart'>#{title}<canvas id='#{id}' width='#{width}' height='#{height}'></canvas><script>#{iife}</script></div>"
end
|
#diff_color_on_x ⇒ Object
115
116
117
118
119
120
|
# File 'lib/my_chart/proto.rb', line 115
def diff_color_on_x
[datasets[0].
merge({backgroundColor: Rainbow[labels.size].map(&:to_s)}).
merge(concrete_style)
]
end
|
#diff_color_on_z ⇒ Object
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/my_chart/proto.rb', line 104
def diff_color_on_z
colors = Rainbow[datasets.size].map do |color|
{borderColor: color.to_s,
backgroundColor: color.alpha(0.2).to_s,
borderWidth: 1}
end
datasets.zip(colors).map do |ds, col|
ds.merge(col).merge concrete_style
end
end
|
#has_z? ⇒ Boolean
100
101
102
|
# File 'lib/my_chart/proto.rb', line 100
def has_z?
datasets.size > 1
end
|
#height ⇒ Object
73
74
75
|
# File 'lib/my_chart/proto.rb', line 73
def height
@height || 300
end
|
#iife ⇒ Object
54
55
56
57
58
59
|
# File 'lib/my_chart/proto.rb', line 54
def iife
"(function(){
var ctx = document.getElementById('#{id}');
var myChart = new Chart(ctx, #{json});
})();"
end
|
#json ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/my_chart/proto.rb', line 43
def json
{
type: type,
data: {
labels: labels,
datasets: styled_datasets
},
options: options
}.to_json
end
|
#labels ⇒ Object
35
36
37
|
# File 'lib/my_chart/proto.rb', line 35
def labels
@grouped_data.labels
end
|
#no_z_axis? ⇒ Boolean
122
123
124
|
# File 'lib/my_chart/proto.rb', line 122
def no_z_axis?
false
end
|
#options ⇒ Object
88
89
90
|
# File 'lib/my_chart/proto.rb', line 88
def options
{responsive: false}.merge concrete_options
end
|
#styled_datasets ⇒ Object
96
97
98
|
# File 'lib/my_chart/proto.rb', line 96
def styled_datasets
has_z? ? diff_color_on_z : diff_color_on_x
end
|
#title ⇒ Object
65
66
67
|
# File 'lib/my_chart/proto.rb', line 65
def title
name ? "<h1>#{name}</h1>" : ""
end
|
#type ⇒ Object
80
81
82
|
# File 'lib/my_chart/proto.rb', line 80
def type
concrete_type || :proto
end
|
#width ⇒ Object
69
70
71
|
# File 'lib/my_chart/proto.rb', line 69
def width
@width || 800
end
|