Skip to content

API 参考

Text

constructor

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

schema

参数类型必填说明
specNarrativeTextSpec文本可视化规范对象

返回值

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

theme

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

返回值

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

registerPlugin

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

返回值

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

streamRender

参数类型必填说明
newJSONFragmentstring追加并增量解析的 JSON 字符串片段。
optionsobject可选回调:onError (error: string),onComplete (result: T8ClarinetParseResult)

clear

参数类型必填说明
(无)

render

参数类型必填说明
(无)

返回值

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

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折线图线条颜色