Essential rules for PatternFly Charts implementation using Victory.js and ECharts.
# ✅ Victory-based charts (recommended)
npm install @patternfly/react-charts victory
# ✅ ECharts-based charts (alternative)
npm install @patternfly/react-charts echarts
# ✅ Patternfly for charts css variables (recommended)
npm install @patternfly/patternfly- ✅ CRITICAL: Use specific import paths - Import from
/victoryor/echartssubdirectories - ❌ Don't use general imports - Avoid importing from main package
⚠️ Common Error: AI assistants often forget the/victorypath
// ✅ Correct imports - MUST include /victory
import { ChartDonut, ChartLine, ChartBar } from '@patternfly/react-charts/victory';
import { EChart } from '@patternfly/react-charts/echarts';
// ❌ Wrong imports - Missing /victory will cause "Module not found" errors
import { ChartDonut } from '@patternfly/react-charts';// In your main App.js or index.js
import '@patternfly/patternfly/patternfly-charts.css';# Error message you'll see:
Module not found: Can't resolve '@patternfly/react-charts'Solutions in order:
-
Fix import paths - Add
/victoryto your imports:// Change this: import { ChartDonut } from '@patternfly/react-charts'; // To this: import { ChartDonut } from '@patternfly/react-charts/victory';
-
Install Victory dependency:
npm install victory
-
Clear cache and reinstall:
rm -rf node_modules package-lock.json npm install
-
Verify installation:
# Check if both packages are installed npm list @patternfly/react-charts npm list victory
- Chart color families include
- red-orange
- orange
- yellow
- teal
- green
- blue
- purple
- black
- Use the same brightness of colors first, then use other brightness
- Base: 300
- Lightest: 100
- Darkest: 500
- Second-lightest: 200
- Second-darkest: 400
- Use PatternFly tokens to define your color variables
- --pf-t-chart-color-[color-family]-[brightness]
- example: --pf-t-chart-color-blue-300
- --pf-t-chart-color-[color-family]-[brightness]
- When selecting colors for your chart, adhere to these general rules:
- Within a color family, use the base color first. Then use the other lighter and darker hues.
- Some families have predetermined uses:
- Blue: Use to show success.
- Red-orange: Use to show failure. Do not use this family unless you're communicating failure.
- Other colors: Use for neutral purposes or categories.
- ✅ Use PatternFly chart color tokens - For consistency with design system
- ❌ Don't use hardcoded colors - Use design tokens instead
// ✅ Correct - Use PatternFly color tokens
const chartColors = [
'var(--pf-t--chart--color--[color family 1]--300)',
'var(--pf-t--chart--color--[color family 2]--300)',
'var(--pf-t--chart--color--[color family 3]--300)'
];
<ChartDonut data={data} colorScale={chartColors} />- ✅ Implement responsive sizing - Charts must work on all screen sizes
- ✅ Use container-based dimensions - Not fixed width/height
- ❌ Don't hardcode dimensions - Charts must be responsive
// ✅ Required responsive pattern
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
useEffect(() => {
const updateDimensions = () => {
if (containerRef.current) {
const { width, height } = containerRef.current.getBoundingClientRect();
setDimensions({ width, height });
}
};
updateDimensions();
window.addEventListener('resize', updateDimensions);
return () => window.removeEventListener('resize', updateDimensions);
}, []);- ✅ Provide ARIA labels - For screen reader support
- ✅ Use high contrast colors - Meet WCAG standards
- ✅ Support keyboard navigation - Add tabIndex and role
// ✅ Required accessibility pattern
<ChartDonut
data={data}
ariaDesc="Chart showing user distribution"
ariaTitle="User Status Chart"
tabIndex={0}
role="img"
/>- ✅ Handle loading states - Show spinners during data loading
- ✅ Handle error states - Show error messages with retry
- ✅ Handle empty states - Show appropriate empty messages
- ✅ Use data memoization - For performance optimization
// ✅ Required state handling
if (isLoading) return <Spinner />;
if (error) return <EmptyState><EmptyStateHeader titleText="Chart error" /></EmptyState>;
if (!data?.length) return <EmptyState><EmptyStateHeader titleText="No data" /></EmptyState>;
const processedData = useMemo(() => {
return rawData.map(item => ({ x: item.date, y: item.value }));
}, [rawData]);- ✅ Use with PatternFly components - Integrate charts in Cards, PageSections
- ✅ Follow grid layouts - Use PatternFly grid for chart dashboards
- ❌ Don't create standalone chart pages - Integrate with PatternFly layout
// ✅ Required integration pattern
import { Card, CardTitle, CardBody } from '@patternfly/react-core';
<Card>
<CardTitle>Chart Title</CardTitle>
<CardBody>
<ChartDonut data={data} />
</CardBody>
</Card>- ✅ Use lazy loading for heavy charts - Improve initial page load
- ✅ Memoize data processing - Use useMemo for expensive calculations
- ✅ Implement proper loading states - Show feedback during data loading
// ✅ Required performance patterns
const LazyChart = lazy(() => import('./HeavyChart'));
<Suspense fallback={<Spinner />}>
<LazyChart />
</Suspense>- Use PatternFly chart color tokens for consistency
- Implement responsive sizing for different screen sizes
- Provide proper ARIA labels and descriptions
- Handle loading, error, and empty states
- Use appropriate chart types for your data
- Optimize performance with data memoization
- Integrate charts with PatternFly layout components
- Hardcode chart dimensions without responsive design
- Use colors that don't meet accessibility standards
- Skip loading states for charts with async data
- Ignore keyboard navigation and screen reader support
- Create overly complex charts
- Mix different charting libraries inconsistently
- Forget to handle empty data states
- Clear cache:
rm -rf node_modules package-lock.json - Reinstall:
npm install - Check paths: Verify import paths are correct
- Check container dimensions: Ensure parent has width/height
- Verify data format: Data must match chart expectations
- Check console: Look for Victory.js or ECharts warnings
- Use data memoization: useMemo for expensive calculations
- Implement lazy loading: For heavy chart components
- Optimize re-renders: Use React.memo for chart components
# Symptoms: Chart color variables (--pf-v6-chart-color-blue-100, --pf-t--chart--color--blue--300) are not definedSolutions:
-
Install patternfly package:
npm install @patternfly/patternfly
-
Import PatternFly Charts CSS:
// In your main App.js or index.js import '@patternfly/patternfly/patternfly-charts.css';
- PatternFly Charts README - Installation and usage
- Victory.js Documentation - Chart library documentation
- PatternFly Chart Guidelines - Design guidelines
- PatternFly Chart Colors - Colors for charts
For the most up-to-date documentation and code examples, consult both PatternFly.org and the official GitHub repository. When using AI tools, leverage context7 to fetch the latest docs from these sources.