diff --git a/apps/android/src/screens/bot/view-chart.tsx b/apps/android/src/screens/bot/view-chart.tsx new file mode 100644 index 0000000..1d4edb2 --- /dev/null +++ b/apps/android/src/screens/bot/view-chart.tsx @@ -0,0 +1,74 @@ +import { useState } from 'react' +import { ScrollView, Text, View } from 'react-native' +import Svg, { Circle, Line, Path, Rect, Text as SvgText } from 'react-native-svg' +import { strings as s } from '../../locales/i18n' +import { objects, text, type BotObject } from '../../services/bot-catalog' +import { Button } from '../../ui/components' +import { useTheme } from '../../ui/theme-context' +import { useThemedStyles } from '../../ui/use-themed-styles' +import { asText, resolve, type Scope } from '../../vendor/ds-bot/views.js' +import { createBotStyles } from './styles' + +/** Geometry from validated numbers only. Exact values remain available as a table. */ +export function BotChart({ node, scope }: { node: BotObject; scope: Scope }) { + const styles = useThemedStyles(createBotStyles) + const { colors } = useTheme() + const [table, setTable] = useState(false) + const categories = (node.categories as unknown[]).map(asText) + const series = objects(node.series).map(entry => ({ label: text(entry.name), values: (entry.values as unknown[]).map(value => { + const number = Number(resolve(value, scope, NaN)) + return Number.isFinite(number) ? number : NaN + }) })) + const palette = [colors.primary, colors.accent, colors.success, colors.warning] + const unit = text(node.unit) + const caption = text(node.caption) + const kind = text(node.kind) || 'bar' + const numbers = series.flatMap(entry => entry.values) + const valid = numbers.every(Number.isFinite) + const magnitude = Math.max(1, ...numbers.filter(Number.isFinite).map(Math.abs)) + const low = Math.min(0, ...numbers.filter(Number.isFinite).map(n => n / magnitude)) + const high = Math.max(0, ...numbers.filter(Number.isFinite).map(n => n / magnitude)) + const span = high - low || 1 + const x = (index: number) => 48 + (index + 0.5) * 284 / categories.length + const y = (value: number) => 164 - (value / magnitude - low) / span * 140 + const baseline = y(0) + const point = (index: number, value: number) => `${x(index)},${y(value)}` + const donut = series[0]?.values ?? [] + const total = donut.reduce((sum, value) => sum + Math.max(0, value / magnitude), 0) + let offset = 0 + return + {!!caption && {caption}} + {valid && + {kind === 'donut' ? <> + + {donut.map((value, index) => { + const length = total > 0 ? Math.max(0, value / magnitude) / total * 2 * Math.PI * 65 : 0 + const at = offset; offset += length + return + })} + : kind === 'hbar' ? <> + + {series.flatMap((entry, group) => entry.values.map((value, index) => { + const zero = 48 + (0 - low) / span * 284 + const end = 48 + (value / magnitude - low) / span * 284 + return + }))} + {categories.map((label, index) => {label.slice(0, 6)})} + : <> + + {Number((high * magnitude).toPrecision(3))} + {Number((low * magnitude).toPrecision(3))} + {series.map((entry, group) => kind === 'line' || kind === 'area' + ? point(index, value)).join(' L ')}${kind === 'area' ? ` L ${x(categories.length - 1)},${baseline} L ${x(0)},${baseline} Z` : ''}`} stroke={palette[group % palette.length]} strokeWidth={2} fill={kind === 'area' ? palette[group % palette.length] : 'none'} fillOpacity={0.15} /> + : entry.values.map((value, index) => ))} + {categories.map((label, index) => {label.slice(0, Math.max(2, Math.floor(36 / categories.length)))})} + } + } + {(kind === 'donut' ? categories : series.map(entry => entry.label)).map((label, i) => {label})} +