|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance for Claude Code (and human developers) working with the py-svg-chart codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**py-svg-chart** is a Python library for generating SVG charts entirely in Python. It produces clean, standalone SVG output that can be embedded directly in web applications without JavaScript dependencies or post-processing. |
| 8 | + |
| 9 | +**Key differentiator:** Unlike image-based charting libraries or JS-dependent solutions, this generates resolution-independent, customizable SVG markup server-side. |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install pysvgchart |
| 15 | +``` |
| 16 | + |
| 17 | +```python |
| 18 | +import pysvgchart as psc |
| 19 | + |
| 20 | +# Simple donut chart |
| 21 | +chart = psc.DonutChart([25, 30, 20, 25]) |
| 22 | +svg = chart.render() |
| 23 | + |
| 24 | +# Line chart |
| 25 | +chart = psc.SimpleLineChart( |
| 26 | + x_values=[1, 2, 3, 4, 5], |
| 27 | + y_values=[[10, 20, 15, 25, 30]], |
| 28 | + y_names=['Sales'] |
| 29 | +) |
| 30 | +chart.add_legend() |
| 31 | +svg = chart.render() |
| 32 | +``` |
| 33 | + |
| 34 | +## Architecture Overview |
| 35 | + |
| 36 | +``` |
| 37 | +pysvgchart/ |
| 38 | +├── charts.py # Chart classes (entry points for users) |
| 39 | +├── axes.py # Axis management (XAxis, YAxis, CategoryYAxis) |
| 40 | +├── series.py # Data series (LineSeries, BarSeries, ScatterSeries, DonutSegment) |
| 41 | +├── scales.py # Scale calculations (Linear, Logarithmic, Categorical) |
| 42 | +├── legends.py # Legend rendering |
| 43 | +├── shapes.py # SVG primitives (Point, Line, Circle, Rect, Text, Group) |
| 44 | +├── helpers.py # Utility functions (tick generation, formatting) |
| 45 | +├── styles.py # CSS style rendering for hover effects |
| 46 | +└── shared.py # Type definitions |
| 47 | +``` |
| 48 | + |
| 49 | +### Class Hierarchy |
| 50 | + |
| 51 | +``` |
| 52 | +Chart (ABC) |
| 53 | +├── CartesianChart |
| 54 | +│ ├── VerticalChart # Y-axis vertical, X-axis horizontal |
| 55 | +│ │ ├── LineChart # Line/area charts |
| 56 | +│ │ │ ├── SimpleLineChart |
| 57 | +│ │ │ ├── BarChart |
| 58 | +│ │ │ ├── NormalisedBarChart |
| 59 | +│ │ │ └── ScatterChart |
| 60 | +│ │ └── (inherits axis setup) |
| 61 | +│ └── HorizontalChart # X-axis has values, Y-axis has categories |
| 62 | +│ └── HorizontalBarChart |
| 63 | +└── DonutChart # Pie/donut (non-Cartesian) |
| 64 | +``` |
| 65 | + |
| 66 | +### Data Flow |
| 67 | + |
| 68 | +1. **User creates chart** with x_values, y_values, configuration |
| 69 | +2. **Axes created** via `x_axis_type`/`y_axis_type` using `scale_maker` functions |
| 70 | +3. **Series constructed** via `series_constructor` (converts data to positioned shapes) |
| 71 | +4. **Optional additions:** legends, grids, hover modifiers, custom elements |
| 72 | +5. **Render:** `chart.render()` or `chart.render_with_all_styles()` produces SVG string |
| 73 | + |
| 74 | +### Key Design Patterns |
| 75 | + |
| 76 | +- **Factory Pattern:** `series_constructor` functions create appropriate Series types |
| 77 | +- **Template Method:** Base `Chart` defines `render()`, subclasses implement `get_element_list()` |
| 78 | +- **Strategy Pattern:** Pluggable `scale_maker` functions, `label_format` callbacks |
| 79 | +- **Composition:** Charts compose axes, series, legends, shapes |
| 80 | + |
| 81 | +## Chart Types |
| 82 | + |
| 83 | +| Chart | Class | Use Case | |
| 84 | +|-------|-------|----------| |
| 85 | +| Line | `LineChart`, `SimpleLineChart` | Time series, trends | |
| 86 | +| Bar (vertical) | `BarChart` | Category comparisons | |
| 87 | +| Bar (horizontal) | `HorizontalBarChart` | Long category names | |
| 88 | +| Stacked (100%) | `NormalisedBarChart` | Part-of-whole comparisons | |
| 89 | +| Scatter | `ScatterChart` | Correlation, distribution | |
| 90 | +| Donut/Pie | `DonutChart` | Proportions | |
| 91 | + |
| 92 | +## Common Patterns |
| 93 | + |
| 94 | +### Adding Interactivity (Hover Effects) |
| 95 | + |
| 96 | +```python |
| 97 | +def hover_fn(position, x_value, y_value, series_name, styles): |
| 98 | + return [psc.Text(x=position.x, y=position.y-10, content=str(y_value), |
| 99 | + classes=['psc-hover-data'])] |
| 100 | + |
| 101 | +chart.add_hover_modifier(hover_fn, radius=5) |
| 102 | +svg = chart.render_with_all_styles() # Must use this for hovers |
| 103 | +``` |
| 104 | + |
| 105 | +### Custom Styling |
| 106 | + |
| 107 | +```python |
| 108 | +# Direct series style modification |
| 109 | +chart.series['Series Name'].styles = {'stroke': 'red', 'stroke-width': '3'} |
| 110 | + |
| 111 | +# Access axis elements |
| 112 | +chart.x_axis.tick_lines = [] # Remove tick marks |
| 113 | +chart.y_axis.axis_line.styles['stroke'] = '#ccc' |
| 114 | +``` |
| 115 | + |
| 116 | +### Adding Custom Elements |
| 117 | + |
| 118 | +```python |
| 119 | +chart.add_custom_element(psc.Circle(x=100, y=100, radius=5, styles={'fill': 'red'})) |
| 120 | +chart.add_custom_element(psc.Text(x=200, y=50, content='Annotation')) |
| 121 | +``` |
| 122 | + |
| 123 | +## Testing |
| 124 | + |
| 125 | +```bash |
| 126 | +# Run all tests |
| 127 | +pytest |
| 128 | + |
| 129 | +# Run specific test file |
| 130 | +pytest tests/test_pysvgchart.py |
| 131 | + |
| 132 | +# Tests generate SVG files in showcase/ directory |
| 133 | +``` |
| 134 | + |
| 135 | +The `showcase/` directory contains generated SVG examples from the test suite. |
| 136 | + |
| 137 | +## File Purposes |
| 138 | + |
| 139 | +| File | Purpose | |
| 140 | +|------|---------| |
| 141 | +| `charts.py` | Main entry points. All public chart classes. Series constructors. | |
| 142 | +| `axes.py` | Axis rendering, tick generation, scale positioning | |
| 143 | +| `series.py` | Data series types that render as SVG paths/shapes | |
| 144 | +| `scales.py` | Numeric scale calculations (min/max, tick intervals) | |
| 145 | +| `shapes.py` | Low-level SVG elements (Point, Line, Circle, Text, Group) | |
| 146 | +| `legends.py` | Legend components for each chart type | |
| 147 | +| `helpers.py` | Utility functions (formatting, element list flattening) | |
| 148 | +| `styles.py` | CSS generation for hover effects | |
| 149 | +| `shared.py` | Type aliases used across modules | |
| 150 | + |
| 151 | +## Type System |
| 152 | + |
| 153 | +Key type aliases from `shared.py`: |
| 154 | +- `number = float | int` |
| 155 | +- `numbers_sequence = list[number] | tuple[number, ...]` |
| 156 | +- `style_def = dict[str, str]` (SVG style attributes) |
| 157 | + |
| 158 | +## Common Tasks |
| 159 | + |
| 160 | +### Adding a new chart type |
| 161 | + |
| 162 | +1. Choose base class (`VerticalChart`, `HorizontalChart`, or `Chart`) |
| 163 | +2. Set class attributes: `x_axis_type`, `y_axis_type`, `series_constructor`, `scale_maker`s |
| 164 | +3. Override `add_legend()` if needed |
| 165 | +4. Add to `__init__.py` exports |
| 166 | + |
| 167 | +### Modifying axis behavior |
| 168 | + |
| 169 | +Axes are in `axes.py`. Key methods: |
| 170 | +- `get_positions()` - Convert data values to pixel positions |
| 171 | +- Scale creation via `scale_maker` parameter |
| 172 | + |
| 173 | +### Adding new shapes |
| 174 | + |
| 175 | +Add to `shapes.py`. Inherit from `Element` or `Shape`, implement `get_element_list()` to return SVG strings. |
| 176 | + |
| 177 | +## Dependencies |
| 178 | + |
| 179 | +- Python 3.10+ |
| 180 | +- No runtime dependencies (standard library only) |
| 181 | +- Dev: pytest for testing |
0 commit comments