# Adaptive Card patterns for Rainbow (validated)

## 1. Donut / gauge / bar charts as generated SVG images

The native `Chart.Donut` element does not render reliably in Rainbow. A pattern that works everywhere images are accepted:

- Draw the chart as an SVG string and embed it in a standard `Image` element as a `data:image/svg+xml;base64,...` URL.
- Donut: one `<circle r="40" cx="60" cy="60" fill="none" stroke-width="18">` per segment, rotated by `-90` degrees around the centre. The visible length of a segment is given by `stroke-dasharray` (circumference = `2*PI*r`, segment = `circumference * value / total`, minus a small gap) and its position by a negative `stroke-dashoffset` equal to the sum of the previous segments.
- Provide a text legend next to the image (`TextBlock`s) and an `altText` summarising the values.
- The image is static: no hover and no per-segment click (only a `selectAction` on the whole image). To change the numbers, regenerate the SVG on the server and return new data (see section 2).

### Example: the donut generator

`donutSvg(values, colors)` in `modules/adaptivecard/server-adaptivecard.js` returns the data URI to put in `Image.url`:

```js
function donutSvg(values, colors, { r = 40, sw = 18, gap = 2 } = {}) {
  const C = 2 * Math.PI * r;
  const total = values.reduce((a, b) => a + b, 0);
  let arcs;
  if (total <= 0) {
    arcs = `<circle r="${r}" cx="60" cy="60" fill="none" stroke="#C8C8C8" stroke-width="${sw}"/>`;
  } else {
    let off = 0;
    arcs = values.map((v, i) => {
      const len = C * v / total;
      const seg = len > 0
        ? `<circle r="${r}" cx="60" cy="60" fill="none" stroke="${colors[i]}" stroke-width="${sw}"` +
          ` stroke-dasharray="${Math.max(len - (values.filter(x => x > 0).length > 1 ? gap : 0), 0.1).toFixed(2)} ${C.toFixed(2)}"` +
          ` stroke-dashoffset="${(-off).toFixed(2)}" transform="rotate(-90 60 60)"/>`
        : '';
      off += len;
      return seg;
    }).join('');
  }
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120" viewBox="0 0 120 120">${arcs}</svg>`;
  return 'data:image/svg+xml;base64,' + Buffer.from(svg).toString('base64');
}
```

Usage: `donutSvg([6, 9, 22], ['#C4314B', '#F2A900', '#92C353'])`.

## 2. Refreshing a card after `Action.Submit` (validated in Rainbow)

Rainbow turns `Action.Submit` into a **POST** to `adaptiveCardSubmitUrl`. The body contains the action's `data` plus the values of the `Input.*` elements (keyed by input `id`).

To make the card refresh with new values:

- Author the card as a **template**: every value that can change is a `${...}` binding (`${high}`, `${total}`, `${donutUrl}`, ...), including the `value` of inputs.
- The submit endpoint must answer with **the same structure as the initial response**, i.e. `{ "card": <template>, "data": <new data> }` (with `adaptiveCardTemplateJsonField: "card"` and `adaptiveCardContentJsonField: "data"` in the manifest).
- Do **not** return a card with hard-coded new values: Rainbow re-applies its original template with the original data and the card falls back to its initial values.
- An `Input.Number` needs a number: a value that is exactly `"${x}"` must resolve to a number (adaptivecards-templating does this; the sandbox `expandTemplate` in `public/adaptivecard/index.html` mimics it).

Reference sample: `svg-risk-donut-editable` (hook `onSubmit` returns `{card, data}`).
