Skip to content

Commit f256c8a

Browse files
authored
*: Add table and advanced exmaple for grafica (#245)
Signed-off-by: Ce Gao <ce.gao@outlook.com>
1 parent 7b78d2c commit f256c8a

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Load the table before the sketch is run
2+
settings <- function() {
3+
# Please install the grafica library before you run the example.
4+
importLibrary("grafica")
5+
size(750, 450)
6+
}
7+
8+
setup <- function() {
9+
10+
table <- loadTable("https://raw.githubusercontent.com/jagracar/grafica/master/examples/LifeExpectancy/data/data.csv", "header")
11+
# Save the data in an array and calculate the point sizes
12+
points <- GPointsArray$new()
13+
# Create the plot
14+
plot <- GPlot$new(processing)
15+
pointSizes <- plot$getMainLayer()$getPointSizes()
16+
17+
print(table)
18+
for (row in 0:(table$getRowCount() - 1)) {
19+
data <- table$getRow(row)
20+
21+
country <- data$getString("country")
22+
income <- data$getFloat("income")
23+
health <- data$getFloat("health")
24+
population <- data$getInt("population")
25+
points$add(GPoint$new(income, health, country))
26+
27+
scaleFactor <- width / 750
28+
pointSizes <- c(pointSizes, 2 * sqrt(population / (200000 * PI)) * scaleFactor)
29+
}
30+
31+
plot$setOuterDim(width, height)
32+
plot$setTitleText("Life expectancy connection to average income")
33+
plot$getXAxis()$setAxisLabelText("Personal income ($/year)")
34+
plot$getYAxis()$setAxisLabelText("Life expectancy (years)")
35+
plot$setLogScale("x")
36+
plot$setPoints(points)
37+
plot$setPointSizes(pointSizes)
38+
plot$activatePointLabels()
39+
plot$activatePanning()
40+
plot$activateZooming(1.1, CENTER, CENTER)
41+
}
42+
43+
# Execute the sketch
44+
draw <- function() {
45+
# Clean the canvas
46+
background(255)
47+
48+
# Draw the plot
49+
plot$beginDraw()
50+
plot$drawBox()
51+
plot$drawXAxis()
52+
plot$drawYAxis()
53+
plot$drawTitle()
54+
plot$drawGridLines(GPlot$BOTH)
55+
plot$drawPoints()
56+
plot$drawLabels()
57+
plot$endDraw()
58+
}

src/rprocessing/r/core.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import(rprocessing.RLangPApplet)
22
import(processing.core.PVector)
3+
import(processing.data.Table)
34

45
alpha = processing$alpha
56
ambient = processing$ambient

0 commit comments

Comments
 (0)