|
| 1 | +<script setup lang="ts"> |
| 2 | +const props = defineProps<{ |
| 3 | + invocation: ChartUIToolInvocation |
| 4 | +}>() |
| 5 | +
|
| 6 | +const color = computed(() => { |
| 7 | + return ({ |
| 8 | + 'output-available': 'bg-gradient-to-br from-sky-400 via-blue-500 to-indigo-600 dark:from-sky-500 dark:via-blue-600 dark:to-indigo-700', |
| 9 | + 'output-error': 'bg-muted text-error' |
| 10 | + })[props.invocation.state as string] || 'bg-muted text-white' |
| 11 | +}) |
| 12 | +
|
| 13 | +const icon = computed(() => { |
| 14 | + return ({ |
| 15 | + 'input-available': 'i-lucide-line-chart', |
| 16 | + 'output-error': 'i-lucide-triangle-alert' |
| 17 | + })[props.invocation.state as string] || 'i-lucide-loader-circle' |
| 18 | +}) |
| 19 | +
|
| 20 | +const message = computed(() => { |
| 21 | + return ({ |
| 22 | + 'input-available': 'Generating chart...', |
| 23 | + 'output-error': 'Can\'t generate chart, please try again' |
| 24 | + })[props.invocation.state as string] || 'Loading chart data...' |
| 25 | +}) |
| 26 | +
|
| 27 | +const xFormatter = (invocation: ChartUIToolInvocation) => { |
| 28 | + return (tick: number, _i?: number, _ticks?: number[]): string => { |
| 29 | + if (!invocation.output?.data[tick]) return '' |
| 30 | + return String(invocation.output.data[tick][invocation.output.xKey] ?? '') |
| 31 | + } |
| 32 | +} |
| 33 | +
|
| 34 | +const categories = (invocation: ChartUIToolInvocation): Record<string, BulletLegendItemInterface> => { |
| 35 | + if (!invocation.output?.series) return {} |
| 36 | + return invocation.output.series.reduce((acc: Record<string, BulletLegendItemInterface>, serie: { key: string, name: string, color: string }) => { |
| 37 | + acc[serie.key] = { |
| 38 | + name: serie.name, |
| 39 | + color: serie.color |
| 40 | + } |
| 41 | + return acc |
| 42 | + }, {} as Record<string, BulletLegendItemInterface>) |
| 43 | +} |
| 44 | +
|
| 45 | +const formatValue = (value: string | number | undefined): string => { |
| 46 | + if (value === undefined || value === null) return 'N/A' |
| 47 | + if (typeof value === 'string') return value |
| 48 | +
|
| 49 | + if (Number.isInteger(value)) { |
| 50 | + return value.toLocaleString() |
| 51 | + } |
| 52 | + return value.toLocaleString(undefined, { |
| 53 | + minimumFractionDigits: 0, |
| 54 | + maximumFractionDigits: 2 |
| 55 | + }) |
| 56 | +} |
| 57 | +</script> |
| 58 | + |
| 59 | +<template> |
| 60 | + <div v-if="invocation.state === 'output-available'"> |
| 61 | + <div v-if="invocation.output.title" class="flex items-center gap-2 mb-2"> |
| 62 | + <UIcon name="i-lucide-line-chart" class="size-5 text-primary shrink-0" /> |
| 63 | + <div class="min-w-0"> |
| 64 | + <h3 class="text-lg font-semibold truncate"> |
| 65 | + {{ invocation.output.title }} |
| 66 | + </h3> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + |
| 70 | + <div class="relative overflow-hidden"> |
| 71 | + <div class="dot-pattern h-full -top-5 left-0 right-0" /> |
| 72 | + |
| 73 | + <LineChart |
| 74 | + :height="300" |
| 75 | + :data="invocation.output.data" |
| 76 | + :categories="categories(invocation)" |
| 77 | + :x-formatter="xFormatter(invocation)" |
| 78 | + :x-label="invocation.output.xLabel" |
| 79 | + :y-label="invocation.output.yLabel" |
| 80 | + :y-grid-line="true" |
| 81 | + :curve-type="CurveType.MonotoneX" |
| 82 | + :legend-position="LegendPosition.Top" |
| 83 | + :hide-legend="false" |
| 84 | + :x-num-ticks="Math.min(6, invocation.output.data.length)" |
| 85 | + :y-num-ticks="5" |
| 86 | + :show-tooltip="true" |
| 87 | + > |
| 88 | + <template #tooltip="{ values }"> |
| 89 | + <div class="bg-muted/50 rounded-sm px-2 py-1 shadow-lg backdrop-blur-sm max-w-xs ring ring-offset-2 ring-offset-(--ui-bg) ring-default border border-default"> |
| 90 | + <div v-if="values && values[invocation.output.xKey]" class="text-sm font-semibold text-highlighted mb-2"> |
| 91 | + {{ values[invocation.output.xKey] }} |
| 92 | + </div> |
| 93 | + <div class="space-y-1.5"> |
| 94 | + <div |
| 95 | + v-for="serie in invocation.output.series" |
| 96 | + :key="serie.key" |
| 97 | + class="flex items-center justify-between gap-3" |
| 98 | + > |
| 99 | + <div class="flex items-center gap-2 min-w-0"> |
| 100 | + <div |
| 101 | + class="size-2.5 rounded-full shrink-0" |
| 102 | + :style="{ backgroundColor: serie.color }" |
| 103 | + /> |
| 104 | + <span class="text-sm text-muted truncate">{{ serie.name }}</span> |
| 105 | + </div> |
| 106 | + <span class="text-sm font-semibold text-highlighted shrink-0"> |
| 107 | + {{ formatValue(values?.[serie.key]) }} |
| 108 | + </span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </template> |
| 113 | + </LineChart> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div v-else class="rounded-xl px-5 py-4" :class="color"> |
| 118 | + <div class="flex items-center justify-center h-44"> |
| 119 | + <div class="text-center"> |
| 120 | + <UIcon |
| 121 | + :name="icon" |
| 122 | + class="size-8 mx-auto mb-2" |
| 123 | + :class="[invocation.state === 'input-streaming' && 'animate-spin']" |
| 124 | + /> |
| 125 | + <div class="text-sm"> |
| 126 | + {{ message }} |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | +</template> |
| 132 | + |
| 133 | +<style> |
| 134 | +:root { |
| 135 | + --vis-tooltip-padding: 0 !important; |
| 136 | + --vis-tooltip-background-color: transparent !important; |
| 137 | + --vis-tooltip-border-color: transparent !important; |
| 138 | +
|
| 139 | + --vis-axis-grid-color: rgba(255, 255, 255, 0) !important; |
| 140 | + --vis-axis-tick-label-color: var(--ui-text-muted) !important; |
| 141 | + --vis-axis-label-color: var(--ui-text-toned) !important; |
| 142 | + --vis-legend-label-color: var(--ui-text-muted) !important; |
| 143 | +
|
| 144 | + --dot-pattern-color: #111827; |
| 145 | +} |
| 146 | +
|
| 147 | +.dark { |
| 148 | + --dot-pattern-color: #9ca3af; |
| 149 | +} |
| 150 | +
|
| 151 | +.dot-pattern { |
| 152 | + position: absolute; |
| 153 | + background-image: radial-gradient(var(--dot-pattern-color) 1px, transparent 1px); |
| 154 | + background-size: 7px 7px; |
| 155 | + background-position: -8.5px -8.5px; |
| 156 | + opacity: 20%; |
| 157 | + mask-image: radial-gradient(ellipse at center, rgba(0, 0, 0, 1), transparent 75%); |
| 158 | +} |
| 159 | +</style> |
0 commit comments