|
| 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 | +} |
0 commit comments