Class: RailsCharts::LineChart

Inherits:
BaseChart show all
Defined in:
lib/rails_charts/line_chart.rb

Constant Summary

Constants inherited from BaseChart

BaseChart::CHART_JS_PATTERN

Instance Attribute Summary

Attributes inherited from BaseChart

#chart_id, #container_id, #data, #debug, #height, #klass, #locale, #options, #other_options, #renderer, #style, #theme, #vertical, #width

Instance Method Summary collapse

Methods inherited from BaseChart

#axises, #build_options, #js_code, #option

Constructor Details

#initialize(data, options = {}) ⇒ LineChart

Returns a new instance of LineChart.



4
5
6
# File 'lib/rails_charts/line_chart.rb', line 4

def initialize(data, options = {})
  super(Array.wrap(data.to_a), options)
end

Instance Method Details

#defaultsObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails_charts/line_chart.rb', line 64

def defaults
  {
    tooltip: {
      trigger: 'axis'
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      },
    }
  }
end

#generate_series_optionsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_charts/line_chart.rb', line 32

def generate_series_options
  # binding.pry if debug
  case data[0]
  when Array
    {
      data: is_a?(AreaChart) || is_a?(ScatterChart) ? data : data.map(&:last),
      type: type,
    }
  when Hash
    data.map do |e| 
      {
        data: e[:data].is_a?(Hash) ? e[:data].values : e[:data],
        type: type,
        name: e[:name]
      }
    end
  end
end

#typeObject



8
9
10
# File 'lib/rails_charts/line_chart.rb', line 8

def type
  'line'
end

#xObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/rails_charts/line_chart.rb', line 12

def x
  case data[0]
  when Array
    data.map{|e| e[0]}
  when Hash
    data.map{|e| e[:data].keys}.flatten
  else
    nil
  end&.uniq
end

#x_axisObject



51
52
53
54
55
56
# File 'lib/rails_charts/line_chart.rb', line 51

def x_axis
  {
    type: 'category',
    data: x
  }
end

#yObject



23
24
25
26
27
28
29
30
# File 'lib/rails_charts/line_chart.rb', line 23

def y
  case data[0]
  when Array
    data.map{|e| e[1]}
  when Hash
    data.map{|e| e[:name]}
  end
end

#y_axisObject



58
59
60
61
62
# File 'lib/rails_charts/line_chart.rb', line 58

def y_axis
  {
    type: 'value',
  }
end