);
return (
);
}
```
--------------------------------
### React GlobalChartsProvider for Theming
Source: https://context7.com/automattic/charts/llms.txt
Illustrates how to use the GlobalChartsProvider to apply a consistent theme across multiple chart components in a React application. This component requires React and imports from @automattic/charts. It accepts a theme object with customizable properties for colors, spacing, and specific chart styles. The provider wraps child components that render charts like LineChart and BarChart.
```javascript
import React from 'react';
import { GlobalChartsProvider } from '@automattic/charts/providers';
import { LineChart } from '@automattic/charts/line-chart';
import { BarChart } from '@automattic/charts/bar-chart';
function Dashboard() {
const customTheme = {
backgroundColor: '#ffffff',
colors: ['#0073aa', '#00a32a', '#f0b849', '#826eb4', '#d63638'],
gridColor: '#e0e0e0',
gridColorDark: '#333333',
tickLength: 8,
legendLabelStyles: { fontSize: '14px', fontWeight: 500 },
conversionFunnelChart: {
primaryColor: '#0073aa',
backgroundColor: '#f9f9f9',
positiveChangeColor: '#00a32a',
negativeChangeColor: '#d63638',
},
leaderboardChart: {
rowGap: 16,
columnGap: 12,
primaryColor: '#0073aa',
secondaryColor: '#cccccc',
deltaColors: ['#d63638', '#666666', '#00a32a'],
},
};
const lineData = [
{
label: 'Sales',
data: [
{ date: new Date('2024-01-01'), value: 100 },
{ date: new Date('2024-01-02'), value: 150 },
{ date: new Date('2024-01-03'), value: 120 },
],
},
];
const barData = [
{
label: 'Revenue',
data: [
{ label: 'Jan', value: 5000 },
{ label: 'Feb', value: 6200 },
{ label: 'Mar', value: 5800 },
],
},
];
return (
);
}
```
--------------------------------
### Import visx components via @automattic/charts/visx
Source: https://github.com/automattic/charts/blob/trunk/src/visx/README.md
Demonstrates how to import visx components through the @automattic/charts/visx module instead of directly from visx packages. This approach simplifies imports and manages dependencies.
```javascript
import { LineShape } from '@automattic/charts/visx/legend';
// Instead of: import { LineShape } from '@visx/legend'
import { Text } from '@automattic/charts/visx/text';
// Instead of: import { Text } from '@visx/text'
```
--------------------------------
### Create Accessible Tooltip with Keyboard Navigation in React
Source: https://context7.com/automattic/charts/llms.txt
This snippet demonstrates how to create a custom tooltip component using React, leveraging the AccessibleTooltip and useKeyboardNavigation hooks from '@automattic/charts/tooltip'. It handles mouse events for showing/hiding tooltips and keyboard events for navigating and selecting data points, ensuring accessibility. The component requires React and the '@automattic/charts/tooltip' package.
```javascript
import React, { useState } from 'react';
import { AccessibleTooltip, useKeyboardNavigation } from '@automattic/charts/tooltip';
import '@automattic/charts/tooltip/style.css';
function CustomTooltipExample() {
const [tooltipData, setTooltipData] = useState(null);
const [showTooltip, setShowTooltip] = useState(false);
const dataPoints = [
{ id: 'point-1', label: 'January', value: 100, x: 50, y: 150 },
{ id: 'point-2', label: 'February', value: 150, x: 150, y: 100 },
{ id: 'point-3', label: 'March', value: 120, x: 250, y: 130 },
];
const { handleKeyDown, focusIndex } = useKeyboardNavigation({
itemCount: dataPoints.length,
onItemSelect: (index) => {
setTooltipData(dataPoints[index]);
setShowTooltip(true);
},
});
return (
{showTooltip && tooltipData && (
)}
);
}
```
--------------------------------
### LeaderboardChart Component in JavaScript
Source: https://context7.com/automattic/charts/llms.txt
Implements a LeaderboardChart for comparing ranked data across periods. It visualizes current and previous metrics, deltas, and progress. Requires React and the @automattic/charts library.
```javascript
import React from 'react';
import { LeaderboardChart } from '@automattic/charts/leaderboard-chart';
import '@automattic/charts/leaderboard-chart/style.css';
function TopPages() {
const data = [
{
id: 'home',
label: 'Home Page',
currentValue: 15420,
previousValue: 12350,
currentShare: 100,
previousShare: 80.1,
delta: 24.9,
},
{
id: 'product',
label: 'Product Page',
currentValue: 12890,
previousValue: 13200,
currentShare: 83.6,
previousShare: 85.7,
delta: -2.3,
},
{
id: 'blog',
label: 'Blog',
currentValue: 9670,
previousValue: 8100,
currentShare: 62.7,
previousShare: 52.6,
delta: 19.4,
},
{
id: 'about',
label: 'About Us',
currentValue: 5230,
previousValue: 4890,
currentShare: 33.9,
previousShare: 31.8,
delta: 7.0,
},
];
return (
);
}
```
--------------------------------
### Utilize React Hooks for Charting Utilities
Source: https://context7.com/automattic/charts/llms.txt
This code showcases the use of several React hooks from '@automattic/charts/hooks' for common charting operations. It includes `useChartDataTransform` for data preparation, `useChartMargin` for calculating optimal chart margins, `useInteractiveLegendData` for managing legend interactivity, and `usePrefersReducedMotion` for respecting user accessibility preferences. The component expects `data`, `width`, and `height` as props.
```javascript
import React from 'react';
import {
useChartDataTransform,
useChartMargin,
useInteractiveLegendData,
usePrefersReducedMotion,
} from '@automattic/charts/hooks';
function CustomChartComponent({ data, width, height }) {
// Transform raw data into chart-ready format
const transformedData = useChartDataTransform(data);
// Calculate optimal margins based on axis labels
const margin = useChartMargin({
hasYAxis: true,
hasXAxis: true,
yAxisWidth: 60,
xAxisHeight: 40,
});
// Manage interactive legend state
const { visibleSeries, toggleSeries, filteredData } = useInteractiveLegendData({
data: transformedData,
chartId: 'my-chart',
});
// Respect user's motion preferences
const prefersReducedMotion = usePrefersReducedMotion();
const shouldAnimate = !prefersReducedMotion;
return (
{/* Render your chart with calculated values */}
{/* Legend with toggleSeries callback */}
);
}
```
--------------------------------
### PieChart Component in JavaScript
Source: https://context7.com/automattic/charts/llms.txt
Renders a PieChart component to display proportional data. It supports customization of size, segment appearance, labels, and interactive legends. Dependencies include React and the @automattic/charts library.
```javascript
import React from 'react';
import { PieChart } from '@automattic/charts/pie-chart';
import '@automattic/charts/pie-chart/style.css';
function TrafficSources() {
const data = [
{
label: 'Organic Search',
value: 4580,
percentage: 45.8,
valueDisplay: '4.6K',
color: '#0073aa',
},
{
label: 'Direct',
value: 2850,
percentage: 28.5,
valueDisplay: '2.9K',
color: '#00a32a',
},
{
label: 'Social Media',
value: 1560,
percentage: 15.6,
valueDisplay: '1.6K',
color: '#f0b849',
},
{
label: 'Email',
value: 780,
percentage: 7.8,
valueDisplay: '780',
color: '#826eb4',
},
{
label: 'Other',
value: 230,
percentage: 2.3,
valueDisplay: '230',
color: '#cccccc',
},
];
return (
);
}
```
--------------------------------
### BarListChart Component in JavaScript
Source: https://context7.com/automattic/charts/llms.txt
Displays a BarListChart, a compact horizontal bar chart for ranked data. It allows custom rendering for labels and values, with options for tooltips and legend visibility. Requires React and the @automattic/charts library.
```javascript
import React from 'react';
import { BarListChart } from '@automattic/charts/bar-list-chart';
import '@automattic/charts/bar-list-chart/style.css';
function TopProducts() {
const data = [
{ label: 'Premium Plan', value: 3250 },
{ label: 'Business Plan', value: 2180 },
{ label: 'Enterprise Plan', value: 1890 },
{ label: 'Starter Plan', value: 1560 },
{ label: 'Add-ons', value: 890 },
];
const renderLabel = ({ label, color }) => (
{label}
);
const renderValue = ({ value, formattedValue }) => (
{formattedValue}
);
return (
);
}
```
--------------------------------
### React Legend Component Usage
Source: https://context7.com/automattic/charts/llms.txt
Shows how to implement a standalone Legend component from @automattic/charts/legend in a React application. It utilizes the useChartLegendItems hook to process chart data and generate legend items, supporting features like value display and totals. The Legend component can be configured with various props for orientation, shape, interactivity, and custom styling.
```javascript
import React from 'react';
import { Legend, useChartLegendItems } from '@automattic/charts/legend';
import '@automattic/charts/legend/style.css';
function CustomChartWithLegend() {
const data = [
{
label: 'Category A',
data: [{ date: new Date('2024-01-01'), value: 100 }],
},
{
label: 'Category B',
data: [{ date: new Date('2024-01-01'), value: 200 }],
},
];
const legendItems = useChartLegendItems({
data,
valueDisplay: 'value',
showTotal: true,
});
return (
{/* Your custom chart implementation */}
);
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.