|
| 1 | +# Chart Export Functionality |
| 2 | + |
| 3 | +This document describes the new Chart Export feature that allows users to export pivot table data as interactive charts and save them as high-resolution PNG images. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Chart Export component (`ChartExport.tsx`) integrates with Chart.js to provide visualization capabilities for pivot table data. Users can: |
| 8 | + |
| 9 | +- Choose from multiple chart types (Bar, Line, Doughnut, Pie) |
| 10 | +- View real-time chart updates as pivot table configuration changes |
| 11 | +- Export charts as high-resolution PNG images |
| 12 | +- Customize chart appearance and data representation |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +### Chart Types |
| 17 | + |
| 18 | +1. **Bar Chart**: Best for comparing values across categories |
| 19 | +2. **Line Chart**: Ideal for showing trends over time or sequences |
| 20 | +3. **Doughnut Chart**: Good for showing proportions of a whole |
| 21 | +4. **Pie Chart**: Similar to doughnut but shows complete proportions |
| 22 | + |
| 23 | +### Data Visualization |
| 24 | + |
| 25 | +- **Row-based grouping**: Row fields become chart labels |
| 26 | +- **Column-based datasets**: Each column field combination becomes a separate dataset |
| 27 | +- **Totals integration**: Row totals can be included as an additional dataset |
| 28 | +- **Dynamic coloring**: Automatic color generation for different datasets |
| 29 | +- **Responsive design**: Charts adapt to container size |
| 30 | + |
| 31 | +### Export Capabilities |
| 32 | + |
| 33 | +- **High-resolution output**: 2x scale for crisp images |
| 34 | +- **PNG format**: Lossless image format suitable for presentations and reports |
| 35 | +- **Automatic naming**: Files include chart type and timestamp |
| 36 | +- **Background handling**: Clean white background for professional appearance |
| 37 | + |
| 38 | +## Technical Implementation |
| 39 | + |
| 40 | +### Dependencies |
| 41 | + |
| 42 | +- `chart.js` (v4.5.0): Core charting library |
| 43 | +- `react-chartjs-2` (v5.3.0): React wrapper for Chart.js |
| 44 | +- `html2canvas` (v1.4.1): HTML to canvas conversion for image export |
| 45 | + |
| 46 | +### Component Structure |
| 47 | + |
| 48 | +```tsx |
| 49 | +<ChartExport |
| 50 | + pivotData={pivotComputationResult} |
| 51 | + rowFields={selectedRowFields} |
| 52 | + columnFields={selectedColumnFields} |
| 53 | + valueField={selectedValueField} |
| 54 | + aggregator={selectedAggregator} |
| 55 | + chartType="bar" |
| 56 | + showRowTotals={true} |
| 57 | + showColumnTotals={true} |
| 58 | +/> |
| 59 | +``` |
| 60 | + |
| 61 | +### Data Flow |
| 62 | + |
| 63 | +1. **Pivot Data**: Raw pivot table computation results |
| 64 | +2. **Chart Conversion**: Data transformation for Chart.js format |
| 65 | +3. **Rendering**: Chart display using react-chartjs-2 |
| 66 | +4. **Export**: HTML to canvas conversion and PNG download |
| 67 | + |
| 68 | +## Usage |
| 69 | + |
| 70 | +### Basic Setup |
| 71 | + |
| 72 | +1. Ensure pivot table has both row and column fields selected |
| 73 | +2. The Chart Export component will automatically appear above the pivot table |
| 74 | +3. Select desired chart type from the dropdown |
| 75 | +4. Click "Export as Image" to download the chart |
| 76 | + |
| 77 | +### Chart Type Selection |
| 78 | + |
| 79 | +- **Bar/Line**: Best for comparing multiple categories with multiple datasets |
| 80 | +- **Pie/Doughnut**: Best for showing proportions when you have one main dimension |
| 81 | + |
| 82 | +### Export Process |
| 83 | + |
| 84 | +1. Click "Export as Image" button |
| 85 | +2. Wait for processing (button shows "Exporting...") |
| 86 | +3. Browser automatically downloads PNG file |
| 87 | +4. File is named: `pivot-chart-{type}-{timestamp}.png` |
| 88 | + |
| 89 | +## Integration |
| 90 | + |
| 91 | +The component is automatically integrated into the PivotTab and only appears when: |
| 92 | +- At least one row field is selected |
| 93 | +- At least one column field is selected |
| 94 | +- Valid pivot data exists |
| 95 | + |
| 96 | +## Styling |
| 97 | + |
| 98 | +- Follows the existing design system with minimal, clean appearance |
| 99 | +- Uses Tailwind CSS classes for consistent styling |
| 100 | +- Responsive design that works on different screen sizes |
| 101 | +- Colorblind-friendly color generation using HSL color space |
| 102 | + |
| 103 | +## Performance Considerations |
| 104 | + |
| 105 | +- Charts are rendered only when pivot data changes |
| 106 | +- Export process uses `html2canvas` for reliable image generation |
| 107 | +- Chart data is memoized to prevent unnecessary re-renders |
| 108 | +- Responsive design maintains good performance on various devices |
| 109 | + |
| 110 | +## Browser Compatibility |
| 111 | + |
| 112 | +- Modern browsers with ES6+ support |
| 113 | +- Canvas API support required for image export |
| 114 | +- File download API support required for automatic downloads |
| 115 | + |
| 116 | +## Troubleshooting |
| 117 | + |
| 118 | +### Common Issues |
| 119 | + |
| 120 | +1. **Chart not appearing**: Ensure both row and column fields are selected |
| 121 | +2. **Export fails**: Check browser console for errors, ensure canvas is properly rendered |
| 122 | +3. **Poor image quality**: Export uses 2x scale by default for high resolution |
| 123 | +4. **Chart data missing**: Verify pivot table configuration and data availability |
| 124 | + |
| 125 | +### Debug Information |
| 126 | + |
| 127 | +- Check browser console for any JavaScript errors |
| 128 | +- Verify pivot data structure matches expected format |
| 129 | +- Ensure all required dependencies are properly installed |
| 130 | + |
| 131 | +## Future Enhancements |
| 132 | + |
| 133 | +Potential improvements for future versions: |
| 134 | +- Additional chart types (scatter, radar, etc.) |
| 135 | +- Custom color schemes |
| 136 | +- Chart configuration options (axes, legends, etc.) |
| 137 | +- Multiple export formats (SVG, PDF) |
| 138 | +- Chart templates and presets |
| 139 | +- Batch export capabilities |
0 commit comments