React Muze is a React wrapper for Muze, a powerful data visualization library that provides a grammar-of-graphics approach to creating complex, interactive visualizations. It offers flexible components for building sophisticated data visualizations with extensive customization options. This guide walks through advanced usage of React Muze, 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, useRef)
- Familiarity with JavaScript/TypeScript
- Understanding of data visualization concepts
Installation
Install React Muze and Muze core:
npm install react-muze muze
Or with yarn:
yarn add react-muze muze
Or with pnpm:
pnpm add react-muze muze
After installation, your package.json should include:
{
"dependencies": {
"react-muze": "^1.0.0",
"muze": "^1.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Project Setup
React Muze requires minimal setup. Import the components and you're ready to use them.
First Example / Basic Usage
Let's create a simple chart. Create a new file src/ChartExample.jsx:
// src/ChartExample.jsx
import React, { useEffect, useRef } from 'react';
import Muze from 'muze';
import 'muze/dist/muze.css';
function ChartExample() {
const chartRef = useRef(null);
useEffect(() => {
const data = [
{ month: 'Jan', sales: 35 },
{ month: 'Feb', sales: 28 },
{ month: 'Mar', sales: 34 },
{ month: 'Apr', sales: 32 },
{ month: 'May', sales: 40 },
{ month: 'Jun', sales: 32 }
];
const DataModel = Muze.DataModel;
const schema = [
{ name: 'month', type: 'dimension' },
{ name: 'sales', type: 'measure' }
];
const dm = new DataModel(data, schema);
const env = Muze();
const canvas = env.canvas();
if (chartRef.current) {
canvas
.data(dm)
.rows(['sales'])
.columns(['month'])
.mount(chartRef.current);
}
return () => {
if (env) {
env.destroy();
}
};
}, []);
return (
<div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
<h2>Basic Muze Chart Example</h2>
<div ref={chartRef} style={{ 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 chart displaying monthly sales data.
Understanding the Basics
React Muze provides a grammar-of-graphics approach:
- Muze instance: Main visualization engine
- DataModel: Data structure with schema
- Canvas: Chart rendering container
- Rows and columns: Define chart layout
- Grammar of graphics: Declarative visualization approach
Key concepts for advanced usage:
- DataModel: Create data model with schema definition
- Schema: Define dimensions and measures
- Canvas: Configure chart rendering
- Rows/Columns: Define chart structure
- Mount: Render chart to DOM element
Here's an example with multiple chart types:
// src/AdvancedChartExample.jsx
import React, { useEffect, useRef } from 'react';
import Muze from 'muze';
import 'muze/dist/muze.css';
function AdvancedChartExample() {
const chartRef1 = useRef(null);
const chartRef2 = useRef(null);
useEffect(() => {
const lineData = [
{ day: 'Mon', value: 120 },
{ day: 'Tue', value: 132 },
{ day: 'Wed', value: 101 },
{ day: 'Thu', value: 134 },
{ day: 'Fri', value: 90 }
];
const barData = [
{ product: 'Product A', revenue: 120 },
{ product: 'Product B', revenue: 200 },
{ product: 'Product C', revenue: 150 }
];
const lineSchema = [
{ name: 'day', type: 'dimension' },
{ name: 'value', type: 'measure' }
];
const barSchema = [
{ name: 'product', type: 'dimension' },
{ name: 'revenue', type: 'measure' }
];
const DataModel = Muze.DataModel;
const env = Muze();
if (chartRef1.current) {
const dm1 = new DataModel(lineData, lineSchema);
env.canvas()
.data(dm1)
.rows(['value'])
.columns(['day'])
.mount(chartRef1.current);
}
if (chartRef2.current) {
const dm2 = new DataModel(barData, barSchema);
env.canvas()
.data(dm2)
.rows(['revenue'])
.columns(['product'])
.mount(chartRef2.current);
}
return () => {
if (env) {
env.destroy();
}
};
}, []);
return (
<div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
<h2>Advanced Muze Example</h2>
<div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}>
<div>
<h3>Line Chart</h3>
<div ref={chartRef1} style={{ width: '100%', height: '300px' }} />
</div>
<div>
<h3>Bar Chart</h3>
<div ref={chartRef2} style={{ width: '100%', height: '300px' }} />
</div>
</div>
</div>
);
}
export default AdvancedChartExample;
Practical Example / Building Something Real
Let's build a comprehensive analytics dashboard:
// src/AnalyticsDashboard.jsx
import React, { useEffect, useRef, useState } from 'react';
import Muze from 'muze';
import 'muze/dist/muze.css';
function AnalyticsDashboard() {
const [selectedPeriod, setSelectedPeriod] = useState('monthly');
const chartRef1 = useRef(null);
const chartRef2 = useRef(null);
const chartRef3 = useRef(null);
useEffect(() => {
const monthlyData = [
{ month: 'Jan', sales: 35, target: 40 },
{ month: 'Feb', sales: 28, target: 35 },
{ month: 'Mar', sales: 34, target: 38 },
{ month: 'Apr', sales: 32, target: 36 },
{ month: 'May', sales: 40, target: 42 },
{ month: 'Jun', sales: 32, target: 38 }
];
const quarterlyData = [
{ quarter: 'Q1', sales: 97, target: 113 },
{ quarter: 'Q2', sales: 106, target: 116 },
{ quarter: 'Q3', sales: 112, target: 120 },
{ quarter: 'Q4', sales: 120, target: 125 }
];
const revenueData = [
{ quarter: 'Q1', revenue: 12000 },
{ quarter: 'Q2', revenue: 15000 },
{ quarter: 'Q3', revenue: 18000 },
{ quarter: 'Q4', revenue: 20000 }
];
const categoryData = [
{ product: 'Product A', value: 35 },
{ product: 'Product B', value: 28 },
{ product: 'Product C', value: 34 },
{ product: 'Product D', value: 32 }
];
const currentData = selectedPeriod === 'monthly' ? monthlyData : quarterlyData;
const dimensionField = selectedPeriod === 'monthly' ? 'month' : 'quarter';
const salesSchema = [
{ name: dimensionField, type: 'dimension' },
{ name: 'sales', type: 'measure' },
{ name: 'target', type: 'measure' }
];
const revenueSchema = [
{ name: 'quarter', type: 'dimension' },
{ name: 'revenue', type: 'measure' }
];
const categorySchema = [
{ name: 'product', type: 'dimension' },
{ name: 'value', type: 'measure' }
];
const DataModel = Muze.DataModel;
const env = Muze();
if (chartRef1.current) {
const dm1 = new DataModel(currentData, salesSchema);
env.canvas()
.data(dm1)
.rows(['sales', 'target'])
.columns([dimensionField])
.mount(chartRef1.current);
}
if (chartRef2.current) {
const dm2 = new DataModel(revenueData, revenueSchema);
env.canvas()
.data(dm2)
.rows(['revenue'])
.columns(['quarter'])
.mount(chartRef2.current);
}
if (chartRef3.current) {
const dm3 = new DataModel(categoryData, categorySchema);
env.canvas()
.data(dm3)
.rows(['value'])
.columns(['product'])
.mount(chartRef3.current);
}
return () => {
if (env) {
env.destroy();
}
};
}, [selectedPeriod]);
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' }}>
<div>
<h2>Sales vs Target</h2>
<div ref={chartRef1} style={{ width: '100%', height: '400px' }} />
</div>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px' }}>
<div>
<h2>Quarterly Revenue</h2>
<div ref={chartRef2} style={{ width: '100%', height: '400px' }} />
</div>
<div>
<h2>Product Distribution</h2>
<div ref={chartRef3} style={{ width: '100%', height: '400px' }} />
</div>
</div>
</div>
</div>
);
}
export default AnalyticsDashboard;
Update your App.jsx:
// src/App.jsx
import React from 'react';
import AnalyticsDashboard from './AnalyticsDashboard';
import './App.css';
function App() {
return (
<div className="App">
<AnalyticsDashboard />
</div>
);
}
export default App;
This example demonstrates:
- Analytics dashboard
- Multiple chart types
- Dynamic data updates
- Interactive features
- Grammar-of-graphics approach
- Custom styling
Common Issues / Troubleshooting
Chart not displaying: Make sure you've imported Muze CSS. Check that the DataModel schema matches your data structure.
Data format error: Ensure data is an array of objects. Define schema with correct dimension and measure types.
Schema errors: Use 'dimension' for categorical data and 'measure' for numeric data. Check that schema field names match data property names.
Mounting issues: Use useRef to get DOM element reference. Mount chart after component mounts using useEffect.
Memory leaks: Clean up Muze instances in useEffect return function. Call env.destroy() to prevent memory leaks.
Performance: For large datasets, consider data aggregation or sampling. Muze handles large datasets well, but optimization may be needed for very large datasets.
Next Steps
Now that you have an advanced understanding of React Muze:
- Explore all available chart types
- Learn about advanced grammar-of-graphics configurations
- Implement custom visualizations
- Add interactive features
- Create complex dashboards
- Learn about other visualization libraries
- Check the official repository: https://github.com/chartshq/react-muze
Summary
You've successfully integrated React Muze into your React application with advanced features including analytics dashboards, interactive charts, and complex visualizations. React Muze provides a powerful, grammar-of-graphics approach to creating data visualizations with extensive customization options.
SEO Keywords
react-muze
React Muze
react-muze tutorial
React data visualization
react-muze installation
React chart library
react-muze example
React grammar of graphics
react-muze setup
React interactive charts
react-muze customization
React chart component
react-muze dashboard
React visualization library
react-muze getting started

Top comments (0)