Module: P5

Defined in:
lib/p5rb.rb

Defined Under Namespace

Modules: Block

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.buffer_drawObject (readonly)

Returns the value of attribute buffer_draw.



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

def buffer_draw
  @buffer_draw
end

.buffer_setupObject (readonly)

Returns the value of attribute buffer_setup.



3
4
5
# File 'lib/p5rb.rb', line 3

def buffer_setup
  @buffer_setup
end

Class Method Details

.draw(&block) ⇒ Object

not tested yet



75
76
77
78
# File 'lib/p5rb.rb', line 75

def draw &block   # not tested yet
  ::P5::Block.buffer = @buffer_draw
  ::P5::Block.module_eval &block
end

.plot_bar_grouped(data) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/p5rb.rb', line 115

def plot_bar_grouped data
  # TODO: this is currently pretty much hardcoded for a time dates charting
  cls = data.values.flat_map(&:keys).uniq.sort
  size = cls.size + 1
  from, to = data.keys.minmax
  max = data.values.flat_map(&:values).max
  P5 500, 500 do
    setup do
      noStroke
      textAlign :CENTER
      cls.each_with_index{ |_, i| text _, 50+400/(size-2)*i, 25, fill: %w{ 'red' 'green' 'blue' }[i] }
      textAlign :RIGHT, :TOP
      (from..to).each do |date|
        y = map date.ajd.to_i, from.ajd.to_i, to.ajd.to_i, 50, 450
        text date.strftime("%m-%d"), 45, y
        rect 50, y,                            map(data.fetch(date,{})[cls[0]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'red'"
        rect 50, "#{y}+#{400/(to-from)/size}", map(data.fetch(date,{})[cls[1]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'green'"
        rect 50, "#{y}+#{800/(to-from)/size}", map(data.fetch(date,{})[cls[2]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'blue'"
      end
    end
  end
end

.plot_bar_stacked(data, names, colorize) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/p5rb.rb', line 137

def plot_bar_stacked data, names, colorize
  count = {}
  max = data.map do |_, day|
    day.each{ |k,v| count[k] ||= 0; count[k] += v }.map(&:last).reduce :+
  end.max
  pairs = {}
  data.each_cons(2) do |(_, day1), (_, day2)|
    (day1.map(&:first) & day2.map(&:first)).each{ |_| pairs[_] ||= 0; pairs[_] += 1 }
  end
  require "pcbr"
  require "set"
  pcbr = PCBR.new
  count.max_by(colorize){ |k,v| v }.each{ |k,v| pcbr.store k, [-v, pairs[k]||0] }
  top = pcbr.sorted
  size = 500
  P5 size, size do
    setup do
      noStroke
      textAlign :RIGHT, :TOP
      border = 25
      left = "#{border} + textWidth(\"00-00 - 00-00\") + 5"
      data.each_with_index do |(bin, day), i|
        y1 = map i, 0, data.size, border, size-border
        y2 = map i+1, 0, data.size, border, size-border
        text bin, "#{left} - 5", y1
        pos = 0
        day.sort_by{ |k,| top.index(k) || top.size }.each do |k, v|
          rect \
            map(pos, 0, max, left, size-border), y1,
            map(v, 0, max, 0, "#{size}-#{border}-(#{left})"), "#{y2}-#{y1}",
            fill: top.include?(k) ? "color('hsl(#{(((3-Math.sqrt(5))*180 * top.index(k)) % 360).round}, 75%, 75%)')" : "color('hsl(0, 0%, 75%)')"
          pos += v
        end
      end
      top.each_with_index{ |id, i| text names[id].inspect[1..-2], size-border, "#{border} + textAscent('X') * #{i}", fill: "color('hsl(#{(((3-Math.sqrt(5))*180 * top.index(id)) % 360).round}, 75%, 75%)')" }
    end
  end
end

.plot_scatter(data, reverse_y: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/p5rb.rb', line 82

def plot_scatter data, reverse_y: false
  size = 1000
  max = nil
  (x_range, x_from, x_to, x_enum, x_f),
  (y_range, y_from, y_to, y_enum, y_f) = data.transpose.map do |axis|
    min, max = axis.minmax
    range = (min - max).abs
    division = 10**Math::log10(range).floor
    from = min.div(division)*division
    to = -(-max).div(division)*division
    [
      to - from,
      from, to,
      from.step(to, division),
    ]
  end
  max = [x_range, y_range].max
  x_f = ->_{ 20 + (size-40.0) * (_ - x_from) / max }
  y_f = ->_{ 20 + (size-40.0) * (reverse_y ? y_to - _ : _ - y_from) / max }
  P5 40 + (size-40.0) * x_range / max + 50,   # TODO: properly fix the issue that with wide labels the right end of the plot may be cut off
     40 + (size-40.0) * y_range / max do
    setup do
      textSize 15
      raw "const w = max([#{y_enum.map{ |_| "textWidth(#{_})" }.join ?,}])"
      raw "translate(w, 15)"
      stroke 200
      textAlign :CENTER, :BOTTOM; x_enum.each{ |_| line x_f[_], y_f[y_from], x_f[_], y_f[y_to]; text _, x_f[_], 20-5 }
      textAlign :RIGHT, :CENTER;  y_enum.each{ |_| line x_f[x_from], y_f[_], x_f[x_to], y_f[_]; text _, x_f[x_from]-5, y_f[_] }
      stroke 0
      data.each{ |x,y| point x_f[x], y_f[y] }
    end
  end
end

.setup(&block) ⇒ Object



71
72
73
74
# File 'lib/p5rb.rb', line 71

def setup &block
  ::P5::Block.buffer = @buffer_setup
  ::P5::Block.module_eval &block
end