Charts, Diagrams, and Infographics
Summary
This chapter introduces specialized JavaScript libraries for creating charts, diagrams, and infographics. You will learn about p5.js MicroSims as the most flexible option, then explore Mermaid.js for declarative diagrams including flowcharts and process diagrams. The chapter covers Chart.js for data visualization with bar charts, line charts, pie charts, and bubble charts. You will learn about different chart types and how to match data types to appropriate visualization methods, plus techniques for adding tooltips and creating informative infographics.
Concepts Covered
This chapter covers the following 23 concepts from the learning graph:
- p5.js MicroSim
- Mermaid.js Diagrams
- Process Diagrams
- Flowcharts
- Chart.js Library
- Bar Charts
- Pie Charts
- Line Charts
- Other MicroSim Libraries
- General vs. Specialized
- Mermaid
- Declarative Layout
- Infographics
- Flowchart
- Workflow
- ToolTips
- ChartJS
- Chart Types
- Bar Chart
- Line Chart
- Bubble Chart
- Other Chart Types
- Data Type to Chart Types
Prerequisites
This chapter builds on concepts from:
Beyond p5.js: No One Size Fits All
Despite the remarkable power and flexibility of p5.js, there is no "one size fits all" rule in generating MicroSims. Sometimes you need a simple bar chart, not a custom physics simulation. Sometimes a flowchart communicates better than an animated visualization. Sometimes an infographic—a carefully designed visual that connects components with understanding—is exactly what your learning objective demands.
In this chapter, we explore the world beyond p5.js: specialized JavaScript libraries designed for specific visualization tasks. You'll learn how simple charts work and discover how the type of data you want to present will guide your chart selection. The right tool for the job makes your MicroSims more effective and your development faster.
The Right Tool Principle
Choosing a specialized library over p5.js isn't a compromise—it's smart design. Chart.js creates better charts than hand-coded p5.js. Mermaid.js creates better flowcharts. Use each tool where it excels.
General vs. Specialized Libraries
Understanding when to use general-purpose versus specialized libraries is a key design decision.
The Spectrum
| Library Type | Examples | Best For | Trade-offs |
|---|---|---|---|
| General-purpose | p5.js, D3.js | Custom visualizations, unique interactions | More code, more control |
| Specialized | Chart.js, Mermaid.js | Specific chart/diagram types | Less code, less customization |
| Hybrid | Plotly.js, vis.js | Charts with custom features | Balance of both |
When to Choose Each
Choose p5.js (general-purpose) when:
- Building physics simulations or games
- Need pixel-level control over rendering
- Creating unique, non-standard visualizations
- Combining multiple visualization types
- Real-time animation is critical
Choose specialized libraries when:
- Creating standard chart types (bar, line, pie)
- Need built-in interactivity (tooltips, zooming)
- Want to minimize development time
- Data updates dynamically
- Consistency with web standards matters
p5.js MicroSim: The Flexible Foundation
A p5.js MicroSim remains your most flexible option. With p5.js, you can build virtually any visualization—but you write all the code yourself.
p5.js Strengths
- Complete control over every pixel
- Built-in animation loop (
draw()) - Canvas-based rendering (fast)
- Large community and examples
- Works standalone or embedded
p5.js Limitations
- No built-in chart types
- Tooltip handling is manual
- Responsive design requires custom code
- Accessibility requires extra work
When p5.js Is the Right Choice
Use p5.js for MicroSims when you need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
This physics simulation would be difficult to achieve with Chart.js or Mermaid—p5.js is clearly the right tool.
Mermaid.js: Declarative Diagrams
Mermaid.js creates diagrams from text descriptions using a declarative layout approach. Instead of specifying pixel positions, you describe relationships—and Mermaid figures out how to draw them.
What Is Declarative Layout?
Declarative layout means you describe what you want, not how to draw it. Compare:
| Approach | Description | Example |
|---|---|---|
| Imperative (p5.js) | "Draw box at (100, 50), draw arrow from (150, 75) to (200, 75)..." | Full control, tedious |
| Declarative (Mermaid) | "A connects to B, B connects to C" | Automatic positioning |
Mermaid Diagram Types
Mermaid supports many diagram types relevant to educational content:
| Diagram Type | Use Case | Mermaid Syntax Start |
|---|---|---|
| Flowchart | Processes, decisions, algorithms | flowchart TD |
| Sequence | Interactions over time | sequenceDiagram |
| Class | Object relationships | classDiagram |
| State | State machines | stateDiagram-v2 |
| Entity-Relationship | Database schemas | erDiagram |
| Gantt | Project timelines | gantt |
| Pie | Simple proportions | pie |
Mermaid Flowchart Syntax
A flowchart shows processes and decisions as connected nodes:
1 2 3 4 5 6 7 | |
Syntax breakdown:
flowchart TD- Top-down direction (also: LR, BT, RL)A[Text]- Rectangle node with ID "A"B{Text}- Diamond (decision) nodeA --> B- Arrow connectionB -->|Label| C- Labeled connection
Process Diagrams
Process diagrams show sequential steps in a workflow:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Process diagrams use subgraph to group related steps.
Workflow Diagrams
A workflow diagram models business or system processes:
1 2 3 4 5 6 7 8 9 | |
Mermaid in MicroSims
To embed Mermaid in a MicroSim, include the library and use a container:
1 2 3 4 5 6 7 8 9 | |
Diagram: Mermaid Flowchart Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
Chart.js: Data Visualization Made Easy
Chart.js (also called ChartJS) is a popular library for creating standard chart types with minimal code. It handles rendering, animations, tooltips, and responsiveness automatically.
Chart.js Features
- 8+ built-in chart types
- Responsive by default
- Animated transitions
- Interactive tooltips
- Legend management
- Accessible (ARIA labels)
- Small file size (~60KB)
Basic Chart.js Setup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Chart Types Overview
Chart.js supports these chart types:
| Chart Type | Data Pattern | Best For |
|---|---|---|
| Bar | Categories with values | Comparing discrete items |
| Line | Values over time | Trends, changes |
| Pie/Doughnut | Parts of a whole | Proportions, percentages |
| Radar | Multiple dimensions | Profile comparisons |
| Scatter | X-Y pairs | Correlations |
| Bubble | X-Y pairs with size | Three-variable relationships |
| Polar Area | Categories with magnitude | Cyclical data |
Bar Charts: Comparing Categories
Bar charts display categorical data with rectangular bars. Bar height (or length) represents the value.
When to Use Bar Charts
- Comparing values across categories
- Showing rankings
- Displaying discrete, non-continuous data
- When labels are long (use horizontal bars)
Bar Chart Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Grouped and Stacked Bar Charts
Grouped bars compare multiple datasets:
1 2 3 4 | |
Stacked bars show totals and composition:
1 2 3 4 5 6 | |
Diagram: Interactive Bar Chart MicroSim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Line Charts: Trends Over Time
Line charts connect data points with lines, emphasizing trends and changes over continuous scales (usually time).
When to Use Line Charts
- Showing change over time
- Displaying trends
- Comparing multiple series over same time period
- Highlighting rates of change
Line Chart Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Line Chart Variations
| Variation | Setting | Effect |
|---|---|---|
| Smooth lines | tension: 0.4 |
Curved connections |
| Area fill | fill: true |
Shaded area below line |
| Stepped | stepped: true |
Stair-step pattern |
| No points | pointRadius: 0 |
Line only |
Pie Charts: Parts of a Whole
Pie charts show proportions as slices of a circle. The entire pie represents 100%.
When to Use Pie Charts
- Showing parts of a whole
- When you have 2-7 categories
- When proportions are the message
- For simple, high-level overviews
Pie Chart Caution
Humans struggle to compare slice angles. If precise comparison matters, use a bar chart instead. Pie charts work best for showing obvious majorities or rough proportions.
Pie Chart Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Doughnut Charts
A doughnut chart is a pie chart with a hole in the center—useful for placing summary statistics:
1 2 3 4 | |
Bubble Charts: Three Variables at Once
Bubble charts extend scatter plots by adding a third dimension: bubble size. Each point shows X position, Y position, and magnitude (size).
When to Use Bubble Charts
- Comparing three numerical variables
- Showing clusters and outliers
- Displaying magnitude alongside position
- Creating impact/effort matrices
Bubble Chart Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Bubble Chart Applications
| Application | X-Axis | Y-Axis | Size |
|---|---|---|---|
| Project prioritization | Effort | Impact | Cost |
| Market analysis | Price | Quality | Market share |
| Student performance | Time spent | Score | Engagement |
| Risk assessment | Probability | Impact | Cost of mitigation |
Diagram: Priority Matrix Bubble Chart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
Other Chart Types
Beyond the core types, Chart.js and other libraries support additional visualizations:
Radar Charts
Radar charts (spider charts) display multiple variables as axes radiating from a center:
1 2 3 4 5 6 7 8 | |
Best for: Comparing profiles, showing skill assessments, displaying multi-attribute items.
Scatter Charts
Scatter charts show correlation between two variables:
1 2 3 4 5 6 7 8 9 | |
Best for: Correlation analysis, identifying clusters, regression visualization.
Polar Area Charts
Polar area charts combine pie and bar concepts—each slice has equal angle but variable radius:
1 2 3 4 5 6 7 | |
Best for: Cyclical data (days, months), comparing magnitudes across categories.
Data Type to Chart Type: The Selection Guide
Choosing the right chart type depends on your data type and what you want to communicate. This guide maps data patterns to appropriate visualizations.
The Decision Matrix
| Your Data Pattern | Best Chart Types | Avoid |
|---|---|---|
| Categories (discrete items) | Bar, Pie, Polar Area | Line, Scatter |
| Time series (values over time) | Line, Area, Bar | Pie, Radar |
| Parts of whole (percentages) | Pie, Doughnut, Stacked Bar | Line, Scatter |
| Correlation (X-Y relationship) | Scatter, Bubble | Pie, Bar |
| Distribution (spread of values) | Histogram, Box Plot | Pie |
| Comparison (multiple series) | Grouped Bar, Multi-line | Single Pie |
| Hierarchy (nested categories) | Treemap, Sunburst | Line, Scatter |
| Flow/Process | Sankey, Flowchart | Bar, Pie |
| Geographic | Map, Choropleth | Bar, Line |
| Multi-dimensional (3+ variables) | Bubble, Radar, Parallel | Pie, Simple Bar |
Data Type Characteristics
| Data Type | Characteristics | Example |
|---|---|---|
| Categorical | Named groups, no order | Colors, countries, products |
| Ordinal | Named groups, ordered | Grades (A,B,C), satisfaction levels |
| Continuous | Numerical, any value | Temperature, price, time |
| Discrete | Numerical, specific values | Count of items, year |
Quick Selection Questions
Answer these questions to choose your chart:
- Are you comparing categories? → Bar chart
- Showing change over time? → Line chart
- Showing parts of a whole? → Pie (few parts) or Stacked Bar (many parts)
- Showing correlation between variables? → Scatter or Bubble
- Comparing profiles across dimensions? → Radar chart
- Showing a process or flow? → Flowchart (Mermaid)
Diagram: Chart Selection Flowchart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Tooltips: Adding Context on Demand
Tooltips are small information boxes that appear when users hover over chart elements. They provide details without cluttering the visualization.
Why Tooltips Matter
- Show exact values without label clutter
- Provide additional context
- Enable exploration of dense data
- Improve accessibility
Chart.js Tooltip Customization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Tooltip Callbacks
| Callback | When Called | Use For |
|---|---|---|
title |
Tooltip title | Category name |
beforeLabel |
Before value | Context info |
label |
Main value | Formatted data |
afterLabel |
After value | Additional info |
footer |
Bottom of tooltip | Calculations, notes |
Mermaid Click Actions
Mermaid supports click interactions for nodes:
1 2 3 4 | |
Infographics: Visual Understanding
Infographics are designed visuals that combine data, graphics, and text to communicate complex information clearly. Unlike charts (which display data) or diagrams (which show relationships), infographics tell a story.
Characteristics of Good Infographics
| Characteristic | Description |
|---|---|
| Focused message | One main point or story |
| Visual hierarchy | Important elements stand out |
| Minimal text | Words support visuals, not replace them |
| Consistent style | Unified color palette and typography |
| Flow | Clear reading path through content |
| Data integrity | Accurate, not misleading |
Infographic Types
- Statistical infographics - Charts with supporting visuals and annotations
- Informational infographics - Explanatory content with icons and sections
- Timeline infographics - Chronological events with visual markers
- Process infographics - Step-by-step guides with icons
- Comparison infographics - Side-by-side visual comparisons
- Geographic infographics - Map-based data visualization
Building Infographics as MicroSims
Infographics can be interactive MicroSims with:
- Hover effects revealing additional data
- Click interactions for drill-down
- Animated transitions between sections
- Responsive layout adaptation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Diagram: Interactive Infographic MicroSim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
Other MicroSim Libraries
Beyond p5.js, Chart.js, and Mermaid, several other libraries serve specialized purposes:
Visualization Libraries
| Library | Specialty | When to Use |
|---|---|---|
| D3.js | Custom data visualization | Complex, unique visualizations |
| Three.js | 3D graphics | 3D models, virtual environments |
| Plotly.js | Scientific charts | Interactive scientific plots |
| vis.js | Networks and timelines | Graph visualization, scheduling |
| Leaflet | Maps | Geographic data |
| Vega-Lite | Grammar of graphics | Specification-based charts |
When to Venture Beyond
Consider other libraries when you need:
- D3.js - Complete control over data-driven documents
- Three.js - 3D molecular models, architectural visualization
- Plotly.js - Publication-quality scientific charts
- vis.js - Interactive network graphs (like learning graphs)
- Leaflet - Interactive maps with markers and layers
Library Selection Checklist
Before choosing a library, consider:
- [ ] Does it support my chart/diagram type?
- [ ] Is the file size acceptable for my use case?
- [ ] Does it handle responsiveness?
- [ ] Are tooltips and interactions built in?
- [ ] How steep is the learning curve?
- [ ] Is it actively maintained?
- [ ] Does it work in my target browsers?
Complete Example: Multi-Library MicroSim
Here's a MicroSim that combines multiple visualization approaches:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Key Takeaways
You've learned to select and use specialized visualization libraries for educational MicroSims:
-
p5.js MicroSims offer maximum flexibility but require more code.
-
General vs. specialized libraries present trade-offs between control and convenience.
-
Mermaid.js uses declarative layout for diagrams without positioning code.
-
Flowcharts and process diagrams show decisions and sequential steps.
-
Workflow diagrams model business and system processes.
-
Chart.js (ChartJS) provides built-in chart types with minimal configuration.
-
Bar charts compare categories; use them for discrete data comparisons.
-
Line charts show trends over time; use them for continuous data.
-
Pie charts show parts of a whole; limit to 2-7 clear categories.
-
Bubble charts display three variables using position and size.
-
Data type to chart type mapping guides visualization selection.
-
Tooltips add context on demand without visual clutter.
-
Infographics combine data, graphics, and text to tell stories.
-
Other libraries (D3, Three.js, Plotly, vis.js) serve specialized needs.
-
Choose the right tool for the job—specialized libraries often outperform general-purpose code.
Challenge: Match Data to Visualization
You have three datasets: (1) monthly sales figures for a year, (2) market share percentages for 5 competitors, (3) the relationship between advertising spend and revenue. For each dataset, select the most appropriate chart type and explain why. Then identify which library you would use.
Next Steps
You now have a toolkit of visualization options beyond p5.js. You can create flowcharts with Mermaid, data charts with Chart.js, and know when to reach for specialized libraries. In the next chapter, we'll explore physics and animation, learning how to add realistic motion, collisions, and dynamic behavior to your MicroSims.
Remember: the best visualization is the one that communicates your message clearly. Sometimes that's a custom p5.js animation. Sometimes it's a simple bar chart. Let your data and learning objectives guide the choice.
References
- Chart.js Documentation - Official Chart.js documentation
- Mermaid.js Live Editor - Interactive Mermaid diagram editor
- Data Visualization Catalogue - Comprehensive chart type guide
- D3.js Gallery - D3.js examples and tutorials
- From Data to Viz - Decision tree for chart selection