Skip to content

API 参考 ​

Text ​

constructor ​

typescript
constructor(container: string | HTMLElement);
参数类型必填说明
containerstring | HTMLElement是容器元素,可以是 DOM 选择器或 DOM 元素

theme ​

参数类型必填说明
theme'dark' | 'light'是主题名称
seedTokenPartial<SeedTokenOptions>否主题令牌配置

返回值

类型说明
TextText 实例,支持链式调用

registerPlugin ​

参数类型必填说明
pluginPluginType | PluginType[]是单个插件或插件数组

返回值

类型说明
TextText 实例,支持链式调用

render ​

使用 T8 语法字符串渲染叙述性文本可视化。

参数类型必填说明
t8Syntaxstring否要渲染的 T8 语法字符串

返回值

类型说明
function卸载函数,移除可视化组件

使用示例

typescript
// 使用 T8 语法字符串渲染
text.theme('light').render(`
  # 销售报告
  总销售额为 [¥1,234,567](metric_value).
`);

clear ​

通过卸载来清除可视化。

参数类型必填说明
(无)

unmount ​

卸载可视化组件。

参数类型必填说明
(无)

Schema ​

NarrativeTextSpec ​

typescript
interface NarrativeTextSpec {
  headline?: HeadlineSpec;
  sections?: SectionSpec[];
}
属性类型必填说明
headlineHeadlineSpec否标题规范
sectionsSectionSpec[]否章节规范数组

SectionSpec ​

typescript
type SectionSpec = (StandardSectionSpec | CustomBlockElement) & CommonProps;
属性类型必填说明
paragraphsParagraphSpec[]否段落规范数组
customTypestring否自定义区块类型
stylesRecord<string, any>否样式对象
classNamestring否类名

ParagraphSpec ​

typescript
type ParagraphSpec = HeadingParagraphSpec | TextParagraphSpec | BulletsParagraphSpec | CustomBlockElement;

enum ParagraphType {
  HEADING1 = 'heading1',
  HEADING2 = 'heading2',
  HEADING3 = 'heading3',
  HEADING4 = 'heading4',
  HEADING5 = 'heading5',
  HEADING6 = 'heading6',
  NORMAL = 'normal',
  BULLETS = 'bullets',
}
属性类型必填说明
typeParagraphType是段落类型
phrasesPhraseSpec[]是短语规范数组
bulletsBulletItemSpec[]否列表项规范数组(仅用于 BULLETS 类型)
customTypestring否自定义区块类型(仅用于自定义区块)

PhraseSpec ​

typescript
type PhraseSpec = TextPhraseSpec | EntityPhraseSpec | CustomPhraseSpec<CustomMetaData>;

enum PhraseType {
  TEXT = 'text',
  ENTITY = 'entity',
  CUSTOM = 'custom',
}
属性类型必填说明
typePhraseType是短语类型
valuestring是短语内容
metadataEntityMetaData | CustomMetaData否元数据

EntityType ​

typescript
type EntityType =
  | 'metric_name' // 指标名称,如 DAU
  | 'metric_value' // 指标值,如 1.23 million
  | 'other_metric_value' // 其他指标值
  | 'contribute_ratio' // 贡献度,如 23%
  | 'delta_value' // 变化值,如 -1.2
  | 'ratio_value' // 变化率,如 +23%
  | 'trend_desc' // 趋势描述,如 up/down
  | 'dim_value' // 维值,如 sex = man
  | 'time_desc' // 时间描述,如 2021-11-19
  | 'proportion'; // 占比,如 20%

插件系统 ​

PluginManager ​

typescript
class PluginManager {
  constructor(plugins?: PluginType[]);
  protected entities: Partial<Record<EntityType, PhraseDescriptor<EntityMetaData>>>;
  protected customPhrases: Record<string, PhraseDescriptor<any>>;
  protected customBlocks: Record<string, BlockDescriptor<any>>;
}

constructor ​

参数类型必填说明
pluginsPluginType[]否插件数组

PhraseDescriptor ​

typescript
interface PhraseDescriptor<MetaData> {
  key: string;
  isEntity: boolean;
  render?: ((value: string, metadata: MetaData) => HTMLElement) | HTMLElement;
  tooltip?:
    | false
    | (Omit<TooltipProps, 'children' | 'title'> & {
        title: ((value: string, metadata: MetaData) => HTMLElement | string | number) | HTMLElement | string | number;
      });
  classNames?: string[] | ((value: string, metadata: MetaData) => string[]);
  style?: CSSProperties | ((value: string, metadata: MetaData, themeSeedToken: SeedTokenOptions) => CSSProperties);
  onHover?: (value: string, metadata: MetaData) => string;
  onClick?: (value: string, metadata: MetaData) => string;
  getText?: (value: string, metadata: MetaData) => string;
  getMarkdown?: (value: string, metadata: MetaData) => string;
}
属性类型必填说明
keystring是插件唯一标识
isEntityboolean是是否为实体短语插件
renderfunction | HTMLElement否渲染函数或元素
tooltipfalse | object否工具提示配置
classNamesstring[] | function否类名数组或生成函数
styleCSSProperties | function否样式对象或生成函数
onHoverfunction否悬停事件处理函数
onClickfunction否点击事件处理函数
getTextfunction否获取纯文本内容函数
getMarkdownfunction否获取 Markdown 内容函数

BlockDescriptor ​

typescript
interface BlockDescriptor<CustomBlockSpec> {
  key: string;
  isBlock: true;
  className?: string | ((spec: CustomBlockSpec) => string);
  style?: CSSProperties | ((spec: CustomBlockSpec) => CSSProperties);
  render?: (spec: CustomBlockSpec) => HTMLElement;
  getText?: (spec: CustomBlockSpec) => string;
  getMarkdown?: (spec: CustomBlockSpec) => string;
}
属性类型必填说明
keystring是插件唯一标识
isBlocktrue是标识为区块插件
classNamestring | function否类名或生成函数
styleCSSProperties | function否样式对象或生成函数
renderfunction否渲染函数
getTextfunction否获取纯文本内容函数
getMarkdownfunction否获取 Markdown 内容函数

预设插件工厂函数 ​

typescript
function createMetricName(
  customDescriptor?: SpecificEntityPhraseDescriptor,
  mode?: CustomEntityMode,
): PhraseDescriptor<EntityMetaData>;
参数类型必填说明
customDescriptorSpecificEntityPhraseDescriptor否自定义描述符
modeCustomEntityMode否合并模式,'merge' 或 'overwrite'

返回值

类型说明
PhraseDescriptor<EntityMetaData>实体短语插件描述符

注:其他预设插件工厂函数(createMetricValue, createDeltaValue 等)的参数和返回值与 createMetricName 相同。

主题系统 ​

SeedTokenOptions ​

typescript
interface SeedTokenOptions {
  // 基础配置
  fontSize: number;
  lineHeight: number;
  fontFamily: string;

  // 颜色系统
  colorBase: string; // 基础文本颜色
  colorEntityBase: string; // 实体基础颜色
  colorHeadingBase: string; // 标题基础颜色
  colorPositive: string; // 正向颜色
  colorNegative: string; // 负向颜色
  colorConclusion: string; // 结论颜色
  colorDimensionValue: string; // 维度值颜色
  colorMetricName: string; // 指标名颜色
  colorMetricValue: string; // 指标值颜色
  colorOtherValue: string; // 其他值颜色
  colorProportionShadow: string; // 占比图表阴影颜色
  colorProportionFill: string; // 占比图表填充颜色
  colorLineStroke: string; // 折线图线条颜色
}
属性类型必填说明
fontSizenumber是基础字号大小
lineHeightnumber是基础行高
fontFamilystring是字体族
colorBasestring是基础文本颜色
colorEntityBasestring是实体基础颜色
colorHeadingBasestring是标题基础颜色
colorPositivestring是正向颜色
colorNegativestring是负向颜色
colorConclusionstring是结论颜色
colorDimensionValuestring是维度值颜色
colorMetricNamestring是指标名颜色
colorMetricValuestring是指标值颜色
colorOtherValuestring是其他值颜色
colorProportionShadowstring是占比图表阴影颜色
colorProportionFillstring是占比图表填充颜色
colorLineStrokestring是折线图线条颜色