Data in Action_ Collecting Data in Bar Graphs and Pie Charts_05_30_23 (1)
Data in Action_ Collecting Data in Bar Graphs and Pie Charts_05_30_23 (1)
Track My Mood
This step-by-step guide shows you how to
create an app that tracks your mood for a Discussion: What Visualizations Do You
week or month, then displays the data with a Like?
helpful visual. Based on our popular Mood Visualizations present complex data in a
Ring tutorial, the app goes one step further graphical way that is easier for people to
— it allows you to create a visualization of understand. They help people see at a
the number of happy, angry, and sad days glance what is happening with an issue they
over time.
All the data stays in the app until you press Reset. Look for the following guide in this
series on sending your data to an online spreadsheet for sharing with a group.
1
Prerequisites:
● Comfort navigating App Inventor Designer and Blocks screens.
● If you are using a phone or table, you will need an up-to-date App Inventor
Companion (version 2.65 or higher) for Android devices (iOS not available yet, iOS
chart capability currently in development)
● Don’t have a device? To use the phone emulator on your computer, check the
setup instructions.
2
THE CHALLENGE: Add Charts to Your App
Why Create an App to Collect Data?
Phone apps are a great way to collect data using surveys, polls, or observations of nature.
For example, the Track My Mood app collects a user’s private data (How are you feeling?)
in three categories (“Happy,” “Angry,” and “Sad”). The app shows the data back to you so
you can make sense of it.
● App Inventor can show data in many ways, including charts and
graphs:
In this guide, you will learn how to pick different charts for use in your future app ideas!
3
PART 1: Get to Know the App Template
1. Download the app template.
● The template gives you a head start on building the app.
● Locate the file (MoodTracker_tempate.aia) provided in the folder for this
lesson.
● Download the file to your computer's hard drive. Note where you saved the file.
● Go to App Inventor (http://ai2.appinventor.mit.edu) and import the AIA file
from your computer.
4
What Does It Do? Make Your Prediction
Look over the components in the Designer screen. Can you predict what each
does? (Note: This guide will use these same component names later. Please do not
rename them.)
5
● HorizontalArrangement1 contains three Buttons to track
Happy, Angry, and Sad days in the app:
6
PART 2: Add Charts to Your App
4. Use the Chart component (NEW SKILL)
● In Designer, drag a Chart component
onto your app.
● Drop it inside HorizontalArrangement2
(that empty space up top!):
7
5. Set your charts to a bar graph and pie chart (NEW SKILL)
● In the properties for the chart, click the Type, and choose “bar”:
● Make some adjustments to the design in the properties for the chart.
○ The bar graph needs
some labels. The first
label on the graph
appears under the y-
axis (the line all the
way to the left). To
make everything look
right, make the first
label a 0 by adding
this exact string to
the LabelsFromString
property: “0, Happy,
Angry, Sad”
8
○ Uncheck “LegendEnabled”
for the bar graph. (For the
pie chart, you can ignore this
setting and keep it checked.)
● Add the pie chart. How would you repeat the steps above to add a pie chart to
the empty HorizonalArrangement3?
9
6. Rename the chart
components
to keep track of them
when coding
● Clear names help
you choose the
right components
when you are
coding. Change the
name of Chart1 to
“barGraph”
● Change ChartData2D1
to
“barGraphData2D”
● Do the same for both pie chart components
on the bottom, as shown.
10
● How would you use the
blocks shown below —
including the variable
happyButtonTotal — to
keep a total of how many
times the user has pressed
the button?
● Use what you know from
other App Inventor apps to
do this. Once you are done,
check your work on the next
page.
11
● Your new code should look like this:
12
PART 4: Update the Charts with the User Data
9. Write a procedure to update the charts
● To change the charts, you could
add the same code to all three Remember Procedures?
buttons. But that would be a lot of
duplication. You may have come across them in
● Instead, create procedures! other coding projects — procedures
store a sequence of code under a name.
Instead of having to keep adding the
same long code every time, call the
procedure whenever you want your
● Drag a to procedure do block and drop it into an empty space in your code.
This procedure will “do” all actions to update the charts.
13
10. Add code to update the bar graph (NEW SKILL)
● How will you update the bar graph with the new values in variables
happyButtonTotal, angryButtonTotal, and sadButtonTotal to the bar
graph?
○ First, Clear away any old data in the bar graph.
14
○ Here’s the block — ImportFromList:
15
○ The list will consist of 3 types of data — data for the Happy, Angry, and
Sad categories.
16
■ For the first slot, the
element will define
the placement of
“Happy” on the bar
graph. “Happy” will
be the first bar, so
add a number block
with 1 in this spot.
(Note: The bar graph
starts with a 0 slot before the first element, but the 0 slot appears
over the y-axis, so we don’t notice it.)
○ Using the example above, code the other entries for the Angry and Sad
data (angryButtonTotal and sadButtonTotal).
17
● Did you code your procedure block like this? That’s close!
● That code would certainly work, but the pie chart on the bottom can take
strings for each category in the data. (The bar graph can only use numbers to
identify the category names.)
○ From the text drawer, drag in text blocks.
○ Add the names of the categories (Happy, Angry, Sad):
18
○ The last step is to make all the buttons “call” this procedure every time the
user presses a button. Look in the procedure drawer for a block that would
help.
19
Test your code with App Inventor Companion (version 2.65 or
higher) on your phone or tablet, or by using the emulator. In
the App Inventor menu, go to Connect —> AI Companion or
Connect —> Emulator.
● Click the Happy, Angry, and Sad buttons. The chart and
graph should change (it will appear all black for now. We
will fix the colors next!)
20
12. Fix the chart colors
● An all-black chart seems like a pretty bad user interface
(UI)! What problems do you see for users interpreting
the bar graph in black? How about the pie chart?
● Clearly, color is needed. We will use the blocks in the
template:
● Put colors in the charts when the app starts on your phone or tablet.
○ Add a when Screen1.initialize block:
○ Drag the set barGraphData2D.Colors block and drop it into the when
Screen1.initialize block. This block changes the colors of the bar graph. (Be
careful not to use the barGraphData2D.Color blocks.)
21
○ Drag a set Colors block for the
bottom chart.
○ Drop the make a list blocks from the template into the blocks.
■ Drop the 4-color list into the bar graph (topChartData2D) block. (We
use 4 colors for the bar graph to prevent putting color on top of the y-
axis. The colors correspond to the numbers 0, 1, 2, 3)
■ Drop the 3-color list into the pie chart (bottomChartData2D) block.
22
Test again with the full code with App Inventor Companion
(version 2.65 or higher) on your phone or tablet, or by using the
emulator. In the App Inventor menu, go to Connect —> AI
Companion or Connect —> Emulator.
● Click the Happy, Angry, and Sad buttons. Do they work
as expected? If not, double check your code below:
23
PART 5: Make It Your Own!
● Create your own categories. Replace Happy, Angry, Sad with anything you want to
count. You could have more or less than 3 categories.
● Try other types of data, such as the types of pets in your classroom (“Cat,” “Dog,”
“Snake,” “Other”)?
● Send your data to an online spreadsheet (Check the next tutorial in this series to
learn how to do this).
○ Reasons to send your data online:
■ Keep the data safe in case your phone dies!
■ Perform analysis on the data in a spreadsheet.
■ Share data with others through an app or internet link.
■ With enough data over time, you can create a predictive model to
understand what may happen in the future!
24