React Google Charts is a React wrapper for Google Charts, providing a React-friendly API for creating interactive, responsive charts. It offers access to all Google Charts types with extensive customization options and real-time data updates. This guide walks through advanced usage of React Google Charts, including complex chart configurations, interactive features, and dashboard implementations.
Prerequisites
Before you begin, make sure you have:
- Node.js version 14.0 or higher installed
- npm, yarn, or pnpm package manager
- A React project (version 16.8 or higher) or create-react-app setup
- Basic knowledge of React hooks (useState, useEffect)
- Familiarity with JavaScript/TypeScript
- Understanding of Google Charts API
Installation
Install React Google Charts using your preferred package manager:
npm install react-google-charts
Or with yarn:
yarn add react-google-charts
Or with pnpm:
pnpm add react-google-charts
After installation, your package.json should include:
{
"dependencies": {
"react-google-charts": "^4.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Project Setup
React Google Charts requires minimal setup. Import the components and you're ready to use them.
First Example / Basic Usage
Let's create a simple line chart. Create a new file src/ChartExample.jsx:
// src/ChartExample.jsx
import React from 'react';
import { Chart } from 'react-google-charts';
function ChartExample() {
const data = [
['Month', 'Sales'],
['Jan', 35],
['Feb', 28],
['Mar', 34],
['Apr', 32],
['May', 40],
['Jun', 32]
];
const options = {
title: 'Monthly Sales',
hAxis: { title: 'Month' },
vAxis: { title: 'Sales' },
legend: 'none'
};
return (
<div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
<h2>Basic Google Charts Example</h2>
<Chart
chartType="LineChart"
data={data}
options={options}
width="100%"
height="400px"
/>
</div>
);
}
export default ChartExample;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import ChartExample from './ChartExample';
import './App.css';
function App() {
return (
<div className="App">
<ChartExample />
</div>
);
}
export default App;
This creates a basic line chart with monthly sales data.
Understanding the Basics
React Google Charts provides a Chart component:
- Chart component: Main chart component
- chartType prop: Type of chart (LineChart, BarChart, PieChart, etc.)
- Data format: Array of arrays, first row is headers
- Options: Chart configuration object
- Interactive: Built-in interactivity
Key concepts for advanced usage:
- Data structure: First row contains column headers, subsequent rows contain data
- Chart types: LineChart, BarChart, PieChart, AreaChart, etc.
- Options object: Configure chart appearance and behavior
- Events: Handle chart events with callbacks
- Real-time updates: Update data dynamically
Here's an example with multiple chart types:
// src/AdvancedChartExample.jsx
import React from 'react';
import { Chart } from 'react-google-charts';
function AdvancedChartExample() {
const lineData = [
['Month', 'Sales', 'Target'],
['Jan', 35, 40],
['Feb', 28, 35],
['Mar', 34, 38],
['Apr', 32, 36],
['May', 40, 42],
['Jun', 32, 38]
];
const barData = [
['Quarter', 'Revenue'],
['Q1', 12000],
['Q2', 15000],
['Q3', 18000],
['Q4', 20000]
];
const pieData = [
['Product', 'Sales'],
['Product A', 35],
['Product B', 28],
['Product C', 34],
['Product D', 32]
];
const lineOptions = {
title: 'Sales vs Target',
hAxis: { title: 'Month' },
vAxis: { title: 'Sales' },
legend: { position: 'top' }
};
const barOptions = {
title: 'Quarterly Revenue',
hAxis: { title: 'Quarter' },
vAxis: { title: 'Revenue' }
};
const pieOptions = {
title: 'Product Distribution',
pieHole: 0.4
};
return (
<div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
<h2>Advanced Google Charts Example</h2>
<div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}>
<Chart
chartType="LineChart"
data={lineData}
options={lineOptions}
width="100%"
height="400px"
/>
<Chart
chartType="BarChart"
data={barData}
options={barOptions}
width="100%"
height="400px"
/>
<Chart
chartType="PieChart"
data={pieData}
options={pieOptions}
width="100%"
height="400px"
/>
</div>
</div>
);
}
export default AdvancedChartExample;
Practical Example / Building Something Real
Let's build a comprehensive analytics dashboard:
// src/AnalyticsDashboard.jsx
import React, { useState } from 'react';
import { Chart } from 'react-google-charts';
function AnalyticsDashboard() {
const [selectedPeriod, setSelectedPeriod] = useState('monthly');
const monthlyData = [
['Month', 'Sales', 'Target'],
['Jan', 35, 40],
['Feb', 28, 35],
['Mar', 34, 38],
['Apr', 32, 36],
['May', 40, 42],
['Jun', 32, 38]
];
const quarterlyData = [
['Quarter', 'Sales', 'Target'],
['Q1', 97, 113],
['Q2', 106, 116],
['Q3', 112, 120],
['Q4', 120, 125]
];
const revenueData = [
['Quarter', 'Revenue'],
['Q1', 12000],
['Q2', 15000],
['Q3', 18000],
['Q4', 20000]
];
const categoryData = [
['Product', 'Sales'],
['Product A', 35],
['Product B', 28],
['Product C', 34],
['Product D', 32]
];
const currentData = selectedPeriod === 'monthly' ? monthlyData : quarterlyData;
const lineOptions = {
title: 'Sales Performance',
hAxis: { title: selectedPeriod === 'monthly' ? 'Month' : 'Quarter' },
vAxis: { title: 'Sales' },
legend: { position: 'top' },
curveType: 'function'
};
const barOptions = {
title: 'Quarterly Revenue',
hAxis: { title: 'Quarter' },
vAxis: { title: 'Revenue' }
};
const pieOptions = {
title: 'Product Distribution',
pieHole: 0.4,
is3D: true
};
return (
<div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
<h1>Analytics Dashboard</h1>
<div style={{ marginBottom: '20px', textAlign: 'center' }}>
<button
onClick={() => setSelectedPeriod('monthly')}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: selectedPeriod === 'monthly' ? '#007bff' : '#6c757d',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
Monthly
</button>
<button
onClick={() => setSelectedPeriod('quarterly')}
style={{
padding: '8px 16px',
margin: '0 5px',
backgroundColor: selectedPeriod === 'quarterly' ? '#007bff' : '#6c757d',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer'
}}
>
Quarterly
</button>
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}>
<Chart
chartType="LineChart"
data={currentData}
options={lineOptions}
width="100%"
height="400px"
/>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px' }}>
<Chart
chartType="BarChart"
data={revenueData}
options={barOptions}
width="100%"
height="400px"
/>
<Chart
chartType="PieChart"
data={categoryData}
options={pieOptions}
width="100%"
height="400px"
/>
</div>
</div>
</div>
);
}
export default AnalyticsDashboard;
Now create an interactive chart with events:
// src/InteractiveChart.jsx
import React, { useState } from 'react';
import { Chart } from 'react-google-charts';
function InteractiveChart() {
const [selectedData, setSelectedData] = useState(null);
const data = [
['Month', 'Sales'],
['Jan', 35],
['Feb', 28],
['Mar', 34],
['Apr', 32],
['May', 40],
['Jun', 32]
];
const options = {
title: 'Interactive Sales Chart',
hAxis: { title: 'Month' },
vAxis: { title: 'Sales' },
legend: 'none'
};
const chartEvents = [
{
eventName: 'select',
callback: ({ chartWrapper }) => {
const chart = chartWrapper.getChart();
const selection = chart.getSelection();
if (selection.length > 0) {
const selectedRow = selection[0].row + 1;
setSelectedData(data[selectedRow]);
}
}
}
];
return (
<div style={{ padding: '20px', maxWidth: '900px', margin: '0 auto' }}>
<h2>Interactive Chart</h2>
{selectedData && (
<div style={{
padding: '10px',
backgroundColor: '#f8f9fa',
borderRadius: '4px',
marginBottom: '20px'
}}>
Selected: {selectedData[0]} - {selectedData[1]}
</div>
)}
<Chart
chartType="LineChart"
data={data}
options={options}
width="100%"
height="500px"
chartEvents={chartEvents}
/>
</div>
);
}
export default InteractiveChart;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import AnalyticsDashboard from './AnalyticsDashboard';
import InteractiveChart from './InteractiveChart';
import './App.css';
function App() {
return (
<div className="App">
<AnalyticsDashboard />
<InteractiveChart />
</div>
);
}
export default App;
This example demonstrates:
- Analytics dashboard
- Multiple chart types
- Interactive features
- Event handling
- Dynamic data updates
- Custom styling
Common Issues / Troubleshooting
Chart not displaying: Make sure you're importing
Chartcorrectly from'react-google-charts'. Check that the data array is properly formatted with headers.Data format error: Ensure data follows the correct format: first row contains headers, subsequent rows contain data values.
Options not working: Check that options object follows Google Charts configuration format. Refer to Google Charts documentation for correct option structure.
Events not firing: Use the
chartEventsprop to attach event handlers. Make sure event names match Google Charts event names.Styling issues: Customize colors and styles in the options object. Use
colorsarray for custom color schemes.Performance: For large datasets, consider data sampling or pagination. Google Charts handles large datasets well, but optimization may be needed for very large datasets.
Next Steps
Now that you have an advanced understanding of React Google Charts:
- Explore all available chart types
- Learn about advanced configurations
- Implement real-time data updates
- Add custom tooltips and formatters
- Create combo charts
- Learn about other chart libraries
- Check the official repository: https://github.com/RakanNimer/react-google-charts
Summary
You've successfully integrated React Google Charts into your React application with advanced features including analytics dashboards, interactive charts, and event handling. React Google Charts provides a powerful solution for creating professional data visualizations with extensive customization options.
SEO Keywords
react-google-charts
React Google Charts
react-google-charts tutorial
React data visualization
react-google-charts installation
React chart library
react-google-charts example
React Google Charts dashboard
react-google-charts setup
React interactive charts
react-google-charts customization
React chart component
react-google-charts events
React chart visualization
react-google-charts getting started
Top comments (0)