Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion __tests__/annotation/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectTypeOf, it } from 'vitest';
import { expect, expectTypeOf, it } from 'vitest';
import type {
Annotation,
AnnotationOptions,
Expand All @@ -11,8 +11,10 @@ import type {
NetworkGraphConfig,
PieConfig,
ScatterConfig,
Summary,
SummaryConfig,
TableConfig,
ThemeStyle,
} from '../../src';
import type { RendererAnnotation } from '../../src/annotation/registry';

Expand Down Expand Up @@ -119,6 +121,30 @@ it('defines runnable configuration examples for primitives, table targets and su
void [missingTarget, unsupportedTable, unsupportedSummary];
});

it('shares Summary inputs across render and unified config and supports consumer narrowing', () => {
type SummaryRenderInput = Parameters<ReturnType<typeof Summary>['render']>[0];
expectTypeOf<SummaryRenderInput>().toEqualTypeOf<SummaryConfig>();
expectTypeOf<SummaryConfig>().toMatchTypeOf<GPTVisConfig>();
expectTypeOf<Exclude<GPTVisConfig, string>['style']>().toMatchTypeOf<ThemeStyle | undefined>();

function getText(config: SummaryConfig): string {
return typeof config === 'string' ? config.trim() : config.text.trim();
}
function getStyle(config: Exclude<GPTVisConfig, string>): ThemeStyle | undefined {
return config.style;
}

expect(getText(' Revenue ')).toBe('Revenue');
expect(getText({ text: ' Revenue ' })).toBe('Revenue');
expect(getStyle({ type: 'summary', text: 'Revenue' })).toBeUndefined();
expect(
getStyle({ type: 'summary', text: 'Revenue', style: { backgroundColor: 'transparent' } }),
).toEqual({ backgroundColor: 'transparent' });
expect(getStyle({ type: 'line', data: [], style: { backgroundColor: 'transparent' } })).toEqual({
backgroundColor: 'transparent',
});
});

it('keeps appearance in style and restricts fields to the annotation kind', () => {
type Point = Extract<Annotation, { type: 'point' }>;
type Path = Extract<Annotation, { type: 'path' }>;
Expand Down
81 changes: 81 additions & 0 deletions __tests__/vis/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,87 @@ describe('render - summary theme', () => {
expect(textInstances[0].syntax).toBe('# Revenue');
});

it('applies local styles over the construction theme to text, background and annotations', () => {
const container = new MockElement();
const instance = Summary({ container: container as unknown as HTMLElement, theme: 'pop' });
instance.render({
text: '# Revenue',
style: {
backgroundColor: 'transparent',
summary: { colorMetricValue: '#2563eb' },
label: { fill: '#123456' },
},
annotations: [{ type: 'highlight', data: { text: 'Revenue' } }],
});

expect(container.children[0].style.backgroundColor).toBe('transparent');
expect(textInstances[0].themeArgs).toEqual([
'light',
expect.objectContaining({ colorMetricValue: '#2563eb', fontSize: 15, lineHeight: 28 }),
]);
expect(bindAnnotations).toHaveBeenLastCalledWith(
expect.objectContaining({
defaults: expect.objectContaining({
labelFill: '#123456',
highlight: expect.objectContaining({ background: 'transparent' }),
}),
}),
);
});

it('accepts per-render themes and restores construction defaults for subsequent inputs', () => {
const container = new MockElement();
const instance = Summary({ container: container as unknown as HTMLElement, theme: 'pop' });
instance.render({
text: '# Revenue',
theme: {
id: 'local-summary',
label: 'Local summary',
baseTheme: 'dark',
style: {
backgroundColor: '#10131a',
summary: { fontFamily: 'Georgia, serif', colorMetricValue: '#2563eb' },
},
},
style: { summary: { colorMetricValue: '#abcdef' } },
});
expect(container.children[0].style.backgroundColor).toBe('#10131a');
expect(textInstances[0].themeArgs).toEqual([
'dark',
expect.objectContaining({ fontFamily: 'Georgia, serif', colorMetricValue: '#abcdef' }),
]);

instance.render('# Updated');
expect(container.children[0].style.backgroundColor).toBe('#fffdf5');
expect(textInstances[1].themeArgs).toEqual([
'light',
expect.objectContaining({ colorMetricValue: '#a90055' }),
]);
instance.render({ text: '# Updated again' });
expect(container.children[0].style.backgroundColor).toBe('#fffdf5');
expect(textInstances[2].themeArgs).toEqual(textInstances[1].themeArgs);
});

it('keeps inline theme overrides working with strings and styled objects', () => {
const container = new MockElement();
const instance = Summary({ container: container as unknown as HTMLElement, theme: 'pop' });
instance.render('theme dark\n# Revenue');
expect(textInstances[0].themeArgs?.[0]).toBe('dark');
expect(textInstances[0].syntax).toBe('# Revenue');

instance.render({
text: 'theme light\n# Updated',
theme: 'dark',
style: { backgroundColor: 'transparent', label: { fill: '#123456' } },
});
expect(textInstances[1].themeArgs?.[0]).toBe('light');
expect(textInstances[1].syntax).toBe('# Updated');
expect(container.children[0].style.backgroundColor).toBe('transparent');
expect(bindAnnotations).toHaveBeenLastCalledWith(
expect.objectContaining({ defaults: expect.objectContaining({ labelFill: '#123456' }) }),
);
});

it('accepts the object API, passes targets to the renderer and clears labels on rerender and destroy', () => {
const container = new MockElement();
const instance = Summary({ container: container as unknown as HTMLElement });
Expand Down
2 changes: 1 addition & 1 deletion skills/chart-visualization/references/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

使用 T8 语法(类 Markdown + 语义标注)直接书写。

需要对可见文字添加外部强调时,可传入对象 `{ "type": "summary", "text": "T8 正文", "annotations": [...] }`。摘要当前仅支持 highlight,`data.text` 精确且唯一匹配可见文字,详见 [标注参考](annotations.md#表格与摘要的数据目标)。正文仍使用下面的 T8 语法,主题由 GPTVis 构造参数提供。
需要对可见文字添加外部强调时,可传入对象 `{ "type": "summary", "text": "T8 正文", "annotations": [...] }`。摘要当前仅支持 highlight,`data.text` 精确且唯一匹配可见文字,详见 [标注参考](annotations.md#表格与摘要的数据目标)。正文仍使用下面的 T8 语法,默认沿用 GPTVis 构造主题;对象可通过 `theme` 覆盖主题,通过 `style` 覆盖当前渲染的样式,例如 `"style": { "backgroundColor": "transparent", "summary": { "colorMetricValue": "#2563eb" } }`。

#### 语义标注语法

Expand Down
31 changes: 28 additions & 3 deletions src/vis/summary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,34 @@

## 配置

| 属性 | 类型 | 是否必填 | 默认值 | 说明 |
| ----- | ---------------------------------- | -------- | ----------- | --------------------------------------------- |
| theme | `'default' \| 'academy' \| 'dark'` | 选填 | `'default'` | 图表主题,`'default'` 和 `'academy'` 均为亮色 |
`SummaryConfig` 统一表达两种渲染输入:原有的 T8 语法字符串,以及包含 `text` 和可选 `annotations` 的对象。直接使用 `Summary` 时,对象的 `type` 可省略;通过 `GPTVis` 渲染对象时,需要指定 `type: 'summary'`。

```ts
import { Summary, type SummaryConfig } from '@antv/gpt-vis';

const summary = Summary({ container: '#container', theme: 'dark' });
summary.render('# 销售报告');
summary.render({
text: '# 销售报告\n\n营收达到 240 万。',
style: { backgroundColor: 'transparent', summary: { colorMetricValue: '#2563eb' } },
annotations: [{ type: 'highlight', data: { text: '240 万' } }],
});

function getText(config: SummaryConfig): string {
return typeof config === 'string' ? config.trim() : config.text.trim();
}
```

调用 `SummaryConfig` 的字符串方法前需要先判断类型。对象形式与其他图表配置一样,支持可选的 `theme` 和 `style`,因此公共代码可以直接访问 `Exclude<GPTVisConfig, string>` 的 `config.style`。

对象中的 `theme` 覆盖构造时的主题,未传入时沿用构造主题;`style` 在主题样式之上覆盖当前这次渲染,也应用于标注样式。后续渲染不会继承上一次的局部覆盖。字符串输入继续使用构造主题,文本中的 `theme light` 或 `theme dark` 仍可切换亮暗模式。

构造选项和对象输入中的 `theme` 均支持内置主题名或可复用的 `ThemeSpec`:

| 属性 | 类型 | 是否必填 | 默认值 | 说明 |
| ----- | ------------------------------- | -------- | ----------- | -------------------------------------- |
| theme | `BuiltinThemeName \| ThemeSpec` | 选填 | `'default'` | 内置主题名或可复用主题 |
| style | `ThemeStyle` | 选填 | — | 仅对象输入支持,覆盖当前渲染的主题样式 |

## 语法

Expand Down
17 changes: 10 additions & 7 deletions src/vis/summary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { bindAnnotations } from '../../annotation';
import type { RendererAnnotation } from '../../annotation/registry';
import { getAnnotationDefaults } from '../../theme/annotation';
import { getThemeObject } from '../../theme/summary';
import type { VisualizationOptions } from '../../types';
import type { ThemeStyle, VisualizationOptions, VisualizationTheme } from '../../types';

/**
* T8 syntax, or an object pairing syntax with text highlight annotations.
Expand All @@ -19,6 +19,8 @@ export type SummaryConfig =
| {
type?: 'summary';
text: string;
theme?: VisualizationTheme;
style?: ThemeStyle;
annotations?: RendererAnnotation<{ text: string }, 'summary', 'summary'>[];
};

Expand Down Expand Up @@ -53,8 +55,6 @@ export interface SummaryInstance {
* ```
*/
export const Summary = (options: VisualizationOptions): SummaryInstance => {
const appearance = getThemeObject('summary', options.theme);
const { theme } = appearance;
const container =
typeof options.container === 'string'
? document.querySelector(options.container)
Expand All @@ -77,11 +77,13 @@ export const Summary = (options: VisualizationOptions): SummaryInstance => {
* Supports an optional `theme <value>` line in the syntax to override the theme.
*/
const render = (config: SummaryConfig): void => {
const { text: syntax, annotations } =
typeof config === 'string' ? { text: config, annotations: undefined } : config;
const input: Exclude<SummaryConfig, string> =
typeof config === 'string' ? { text: config } : config;
const { text: syntax, annotations, theme = options.theme, style: localStyle } = input;
const appearance = getThemeObject('summary', theme, localStyle);

// Extract optional `theme <value>` line from syntax
let activeTheme = theme;
let activeTheme = appearance.theme;
const cleanedSyntax = syntax.replace(/^theme\s+(light|dark)\s*$/im, (_, t) => {
activeTheme = t as 'light' | 'dark';
return '';
Expand Down Expand Up @@ -109,7 +111,8 @@ export const Summary = (options: VisualizationOptions): SummaryInstance => {
annotations,
defaults: getAnnotationDefaults(
'summary',
activeTheme === theme ? options.theme : activeTheme,
activeTheme === appearance.theme ? theme : activeTheme,
localStyle,
),
});
};
Expand Down
Loading