# Chart Builder

## Objective

Let someone pick a data source, choose the fields for each axis, and save the chart.

A guided builder that turns a data source and a field selection into a saved chart, placeable on the app's dashboards.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Expose a defined set of data sources with typed fields rather than a free query box. The builder should know which fields are dates, categories and measures, because that is what makes the field pickers sensible.
2. Render through the app's existing chart components and colour tokens. If Chart Color Tokens exists, use it; a builder that introduces its own palette will not match the dashboards the charts land on.
3. Save charts as objects the app's dashboards can already host. If Customizable Dashboard Widgets exists, a saved chart becomes one of its widgets rather than a new kind of tile.
4. Give every saved chart the accessible summary and data table the app already provides for charts, so the numbers are reachable without reading the picture.
5. Do not offer every chart type for every field selection. Filter the options to the ones that produce a readable result, and explain why a combination is unavailable rather than hiding it silently.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Some field and chart-type pairs produce nonsense: a pie of a continuous measure, a time series with no date field, a stacked bar of unrelated units. Block those combinations at selection time and say what is missing.
- A source with a million rows must not be plotted point by point. Cap the result set, aggregate into buckets by default, and tell the user when the chart is showing an aggregate rather than every row.
- A saved chart is run for whoever opens it, not whoever built it. Apply the viewer's row-level permissions at query time, or a shared dashboard becomes a data leak.
- Nulls, zeroes and empty time buckets are three different things. Decide for each chart type whether a gap is drawn as zero, as a break in the line, or as nothing, and be consistent across the app.
- Source fields get renamed and removed. A saved chart pointing at a field that no longer exists must show a clear broken state with a way to remap it, not an empty chart or a stack trace.
- Charts must respect the viewer's locale and time zone for dates and numbers, or two people looking at the same dashboard will read different days.
- A query that runs too long needs a timeout and a visible message. A dashboard of six charts must not hold the page hostage because one source is slow.
- An empty result is a legitimate outcome. Show the app's empty state with the filters that produced it, not a blank rectangle.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] A user can choose a source, map fields to axes, pick a chart type and save the result to a dashboard.
- [ ] Chart types incompatible with the selected fields are unavailable and the reason is stated.
- [ ] Large result sets are capped and aggregated, and the aggregation is disclosed on the chart.
- [ ] Saved charts run against the viewer's permissions, not the author's.
- [ ] Nulls, zeroes and missing time buckets render according to a documented rule.
- [ ] A chart whose source field was renamed or removed shows a broken state with a remap path.
- [ ] Every saved chart carries the app's accessible summary and underlying data table.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
