class MyChart::Proto

Attributes

id[R]
name[R]

Public Class Methods

new(grouped_data, opt={}) click to toggle source
# 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
no_z_axis() click to toggle source
# 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() click to toggle source
# File lib/my_chart/proto.rb, line 18
def same_color_on_x
  define_method :styled_datasets do
    diff_color_on_z
  end
end

Public Instance Methods

concrete_options() click to toggle source
# File lib/my_chart/proto.rb, line 84
def concrete_options
  {}
end
concrete_style() click to toggle source
# File lib/my_chart/proto.rb, line 92
def concrete_style
  {}
end
concrete_type() click to toggle source
# File lib/my_chart/proto.rb, line 77
def concrete_type
end
datasets() click to toggle source
# File lib/my_chart/proto.rb, line 39
def datasets
  @grouped_data.datasets
end
default_html() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/my_chart/proto.rb, line 100
def has_z?
  datasets.size > 1
end
height() click to toggle source
# File lib/my_chart/proto.rb, line 73
def height
  @height || 300
end
iife() click to toggle source
# File lib/my_chart/proto.rb, line 54
def iife
  "(function(){
    var ctx = document.getElementById('#{id}');
    var myChart = new Chart(ctx, #{json});
  })();"
end
json() click to toggle source
# File lib/my_chart/proto.rb, line 43
def json
  {
   type: type,
   data: {
     labels: labels,
     datasets: styled_datasets
     },
   options: options
  }.to_json
end
labels() click to toggle source
# File lib/my_chart/proto.rb, line 35
def labels
  @grouped_data.labels
end
no_z_axis?() click to toggle source
# File lib/my_chart/proto.rb, line 122
def no_z_axis?
  false
end
options() click to toggle source
# File lib/my_chart/proto.rb, line 88
def options
  {responsive: false}.merge concrete_options
end
styled_datasets() click to toggle source
# File lib/my_chart/proto.rb, line 96
def styled_datasets
  has_z? ? diff_color_on_z : diff_color_on_x
end
title() click to toggle source
# File lib/my_chart/proto.rb, line 65
def title
  name ? "<h1>#{name}</h1>" : ""
end
type() click to toggle source
# File lib/my_chart/proto.rb, line 80
def type
  concrete_type || :proto
end
width() click to toggle source
# File lib/my_chart/proto.rb, line 69
def width
  @width || 800
end