{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "slope-chart",
  "type": "registry:item",
  "title": "Slope Chart",
  "description": "Before and after on two axes: lines draw in, rises in blue, and new data slides every end to its new place.",
  "categories": [
    "data"
  ],
  "dependencies": [
    "@shopify/react-native-skia",
    "react-native-reanimated",
    "react-native-worklets"
  ],
  "registryDependencies": [
    "https://kinetik-ui.dev/r/kinetik-tokens.json",
    "https://kinetik-ui.dev/r/kinetik-motion.json"
  ],
  "files": [
    {
      "path": "src/components/kinetik/slope-chart.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/slope-chart.tsx",
      "content": "import { Canvas, Circle, Line, vec } from '@shopify/react-native-skia';\nimport { useEffect, useMemo, useState } from 'react';\nimport { Pressable, StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';\nimport Animated, {\n  useAnimatedStyle,\n  useDerivedValue,\n  useSharedValue,\n  withDelay,\n  withSpring,\n  withTiming,\n} from 'react-native-reanimated';\n\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { durations, easings, spring } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\n\nexport type SlopeSeries = {\n  label: string;\n  /** Value on the left, e.g. last year. */\n  from: number;\n  /** Value on the right, e.g. this year. */\n  to: number;\n};\n\nexport type SlopeChartProps = {\n  series: readonly SlopeSeries[];\n  /** Heading over the left column. Defaults to 'Before'. */\n  fromLabel?: string;\n  /** Heading over the right column. Defaults to 'After'. */\n  toLabel?: string;\n  /** Formats values beside the dots. Defaults to whole numbers. */\n  formatValue?: (value: number) => string;\n  /** Plot height, without the headings. Defaults to 240. */\n  height?: number;\n  /** What the chart shows, read first, e.g. \"Share of trips by city\". */\n  accessibilityLabel: string;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\nconst LABEL_W = 92;\nconst ROW_H = 18;\nconst DOT = 4;\n\n/** Spreads label positions apart so none overlap, keeping each as close to its dot as it can. */\nfunction spread(ys: number[], min: number, max: number): number[] {\n  const order = ys.map((y, i) => ({ y, i })).sort((a, b) => a.y - b.y);\n  const out = order.map((o) => o.y);\n  for (let k = 1; k < out.length; k++) out[k] = Math.max(out[k]!, out[k - 1]! + ROW_H);\n  // Pushed past the bottom: walk back up.\n  const overflow = out.length ? out[out.length - 1]! - max : 0;\n  if (overflow > 0) for (let k = 0; k < out.length; k++) out[k] = out[k]! - overflow;\n  for (let k = out.length - 2; k >= 0; k--) out[k] = Math.min(out[k]!, out[k + 1]! - ROW_H);\n  for (let k = 0; k < out.length; k++) out[k] = Math.max(min, out[k]!);\n  const result = new Array<number>(ys.length);\n  order.forEach((o, k) => (result[o.i] = out[k]!));\n  return result;\n}\n\n/**\n * Before and after on two axes. Each line draws itself from left to right,\n * rising lines in blue and falling ones in grey, and new data slides every\n * end to its new place. Tap a label to bring one line forward.\n */\nexport function SlopeChart({\n  series,\n  fromLabel = 'Before',\n  toLabel = 'After',\n  formatValue = (v) => String(Math.round(v)),\n  height = 240,\n  accessibilityLabel,\n  testID,\n  style,\n}: SlopeChartProps) {\n  const { colors, font } = useKinetikTheme();\n  const reduced = useReduceMotion();\n  const [width, setWidth] = useState(0);\n  const [focus, setFocus] = useState<number | null>(null);\n\n  const pad = ROW_H / 2 + 2;\n  const { yFrom, yTo, labelsFrom, labelsTo } = useMemo(() => {\n    const values = series.flatMap((s) => [s.from, s.to]);\n    const lo = Math.min(...values);\n    const hi = Math.max(...values);\n    const span = hi - lo || 1;\n    const y = (v: number) => pad + (1 - (v - lo) / span) * (height - pad * 2);\n    const a = series.map((s) => y(s.from));\n    const b = series.map((s) => y(s.to));\n    return {\n      yFrom: a,\n      yTo: b,\n      labelsFrom: spread(a, ROW_H / 2, height - ROW_H / 2),\n      labelsTo: spread(b, ROW_H / 2, height - ROW_H / 2),\n    };\n  }, [series, height, pad]);\n\n  const xL = LABEL_W;\n  const xR = Math.max(xL + 1, width - LABEL_W);\n  const summary = series\n    .map((s) => `${s.label}: ${formatValue(s.from)} to ${formatValue(s.to)}`)\n    .join('; ');\n\n  return (\n    <View testID={testID} style={style}>\n      <View style={styles.heads}>\n        {[fromLabel, toLabel].map((h, i) => (\n          <Text\n            key={h + i}\n            style={[\n              styles.head,\n              {\n                color: colors.textSubtle,\n                fontSize: font.size.xs,\n                fontWeight: font.weight.semibold,\n              },\n              i === 1 && styles.right,\n            ]}\n          >\n            {h}\n          </Text>\n        ))}\n      </View>\n      <View\n        accessible\n        accessibilityRole=\"image\"\n        accessibilityLabel={`${accessibilityLabel}. ${fromLabel} to ${toLabel}. ${summary}`}\n        style={{ height }}\n        onLayout={(e) => setWidth(e.nativeEvent.layout.width)}\n      >\n        {width > 0 ? (\n          <Canvas style={{ width, height }}>\n            <Line\n              style=\"stroke\"\n              p1={vec(xL, 0)}\n              p2={vec(xL, height)}\n              color={colors.border}\n              strokeWidth={1}\n            />\n            <Line\n              style=\"stroke\"\n              p1={vec(xR, 0)}\n              p2={vec(xR, height)}\n              color={colors.border}\n              strokeWidth={1}\n            />\n            {series.map((s, i) => (\n              <SlopeLine\n                key={s.label}\n                index={i}\n                xL={xL}\n                xR={xR}\n                yL={yFrom[i]!}\n                yR={yTo[i]!}\n                color={s.to >= s.from ? colors.azure : colors.textSubtle}\n                dimmed={focus !== null && focus !== i}\n                strong={focus === i}\n                reduced={reduced}\n              />\n            ))}\n          </Canvas>\n        ) : null}\n        {width > 0\n          ? series.map((s, i) => {\n              const rising = s.to >= s.from;\n              const dimmed = focus !== null && focus !== i;\n              const change = s.to - s.from;\n              return [\n                <Label\n                  key={`${s.label}-l`}\n                  y={labelsFrom[i]!}\n                  side=\"left\"\n                  reduced={reduced}\n                  dimmed={dimmed}\n                  onPress={() => setFocus((f) => (f === i ? null : i))}\n                  text={`${s.label}  ${formatValue(s.from)}`}\n                  color={colors.textMuted}\n                  strong={focus === i}\n                />,\n                <Label\n                  key={`${s.label}-r`}\n                  y={labelsTo[i]!}\n                  side=\"right\"\n                  reduced={reduced}\n                  dimmed={dimmed}\n                  onPress={() => setFocus((f) => (f === i ? null : i))}\n                  text={`${formatValue(s.to)}  ${change >= 0 ? '+' : '−'}${formatValue(Math.abs(change))}`}\n                  color={rising ? colors.azure : colors.textMuted}\n                  strong={focus === i}\n                />,\n              ];\n            })\n          : null}\n      </View>\n    </View>\n  );\n}\n\ntype SlopeLineProps = {\n  index: number;\n  xL: number;\n  xR: number;\n  yL: number;\n  yR: number;\n  color: string;\n  dimmed: boolean;\n  strong: boolean;\n  reduced: boolean;\n};\n\nfunction SlopeLine({ index, xL, xR, yL, yR, color, dimmed, strong, reduced }: SlopeLineProps) {\n  const a = useSharedValue(yL);\n  const b = useSharedValue(yR);\n  const draw = useSharedValue(reduced ? 1 : 0);\n  const fade = useSharedValue(1);\n\n  useEffect(() => {\n    if (reduced) {\n      draw.set(1);\n      return;\n    }\n    // Lines draw in one after another the first time.\n    draw.set(\n      withDelay(\n        index * 90,\n        withTiming(1, { duration: durations.slow * 1.4, easing: easings.standard }),\n      ),\n    );\n  }, [index, reduced, draw]);\n  useEffect(() => {\n    a.set(withSpring(yL, spring('smooth', reduced)));\n    b.set(withSpring(yR, spring('smooth', reduced)));\n  }, [yL, yR, reduced, a, b]);\n  useEffect(() => {\n    fade.set(withTiming(dimmed ? 0.18 : 1, { duration: reduced ? 0 : durations.base }));\n  }, [dimmed, reduced, fade]);\n\n  const p1 = useDerivedValue(() => vec(xL, a.get()));\n  const p2 = useDerivedValue(() => {\n    const d = draw.get();\n    return vec(xL + (xR - xL) * d, a.get() + (b.get() - a.get()) * d);\n  });\n  const endR = useDerivedValue(() => DOT * Math.max(0, Math.min(1, (draw.get() - 0.85) / 0.15)));\n\n  return (\n    <>\n      <Line\n        style=\"stroke\"\n        p1={p1}\n        p2={p2}\n        color={color}\n        strokeWidth={strong ? 3 : 2}\n        strokeCap=\"round\"\n        opacity={fade}\n      />\n      <Circle c={p1} r={DOT} color={color} opacity={fade} />\n      <Circle c={p2} r={endR} color={color} opacity={fade} />\n    </>\n  );\n}\n\ntype LabelProps = {\n  y: number;\n  side: 'left' | 'right';\n  text: string;\n  color: string;\n  strong: boolean;\n  dimmed: boolean;\n  reduced: boolean;\n  onPress: () => void;\n};\n\nfunction Label({ y, side, text, color, strong, dimmed, reduced, onPress }: LabelProps) {\n  const { font } = useKinetikTheme();\n  const top = useSharedValue(y);\n  useEffect(() => {\n    top.set(withSpring(y, spring('smooth', reduced)));\n  }, [y, reduced, top]);\n  const style = useAnimatedStyle(() => ({ top: top.get() - ROW_H / 2 }));\n\n  return (\n    <Animated.View\n      style={[styles.label, side === 'left' ? styles.labelLeft : styles.labelRight, style]}\n      importantForAccessibility=\"no-hide-descendants\"\n      accessibilityElementsHidden\n    >\n      <Pressable onPress={onPress} hitSlop={{ top: 13, bottom: 13 }}>\n        <Text\n          numberOfLines={1}\n          style={{\n            color,\n            opacity: dimmed ? 0.35 : 1,\n            fontSize: font.size.xs,\n            fontWeight: strong ? font.weight.bold : font.weight.medium,\n            textAlign: side === 'left' ? 'right' : 'left',\n            fontVariant: ['tabular-nums'],\n          }}\n        >\n          {text}\n        </Text>\n      </Pressable>\n    </Animated.View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  heads: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 6 },\n  // Each heading sits against its axis, like the labels under it.\n  head: { width: LABEL_W - 8, textAlign: 'right' },\n  right: { textAlign: 'left', width: LABEL_W - 8 },\n  label: { position: 'absolute', height: ROW_H, width: LABEL_W - 10, justifyContent: 'center' },\n  labelLeft: { left: 0 },\n  labelRight: { right: 0 },\n});\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "data",
    "web": "live",
    "perf": {
      "budget": "One Skia canvas of lines and dots, driven by four shared values per series; labels are plain text placed by springs. Nothing runs at rest.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 18,
          "p90Ms": 21,
          "baselineMs": 20,
          "jankyPercent": 2.88,
          "atRest": "idle",
          "memoryMb": 154
        }
      ]
    },
    "a11y": {
      "reducedMotion": "Lines appear drawn and move straight to new values.",
      "screenReader": "One image whose name reads the title, both column headings and every series from start to end value.",
      "touchTarget": "Labels take taps with 13 pt of slop above and below, so 44 pt rows."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { SlopeChart } from '@/components/kinetik/slope-chart';\n\n<SlopeChart\n  accessibilityLabel=\"Share of trips\"\n  fromLabel=\"2025\"\n  toLabel=\"2026\"\n  series={[\n    { label: 'Lisbon', from: 24, to: 31 },\n    { label: 'Kyoto', from: 30, to: 22 },\n  ]}\n  formatValue={(v) => `${v}%`}\n/>;\n```\n\nEach series is one line from its `from` value on the left to its `to` value on the right. The right-hand label shows the new value and the change.\n\n## When to use\n\n- Comparing two moments for a handful of items: this year against last, before and after a change.\n- When the direction and size of each change matters more than the path between.\n\n## When not to use\n\n- More than eight or so series. Labels crowd and lines tangle; use a sorted bar chart of the change.\n- Values over many points in time. Use Line Chart.\n\n## Notes\n\n- Labels on each side are spread apart so they never overlap, while dots stay at their true values.\n- Change `series` and every end slides to its new place on a spring. Lines are keyed by `label`.\n- Tap a label to bring that line forward and dim the rest; tap it again to clear.\n- With reduced motion the lines appear drawn and move without springs.\n"
  },
  "docs": "Prefer `npx kinetik-ui add slope-chart`: it installs dependencies with expo install, so versions match your Expo SDK."
}
