NewPlatano

Master React Native Charts | Tutorial for Dynamic Data Visualization

Beto, June 27, 2024 · 21,820 views

Learn how to build beautiful, dynamic charts in React Native using the react-native-gifted-charts library. It focuses on integrating chart data from a local SQLite database, making it practical for apps that track income and expenses over time.

You'll see how to fetch and display weekly data with smooth animations and color gradients that change based on values. This tutorial is ideal if you want to visualize financial data or any time-series data in your React Native app with a modern, well-maintained charting library.

What's inside

  • Introduction to the budget app and SQLite data source
  • Generating and managing test data with mockaroo and TablePlus
  • Installing and configuring react-native-gifted-charts and dependencies
  • Setting up the SQLite database and inspecting data
  • Creating the SummaryChart component with dynamic data
  • Adding linear gradients and conditional colors to bars
  • Animating chart transitions and handling user interaction
  • Integrating the chart into the home screen

Introduction to the budget app and SQLite data source

I use a budget app previously built on this channel as the base project. This app tracks income and expenses stored locally in a SQLite database on the device. The chart will visualize this financial data dynamically.

Using SQLite allows the app to work offline and manage persistent data efficiently. Although the example uses SQLite, the approach applies to any data source you want to visualize in React Native charts.

Generating and managing test data with mockaroo and TablePlus

To test the chart with realistic data, I demonstrate generating 1,000 rows of mock transaction data using Mockaroo, a web tool for creating database inserts. This saves time compared to manual data entry.

TablePlus is used as a database management tool to inspect and run SQL queries on the SQLite database. It supports multiple database types and provides a GUI to view tables and data, making it easier to verify the inserted transactions.

Installing and configuring react-native-gifted-charts and dependencies

The charting library used is react-native-gifted-charts, a popular and actively maintained React Native chart library. Installation requires adding react-native-gifted-charts plus peer dependencies: expo-linear-gradient for gradients and react-native-svg for SVG rendering.

I cover stopping the Metro server, installing the packages, and rebuilding the app to ensure the native dependencies link correctly. This setup is essential for the chart animations and gradient features to work.

Setting up the SQLite database and inspecting data

The SQLite database file lives locally on the device and contains a transactions table with fields like id, categoryId, amount, date (stored as seconds), description, and type (income or expense).

Using TablePlus or a SQLite viewer extension, you can open the database file, browse the transactions, and verify the data structure. This step confirms the app has the necessary data to feed into the charts.

Creating the SummaryChart component with dynamic data

A new React Native component named SummaryChart is created to encapsulate the chart UI. I show how to import React and React Native components, then set up a basic view with placeholder text.

This component will later fetch and process the SQLite data to display weekly income or expense summaries. Keeping the chart logic separate makes the code modular and easier to maintain.

Adding linear gradients and conditional colors to bars

The chart bars use linear gradients for a polished look. I explain how to apply gradients using expo-linear-gradient and how to conditionally change bar colors based on their values.

For example, bars representing amounts less than 100 are colored green, while others use different colors. This visual cue helps users quickly interpret the data significance.

Animating chart transitions and handling user interaction

Smooth animations are implemented when switching between income and expense tabs or navigating through weeks. The react-native-gifted-charts library supports these animations out of the box.

I also mention the possibility of extending interactivity, such as displaying detailed info when pressing a bar or navigating to a modal screen, though this is left as an exercise.

Integrating the chart into the home screen

Finally, the SummaryChart component is imported and used in the app’s single home screen. I show how to comment out unrelated UI elements temporarily and focus on the chart display.

This integration demonstrates how to replace or complement existing transaction lists with a dynamic visual summary, improving the app’s user experience.

Resources

CourseReact Native course

Let's connect!

Had a win? Get featured on Code with Beto.Share your story