Back

Activity 2.20: Raylib Lab 11 — Visualizing a Dataset

divider

What You're Building

By the end of Task 2 you have two charts of the same five numbers: how many matches each hero in a game has won. In the top chart the heroes look evenly matched. In the bottom one, Rogue looks like it wins ten times as often as Archer.

Two bar charts of the same five win counts for Knight, Archer, Mage, Rogue and Bard. Counting from 0, the bars are nearly the same height. Counting from 380, Rogue's bar towers over Archer's.

Task 2: one dataset, two stories

Neither chart is lying. Every bar is the right height for its own scale, and every bar has its real number under it. One number in one line of code separates them.

Task 3 is not shown. That one is yours to work out.

divider

Raylib Lab 11

Visualizing a Dataset

Key Concepts

A Chart Is a Program

The Baseline Decides the Story

True Bars, False Impressions

How to Read Someone Else's Chart

The Last Step With Data Is Showing It

Activity 2.18 turned data into information, and cleaned it first. Today you show someone what you found, as a chart.

The College Board lists the ways programs process data: transforming every element, filtering, combining and comparing, and visualizing. A chart does the first and last at once: it transforms every number into a bar height, then draws it.

Five Numbers

A teacher's quiz averages across five class periods. Is any period doing much better than the others?

Value Trace
quiz average, five class periods
P1 82 P2 79 P3 84 P4 81 P5 80

Not really. The best is 84 and the worst is 79. It is a steady week.

Five Numbers

Draw them as bars that start counting at 0.

Value Trace
counting from 0
P1 82 ####################
P2 79 ###################
P3 84 ####################
P4 81 ###################
P5 80 ###################

The picture agrees with the numbers.

Five Numbers

Now start counting at 75. Same numbers, same labels.

Value Trace
counting from 75
P1 82 ################
P2 79 #########
P3 84 ####################
P4 81 #############
P5 80 ###########

Period 2 now looks like it is failing. Its bar is half of Period 3's, though its score is 5 points lower out of 84.

The Baseline Is a Choice

A chart's baseline is the value its bars start counting from. Starting at 75 cut off the bottom 75 points of every bar, which is the part they all share.

What is left is only the differences, so small differences look huge. Sometimes that is exactly what you want to show. It must never be a surprise to the reader.

A Chart Can Mislead With Only True Numbers

Every bar in the second chart has the right height for its scale. Every score is printed next to its bar.

The numbers are true and the impression is false. Most readers look at the shapes, not the labels, and the shape says Period 2 is failing.

Three Activities, One Lesson

2.16: every sample was measured correctly, and three samples hid the shape of the wave.

2.18: every survey answer was accurate, and the survey was still biased.

Today: every bar is the right height, and the chart still misleads. Correct data is not the same as a correct conclusion.

So Read Charts Like This

  • Where does the axis start? Look for the baseline before the bars.
  • What is not plotted? Missing years, missing groups.
  • Who chose the range, and what did they want you to think?

Today's Objectives

  • Writing a program that turns a dataset into a chart
  • Using a parameter to change the chart's baseline
  • Explaining how a chart can mislead without a single wrong value
  • Naming what to check before believing someone else's chart

Key Terms

Visualization
A chart, graph or other picture used to communicate what was found in data.
Baseline
The value an axis starts counting from. Choosing it is a decision, not a fact.

'F' → Fullscreen

divider

Build

The game studio's balance team is arguing about whether the Rogue hero is too strong, and they want a chart for tomorrow's meeting. Create a new Raylib program named 2-20-win-chart.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.

This lab needs the workspace, not Juicemind. Close the window to end the program. Escape works too.


Task 1: One Honest Chart

  • DrawChart first finds the biggest win count, then draws one bar per hero. Every bar is up to 140 pixels tall, and its height is worked out by this line:
Value Trace
height = (value - baseline) * 140 / (biggest - baseline)
baseline 0 a bar's height is its share of the biggest value
baseline 380 a bar's height is its share of the part above 380
  • The multiplication comes before the division on purpose. In integer division, 412 / 455 is 0, so dividing first would make every bar but the biggest 0 pixels tall.
  • DrawChart takes three parameters. Name each one and say what changes when you change it.
  • Change Rogue's 455 to 600 and run it. Every other bar shrinks, though their numbers did not change. Explain why, using the loop that finds biggest. Then put 455 back.
2-20-win-chart
#:package Raylib-cs@8.0.0
using Raylib_cs;
string[] heroes = { "Knight", "Archer", "Mage", "Rogue", "Bard" };
int[] wins = { 412, 388, 401, 455, 423 };
Raylib.InitWindow(800, 460, "Win Chart");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(24, 26, 34, 255));
DrawChart(0, 10, "Wins, counting from 0");
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawChart(int baseline, int top, string title)
{
int biggest = 0;
for (int i = 0; i < wins.Length; i++)
{
if (wins[i] > biggest)
{
biggest = wins[i];
}
}
Raylib.DrawText(title, 60, top, 20, Color.White);
for (int i = 0; i < wins.Length; i++)
{
int height = (wins[i] - baseline) * 140 / (biggest - baseline);
int x = 60 + i * 140;
Raylib.DrawRectangle(x, top + 170 - height, 100, height, Color.SkyBlue);
Raylib.DrawText(heroes[i], x, top + 176, 16, Color.LightGray);
Raylib.DrawText($"{wins[i]}", x, top + 194, 16, Color.LightGray);
}
}

Task 2: The Same Data, Told Differently

  • Add the second call on line 17. Change nothing else. The only difference between the two calls is the baseline, and where the chart is drawn.
  • Rogue has won 455 matches and Archer 388. Work out how many times taller Rogue's bar is than Archer's in each chart, using the heights from the formula. Then compare with 455 ÷ 388.
  • Which chart would you bring to argue that Rogue must be made weaker? Which would you bring to argue the game is balanced? Be honest about it.
2-20-win-chart
#:package Raylib-cs@8.0.0
using Raylib_cs;
string[] heroes = { "Knight", "Archer", "Mage", "Rogue", "Bard" };
int[] wins = { 412, 388, 401, 455, 423 };
Raylib.InitWindow(800, 460, "Win Chart");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(24, 26, 34, 255));
DrawChart(0, 10, "Wins, counting from 0");
DrawChart(380, 240, "Wins, counting from 380");
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawChart(int baseline, int top, string title)
{
int biggest = 0;
for (int i = 0; i < wins.Length; i++)
{
if (wins[i] > biggest)
{
biggest = wins[i];
}
}
Raylib.DrawText(title, 60, top, 20, Color.White);
for (int i = 0; i < wins.Length; i++)
{
int height = (wins[i] - baseline) * 140 / (biggest - baseline);
int x = 60 + i * 140;
Raylib.DrawRectangle(x, top + 170 - height, 100, height, Color.SkyBlue);
Raylib.DrawText(heroes[i], x, top + 176, 16, Color.LightGray);
Raylib.DrawText($"{wins[i]}", x, top + 194, 16, Color.LightGray);
}
}

Task 3: Read Your Own Charts

  • Draw an average line on both charts, with a label giving the average. Work out the average inside DrawChart, find its height with the same formula as a bar, and draw a rectangle two pixels tall across the chart at that height.
  • Write a one-sentence headline for each chart: the headline a gaming website would run if it had seen only that chart.
  • Now pick the data yourself. Replace the five win counts with numbers of your own, all above 380, so that the two charts tell the same story. Say what is true about your numbers that makes it work.

Challenge (Optional): Highlight the Leader

  • Draw the bar for the hero with the most wins in a different color from the rest.
  • Did that help a reader, or only decorate the chart? Say how you would tell the difference.
divider

Checkpoint

Both pictures are screenshots of this page's own programs.

One bar chart of five win counts, counting from 0, with all five bars nearly the same height

Task 1

The same chart, and below it the same five numbers counted from 380

Task 2

  • You named DrawChart's three parameters and explained why the bars shrank
  • You compared Rogue and Archer in both charts with 455 ÷ 388
  • Both charts have a labeled average line
  • You found five numbers of your own where both charts agree, and said why
divider

Reflection

Answer the following questions before submitting your work.

  1. Both charts printed the true number under every bar. Explain why that did not stop the bottom chart from misleading.
  2. You chose five numbers in Task 3 so that both charts agreed. What had to be true about them?
  3. Name one thing you will now check before believing a chart in a news article or a video.
divider

Submit

Submit four things to the dropbox:

  1. Your 2-20-win-chart program file, with your Task 3 numbers and average lines.
  2. Your Task 1 answers.
  3. Your Task 2 heights and comparison, and which chart you would bring to each argument.
  4. Your two headlines, why your Task 3 numbers work, and your reflection answers.

Activity Complete