Skip to content

Commit d4cc459

Browse files
author
monkstone
committed
update load_table sketch
1 parent 684b749 commit d4cc459

File tree

2 files changed

+30
-43
lines changed

2 files changed

+30
-43
lines changed

samples/processing_app/topics/advanced_data/library/bubble/bubble.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# The bubble library, include BubbleStruct
2-
32
class Bubble
43
include Processing::Proxy
54

@@ -16,8 +15,8 @@ def initialize(x, y, diameter, name)
1615

1716
# Checking if mouse is over the Bubble
1817
def rollover(px, py)
19-
d = dist(px,py,x,y)
20-
@over = (d < diameter/2)? true : false
18+
d = dist(px, py, x, y)
19+
@over = (d < diameter / 2.0) ? true : false
2120
end
2221

2322
# Display the Bubble
@@ -26,15 +25,21 @@ def display
2625
stroke_weight(2)
2726
noFill
2827
ellipse(x, y, diameter, diameter)
29-
if (over)
30-
fill(0)
31-
text_align(CENTER)
32-
text(name, x, y + diameter / 2 + 20)
33-
end
28+
return unless over
29+
fill(0)
30+
text_align(CENTER)
31+
text(name, x, y + diameter / 2 + 20)
32+
end
33+
34+
def to_a
35+
[x, y, diameter, name]
3436
end
3537

3638
def to_hash
37-
{'position' => {'x' => x, 'y' => y}, 'diameter' => diameter, 'label' => name}
39+
{ 'position' => { 'x' => x, 'y' => y },
40+
'diameter' => diameter,
41+
'label' => name
42+
}
3843
end
3944

4045
def to_struct
Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#
22
# Loading Tabular Data
3-
# after Daniel Shiffman, by Martin Prout.
4-
#
3+
# after Daniel Shiffman, by Martin Prout.
4+
#
55
# This example demonstrates how to use CSV
6-
# to retrieve data from a CSV file and make objects
6+
# to retrieve data from a CSV file and make objects
77
# from that data.
88
#
99
# Here is what the CSV looks like:
@@ -32,54 +32,36 @@ def draw
3232
b.display
3333
b.rollover(mouse_x, mouse_y)
3434
end
35-
3635
text_align(LEFT)
3736
fill(0)
3837
text('Click to add bubbles.', 10, height - 10)
3938
end
4039

4140
def load_data
4241
# Load CSV file into an Array of Hash objects
43-
# :headers option indicates the file has a header row
44-
@data = CSV.read('data/data.csv', :headers => true).map{|row| row.to_hash}
45-
46-
# The size of the array of Bubble objects is determined by the total number of 'rows' in the CSV
47-
@bubbles = []
48-
49-
data.each do |row|
50-
# You access the values via their column name (set by using headers option above)
42+
# headers: option indicates the file has a header row
43+
@bubbles = []
44+
CSV.foreach('data/data.csv', headers: true) do |row|
5145
x = row['x'].to_f
5246
y = row['y'].to_f
5347
d = row['diameter'].to_f
54-
n = row['name']
48+
n = row['name']
5549
# Make a Bubble object out of the data read
5650
bubbles << Bubble.new(x, y, d, n)
57-
end
58-
51+
end
5952
end
6053

6154
def mouse_pressed
62-
# Create a new 'row' hash
63-
row = {'x' => mouse_x.to_s, 'y' => mouse_y.to_s, 'diameter' => random(40, 80).to_s, 'name' => 'Blah'}
64-
# add the row to the existing data array
65-
data << row
66-
67-
# If the table has more than 10 rows
68-
if (data.size > 10)
69-
# Delete the oldest row
70-
data.shift
71-
end
72-
# read column names from data, and generate csv 'string' that can be written to file
73-
column_names = data.first.keys
74-
s = CSV.generate do |csv|
75-
csv << column_names
76-
data.each do |row|
77-
csv << row.values
55+
bubbles << Bubble.new(mouse_x, mouse_y, rand(40..80), 'blah')
56+
# If there are more than 10 bubbles delete the oldest bubble
57+
bubbles.shift if bubbles.size > 10
58+
# Writing the csv data back to the same file, (also specify UTF-8 format)
59+
CSV.open('data/data.csv', 'w:UTF-8') do |csv|
60+
csv << %w(x y diameter name) # create csv headers
61+
bubbles.each do |bubble|
62+
csv << bubble.to_a # write back bubble data
7863
end
7964
end
80-
# Writing the csv data back to the same file, (also specify UTF-8 format)
81-
File.open('data/data.csv', 'w:UTF-8') { |file| file.write(s)}
8265
# And reloading it
8366
load_data
8467
end
85-

0 commit comments

Comments
 (0)