p5rb 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/p5rb.rb +52 -6
  3. data/p5rb.gemspec +3 -1
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5142534f423ae6b2e335259f2b2e949d0c67923caed5257b18536467cd071dc
4
- data.tar.gz: 2d0fdb1a392ba9601bc82c0ca5ac5f281ea8b09dadf27f8a44800af2c0bf5683
3
+ metadata.gz: b79ebfd6c7fa06d28955e916d1dd8c40f550eac344f03b7eaf65f6281c3bed2f
4
+ data.tar.gz: f2b2555f10efd15b009e146dc232a3b947514960cb81c81487bc0c8d39e205e1
5
5
  SHA512:
6
- metadata.gz: 7e1cf13da82440fc26ef0cf0ac63a022e58c3b9543b78256c04f984e3e5aa3ba3abe37b3a36ed941eb3ab9791dcbfbbe8a4a24a47d2a8ef7044f8d64ed8fd8d7
7
- data.tar.gz: d1b0f0a06fa8a585f5534ff1d1e8d9071fa06543bebc2bc141fa4b54f8887f199646f1f3cfc727267e4caaad87c12d8977379040309fbe3ac7a9f78655035038
6
+ metadata.gz: 5f7ccdcb0a16223887864366aeb2957adb705d8a96bbb60f51195bda826acad9e4a7a67ae925e82f5e7f35c4fdfca2641fac32eb560e8c7d01c9ae280760e008
7
+ data.tar.gz: ee31129a27f4073937c6bb5c2bdffbcc41c9b1ae29ccb425ee7d49b1bbcca391747461899544e21bd30557d0c1fa51d5f4460d5b62f410de580fa3b01909cc72
data/lib/p5rb.rb CHANGED
@@ -34,7 +34,7 @@ module P5
34
34
  @buffer.push "ellipse(#{args.join ?,})"
35
35
  end
36
36
  def fill color, alpha = nil
37
- @buffer.push "fill(#{color.inspect}#{", #{alpha}" if alpha})"
37
+ @buffer.push "fill(#{color}#{", #{alpha}" if alpha})"
38
38
  end
39
39
  def rect x, y, w, h, fill: nil
40
40
  (@buffer.push "push()"; fill fill) if fill
@@ -50,6 +50,12 @@ module P5
50
50
  def textAlign *args
51
51
  @buffer.push "textAlign(#{args.join ", "})"
52
52
  end
53
+ def textWidth text
54
+ @buffer.push "textWidth(#{text.inspect})"
55
+ end
56
+ def textAscent text
57
+ @buffer.push "textAscent(#{text.inspect})"
58
+ end
53
59
  def text text, x, y, fill: nil
54
60
  (@buffer.push "push()"; fill fill) if fill
55
61
  @buffer.push "text(#{text.inspect}, #{x}, #{y})"
@@ -106,7 +112,8 @@ module P5
106
112
  end
107
113
  end
108
114
  end
109
- def plot_bar data
115
+ def plot_bar_grouped data
116
+ # TODO: this is currently pretty much hardcoded for a time dates charting
110
117
  cls = data.values.flat_map(&:keys).uniq.sort
111
118
  size = cls.size + 1
112
119
  from, to = data.keys.minmax
@@ -115,15 +122,53 @@ module P5
115
122
  setup do
116
123
  noStroke
117
124
  textAlign :CENTER
118
- cls.each_with_index{ |_, i| text _, 50+400/(size-2)*i, 25, fill: %w{ red green blue }[i] }
125
+ cls.each_with_index{ |_, i| text _, 50+400/(size-2)*i, 25, fill: %w{ 'red' 'green' 'blue' }[i] }
119
126
  textAlign :RIGHT, :TOP
120
127
  (from..to).each do |date|
121
128
  y = map date.ajd.to_i, from.ajd.to_i, to.ajd.to_i, 50, 450
122
129
  text date.strftime("%m-%d"), 45, y
123
- rect 50, y, map(data.fetch(date,{})[cls[0]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "red"
124
- rect 50, "#{y}+#{400/(to-from)/size}", map(data.fetch(date,{})[cls[1]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "green"
125
- rect 50, "#{y}+#{800/(to-from)/size}", map(data.fetch(date,{})[cls[2]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "blue"
130
+ rect 50, y, map(data.fetch(date,{})[cls[0]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'red'"
131
+ rect 50, "#{y}+#{400/(to-from)/size}", map(data.fetch(date,{})[cls[1]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'green'"
132
+ rect 50, "#{y}+#{800/(to-from)/size}", map(data.fetch(date,{})[cls[2]]||0, 0, max, 0, 400), 400/(to-from)/size, fill: "'blue'"
133
+ end
134
+ end
135
+ end
136
+ end
137
+ def plot_bar_stacked data, names, colorize
138
+ count = {}
139
+ max = data.map do |_, day|
140
+ day.each{ |k,v| count[k] ||= 0; count[k] += v }.map(&:last).reduce :+
141
+ end.max
142
+ pairs = {}
143
+ data.each_cons(2) do |(_, day1), (_, day2)|
144
+ (day1.map(&:first) & day2.map(&:first)).each{ |_| pairs[_] ||= 0; pairs[_] += 1 }
145
+ end
146
+ require "pcbr"
147
+ require "set"
148
+ pcbr = PCBR.new
149
+ count.max_by(colorize){ |k,v| v }.each{ |k,v| pcbr.store k, [-v, pairs[k]||0] }
150
+ top = pcbr.sorted
151
+ size = 500
152
+ P5 size, size do
153
+ setup do
154
+ noStroke
155
+ textAlign :RIGHT, :TOP
156
+ border = 25
157
+ left = "#{border} + textWidth(\"00-00 - 00-00\") + 5"
158
+ data.each_with_index do |(bin, day), i|
159
+ y1 = map i, 0, data.size, border, size-border
160
+ y2 = map i+1, 0, data.size, border, size-border
161
+ text bin, "#{left} - 5", y1
162
+ pos = 0
163
+ day.sort_by{ |k,| top.index(k) || top.size }.each do |k, v|
164
+ rect \
165
+ map(pos, 0, max, left, size-border), y1,
166
+ map(v, 0, max, 0, "#{size}-#{border}-(#{left})"), "#{y2}-#{y1}",
167
+ fill: top.include?(k) ? "color('hsl(#{(((3-Math.sqrt(5))*180 * top.index(k)) % 360).round}, 75%, 75%)')" : "color('hsl(0, 0%, 75%)')"
168
+ pos += v
169
+ end
126
170
  end
171
+ 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%)')" }
127
172
  end
128
173
  end
129
174
  end
@@ -135,6 +180,7 @@ def P5 width, height, &block
135
180
  <<~HEREDOC
136
181
  <html>
137
182
  <head>
183
+ <meta charset="UTF-8">
138
184
  <script src="https://github.com/processing/p5.js/releases/download/v1.4.2/p5.min.js"></script>
139
185
  <script>
140
186
  function setup() {
data/p5rb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "p5rb"
3
- spec.version = "0.0.2"
3
+ spec.version = "0.0.3"
4
4
  spec.summary = "Ruby DSL for p5.js"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
@@ -8,5 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.license = "MIT"
9
9
  spec.metadata = {"source_code_uri" => "https://github.com/nakilon/p5rb"}
10
10
 
11
+ spec.add_dependency "pcbr"
12
+
11
13
  spec.files = %w{ LICENSE p5rb.gemspec lib/p5rb.rb }
12
14
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p5rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-08 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pcbr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description:
14
28
  email: nakilon@gmail.com
15
29
  executables: []