|
| 1 | +# Charts Rules |
| 2 | + |
| 3 | +Essential rules for PatternFly Charts implementation using Victory.js and ECharts. |
| 4 | + |
| 5 | +## Related Files |
| 6 | +- [**Component Architecture**](../guidelines/component-architecture.md) - Chart component structure rules |
| 7 | +- [**Performance Optimization**](../troubleshooting/performance.md) - Chart performance considerations |
| 8 | + |
| 9 | +## Installation Rules |
| 10 | + |
| 11 | +### Required Installation |
| 12 | +```bash |
| 13 | +# ✅ Victory-based charts (recommended) |
| 14 | +npm install @patternfly/react-charts victory |
| 15 | + |
| 16 | +# ✅ ECharts-based charts (alternative) |
| 17 | +npm install @patternfly/react-charts echarts |
| 18 | +``` |
| 19 | + |
| 20 | +### Import Rules |
| 21 | +- ✅ **Use specific import paths** - Import from `/victory` or `/echarts` subdirectories |
| 22 | +- ❌ **Don't use general imports** - Avoid importing from main package |
| 23 | + |
| 24 | +```jsx |
| 25 | +// ✅ Correct imports |
| 26 | +import { ChartDonut, ChartLine, ChartBar } from '@patternfly/react-charts/victory'; |
| 27 | +import { EChart } from '@patternfly/react-charts/echarts'; |
| 28 | + |
| 29 | +// ❌ Wrong imports |
| 30 | +import { ChartDonut } from '@patternfly/react-charts'; |
| 31 | +``` |
| 32 | + |
| 33 | +### Troubleshooting Import Issues |
| 34 | +If "module not found" errors occur: |
| 35 | +1. **Clear cache**: `rm -rf node_modules package-lock.json` |
| 36 | +2. **Reinstall**: `npm install` |
| 37 | +3. **Verify paths**: Check import paths match installed version |
| 38 | + |
| 39 | +## Chart Implementation Rules |
| 40 | + |
| 41 | +### Color Rules |
| 42 | +- ✅ **Use PatternFly chart color tokens** - For consistency with design system |
| 43 | +- ❌ **Don't use hardcoded colors** - Use design tokens instead |
| 44 | + |
| 45 | +```jsx |
| 46 | +// ✅ Correct - Use PatternFly color tokens |
| 47 | +const chartColors = [ |
| 48 | + 'var(--pf-v6-chart-color-blue-300)', |
| 49 | + 'var(--pf-v6-chart-color-green-300)', |
| 50 | + 'var(--pf-v6-chart-color-orange-300)' |
| 51 | +]; |
| 52 | + |
| 53 | +<ChartDonut data={data} colorScale={chartColors} /> |
| 54 | +``` |
| 55 | + |
| 56 | +### Responsive Rules |
| 57 | +- ✅ **Implement responsive sizing** - Charts must work on all screen sizes |
| 58 | +- ✅ **Use container-based dimensions** - Not fixed width/height |
| 59 | +- ❌ **Don't hardcode dimensions** - Charts must be responsive |
| 60 | + |
| 61 | +```jsx |
| 62 | +// ✅ Required responsive pattern |
| 63 | +const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); |
| 64 | + |
| 65 | +useEffect(() => { |
| 66 | + const updateDimensions = () => { |
| 67 | + if (containerRef.current) { |
| 68 | + const { width, height } = containerRef.current.getBoundingClientRect(); |
| 69 | + setDimensions({ width, height }); |
| 70 | + } |
| 71 | + }; |
| 72 | + updateDimensions(); |
| 73 | + window.addEventListener('resize', updateDimensions); |
| 74 | + return () => window.removeEventListener('resize', updateDimensions); |
| 75 | +}, []); |
| 76 | +``` |
| 77 | + |
| 78 | +### Accessibility Rules |
| 79 | +- ✅ **Provide ARIA labels** - For screen reader support |
| 80 | +- ✅ **Use high contrast colors** - Meet WCAG standards |
| 81 | +- ✅ **Support keyboard navigation** - Add tabIndex and role |
| 82 | + |
| 83 | +```jsx |
| 84 | +// ✅ Required accessibility pattern |
| 85 | +<ChartDonut |
| 86 | + data={data} |
| 87 | + ariaDesc="Chart showing user distribution" |
| 88 | + ariaTitle="User Status Chart" |
| 89 | + tabIndex={0} |
| 90 | + role="img" |
| 91 | +/> |
| 92 | +``` |
| 93 | + |
| 94 | +### State Management Rules |
| 95 | +- ✅ **Handle loading states** - Show spinners during data loading |
| 96 | +- ✅ **Handle error states** - Show error messages with retry |
| 97 | +- ✅ **Handle empty states** - Show appropriate empty messages |
| 98 | +- ✅ **Use data memoization** - For performance optimization |
| 99 | + |
| 100 | +```jsx |
| 101 | +// ✅ Required state handling |
| 102 | +if (isLoading) return <Spinner />; |
| 103 | +if (error) return <EmptyState><EmptyStateHeader titleText="Chart error" /></EmptyState>; |
| 104 | +if (!data?.length) return <EmptyState><EmptyStateHeader titleText="No data" /></EmptyState>; |
| 105 | + |
| 106 | +const processedData = useMemo(() => { |
| 107 | + return rawData.map(item => ({ x: item.date, y: item.value })); |
| 108 | +}, [rawData]); |
| 109 | +``` |
| 110 | +
|
| 111 | +### Integration Rules |
| 112 | +- ✅ **Use with PatternFly components** - Integrate charts in Cards, PageSections |
| 113 | +- ✅ **Follow grid layouts** - Use PatternFly grid for chart dashboards |
| 114 | +- ❌ **Don't create standalone chart pages** - Integrate with PatternFly layout |
| 115 | +
|
| 116 | +```jsx |
| 117 | +// ✅ Required integration pattern |
| 118 | +import { Card, CardTitle, CardBody } from '@patternfly/react-core'; |
| 119 | + |
| 120 | +<Card> |
| 121 | + <CardTitle>Chart Title</CardTitle> |
| 122 | + <CardBody> |
| 123 | + <ChartDonut data={data} /> |
| 124 | + </CardBody> |
| 125 | +</Card> |
| 126 | +``` |
| 127 | +
|
| 128 | +## Performance Rules |
| 129 | +
|
| 130 | +### Required Optimizations |
| 131 | +- ✅ **Use lazy loading for heavy charts** - Improve initial page load |
| 132 | +- ✅ **Memoize data processing** - Use useMemo for expensive calculations |
| 133 | +- ✅ **Implement proper loading states** - Show feedback during data loading |
| 134 | +
|
| 135 | +```jsx |
| 136 | +// ✅ Required performance patterns |
| 137 | +const LazyChart = lazy(() => import('./HeavyChart')); |
| 138 | + |
| 139 | +<Suspense fallback={<Spinner />}> |
| 140 | + <LazyChart /> |
| 141 | +</Suspense> |
| 142 | +``` |
| 143 | +
|
| 144 | +## Essential Do's and Don'ts |
| 145 | +
|
| 146 | +### ✅ Do's |
| 147 | +- Use PatternFly chart color tokens for consistency |
| 148 | +- Implement responsive sizing for different screen sizes |
| 149 | +- Provide proper ARIA labels and descriptions |
| 150 | +- Handle loading, error, and empty states |
| 151 | +- Use appropriate chart types for your data |
| 152 | +- Optimize performance with data memoization |
| 153 | +- Integrate charts with PatternFly layout components |
| 154 | +
|
| 155 | +### ❌ Don'ts |
| 156 | +- Hardcode chart dimensions without responsive design |
| 157 | +- Use colors that don't meet accessibility standards |
| 158 | +- Skip loading states for charts with async data |
| 159 | +- Ignore keyboard navigation and screen reader support |
| 160 | +- Create overly complex charts |
| 161 | +- Mix different charting libraries inconsistently |
| 162 | +- Forget to handle empty data states |
| 163 | +
|
| 164 | +## Common Issues |
| 165 | +
|
| 166 | +### Module Not Found |
| 167 | +- **Clear cache**: `rm -rf node_modules package-lock.json` |
| 168 | +- **Reinstall**: `npm install` |
| 169 | +- **Check paths**: Verify import paths are correct |
| 170 | +
|
| 171 | +### Chart Not Rendering |
| 172 | +- **Check container dimensions**: Ensure parent has width/height |
| 173 | +- **Verify data format**: Data must match chart expectations |
| 174 | +- **Check console**: Look for Victory.js or ECharts warnings |
| 175 | +
|
| 176 | +### Performance Issues |
| 177 | +- **Use data memoization**: useMemo for expensive calculations |
| 178 | +- **Implement lazy loading**: For heavy chart components |
| 179 | +- **Optimize re-renders**: Use React.memo for chart components |
| 180 | +
|
| 181 | +## Quick Reference |
| 182 | +- **[PatternFly Charts README](https://github.com/patternfly/patternfly-react/tree/main/packages/react-charts#readme)** - Installation and usage |
| 183 | +- **[Victory.js Documentation](https://formidable.com/open-source/victory/)** - Chart library documentation |
| 184 | +- **[PatternFly Chart Guidelines](https://www.patternfly.org/charts/about)** - Design guidelines |
| 185 | +
|
| 186 | +## Reference Documentation |
| 187 | +
|
| 188 | +- [PatternFly Charts on PatternFly.org](https://www.patternfly.org/charts/about) |
| 189 | +- [PatternFly React Charts GitHub Repository](https://github.com/patternfly/patternfly-react/tree/main/packages/react-charts) |
| 190 | +
|
| 191 | +> 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. |
0 commit comments