Class: Toji::Progress::Graph::Ab

Inherits:
Object
  • Object
show all
Defined in:
lib/toji/progress/graph/ab.rb

Instance Method Summary collapse

Constructor Details

#initializeAb

Returns a new instance of Ab.



5
6
7
8
9
# File 'lib/toji/progress/graph/ab.rb', line 5

def initialize
  @coef = 1.0
  @actuals = []
  @expects = []
end

Instance Method Details

#actual(moromi, name = :actual) ⇒ Object



16
17
18
19
# File 'lib/toji/progress/graph/ab.rb', line 16

def actual(moromi, name=:actual)
  @actuals << [moromi, name]
  self
end

#coef(coef) ⇒ Object



11
12
13
14
# File 'lib/toji/progress/graph/ab.rb', line 11

def coef(coef)
  @coef = coef
  self
end

#expect(alcohol, nihonshudo) ⇒ Object



21
22
23
24
# File 'lib/toji/progress/graph/ab.rb', line 21

def expect(alcohol, nihonshudo)
  @expects << [alcohol.to_f, nihonshudo.to_f]
  self
end

#expects(expects) ⇒ Object



26
27
28
29
30
31
# File 'lib/toji/progress/graph/ab.rb', line 26

def expects(expects)
  expects.each {|o|
    expect(o.alcohol, o.nihonshudo)
  }
  self
end

#max_baumeObject



64
65
66
67
68
# File 'lib/toji/progress/graph/ab.rb', line 64

def max_baume
  @actuals.map {|moromi, name|
    moromi.states.map(&:baume).compact.max
  }.compact.max || 0.0
end

#min_baumeObject



58
59
60
61
62
# File 'lib/toji/progress/graph/ab.rb', line 58

def min_baume
  @actuals.map {|moromi, name|
    moromi.states.map(&:baume).compact.min
  }.compact.min || 0.0
end

#plotObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/toji/progress/graph/ab.rb', line 70

def plot
  #_min_baume = [min_baume-1, 0].min
  _min_baume = 0

  _max_baume = [max_baume+1, 10].max

  Plotly::Plot.new(
    data: plot_data,
    layout: {
      xaxis: {
        title: "Alcohol",
        dtick: 2,
        range: [0, 20],
      },
      yaxis: {
        title: "Baume",
        dtick: 1,
        range: [_min_baume, _max_baume],
      }
    }
  )
end

#plot_dataObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/toji/progress/graph/ab.rb', line 33

def plot_data
  result = []

  @actuals.each {|moromi, name|
    states = moromi.states.select{|s| s.alcohol && s.baume}

    xs = states.map(&:alcohol)
    ys = states.map(&:baume)
    texts = states.map{|s| "%s<br />alc=%s, be=%s" % [s.display_time, s.alcohol, s.baume]}

    result << {x: xs, y: ys, text: texts, name: name}
  }

  @expects.each {|alcohol, nihonshudo|
    ys = [0.0, 8.0]
    xs = ys.map{|b| alcohol - (b - nihonshudo * -0.1) * @coef}

    name = "%s%s %s" % [nihonshudo<0 ? "" : "+", nihonshudo, alcohol]

    result << {x: xs, y: ys, name: name}
  }

  result
end