Skip to content

CDL 语法规范 v0.6

概述

CDL(Chart Definition Language)是用于定义数据图表的领域特定语言(DSL)。设计目标:

  • 简洁声明式:像写 Markdown 一样简单
  • AI 友好:结构化、可验证,便于 LLM 生成和修改
  • 安全交付:DSL 不携带数据,权限和数据留在服务端
  • 渐进渲染:核心层必渲染,提示层可选解析
  • ECharts 极简映射:舍弃不高频配置,保留核心能力

三种语法级别

1. 快速语法(Markdown 风格)— 推荐新手

cdl
# 月度销售额

| 月份 | 销售额 |
| --- | --- |
| 1月 | 100 |
| 2月 | 150 |
| 3月 | 200 |

## 折线图

@color "#4fc3f7"
@style "smooth"

自动映射

  • 第一列 → x 轴
  • 第二列 → y 轴
  • 标题关键词 → 图表类型("趋势"→line,"对比"→bar,"占比"→pie)

2. 标准语法(显式声明)— 推荐生产

cdl
Chart 月度销售趋势 {
    use SalesData
    type line
    x month
    y total
    
    @color "#4fc3f7"
    @title "2024年销售趋势"
}

3. 高级语法(多系列 + 坐标轴 + 交互)— v0.6 新增

cdl
# 销售额与利润分析

| 月份 | 销售额 | 利润 |
| --- | --- | --- |
| 1月 | 120 | 15 |
| 2月 | 150 | 20 |
| 3月 | 180 | 25 |

## combo

## series
| field | as | type | color | axis | style |
| --- | --- | --- | --- | --- | --- |
| 销售额 | 销售额(万元) | bar | #4fc3f7 | left | solid |
| 利润 | 利润(万元) | line | #ff9800 | right | smooth |

## axis x
labelRotate: 45

## axis y left
min: 0
max: 200
labelFormatter: "${value}万"

## axis y right
min: 0
max: 50
labelFormatter: "${value}%"

@interaction "tooltip:shared zoom:inside"

数据源定义

内联数据(data)

cdl
@lang(data)
Sales {
    month, amount
    1月, 100
    2月, 150
}

SQL 查询

cdl
@lang(sql)
@source('sales_db')   # 必需:数据源连接名
@timeout(30)          # 可选:超时(秒)
@cache(300)           # 可选:缓存(秒)
SalesData {
    SELECT month, amount FROM sales
}

DAX 查询

cdl
@lang(dax)
@source('powerbi_dataset')
Revenue {
    EVALUATE SUMMARIZE(Sales, Sales[Month], "Revenue", SUM(Sales[Amount]))
}

图表类型

类型说明触发关键词(自动推断)
line折线图趋势、变化、增长、下降
bar柱状图对比、排名、分布、数量
pie饼图占比、构成、百分比
scatter散点图关系、分布、散点
area面积图累积、面积、填充
radar雷达图能力、评估、多边形
heatmap热力图热力、矩阵、密度
combo组合图组合、混合、双轴
gauge仪表盘仪表、进度、达标
candlestick蜡烛图金融K线
boxplot箱线图统计分布
sankey桑基图流向、漏斗
treemap矩形树图层次结构
wordcloud词云图关键词频
liquid水波图填充进度
map地图地理分布

提示层指令(@ 前缀)

指令类型说明示例
@colorstring主色调(HEX)@color "#4fc3f7"
@stylestring样式@style "smooth"
@animationstring动画@animation "ease-out"
@titlestring标题(覆盖默认)@title "自定义标题"
@subtitlestring副标题@subtitle "2024年数据"
@layoutstring布局@layout "compact"
@themelight|dark|auto主题@theme "dark"
@gridboolean显示网格@grid false
@interactionstring交互(见下文)@interaction "tooltip:shared"

高级特性(v0.6 新增)

系列配置(## series

用于多系列图表(combo、分组柱状图等)精细控制每个系列。

cdl
## series
| field | as | type | color | axis | style |
| --- | --- | --- | --- | --- | --- |
| 销售额 | 销售额(万元) | bar | #4fc3f7 | left | solid |
| 利润 | 利润(万元) | line | #ff9800 | right | smooth |

字段说明

  • field:数据列名(必需)
  • as:系列显示名称
  • type:系列类型(combo 图可混合 bar/line)
  • color:HEX 颜色
  • axis:坐标轴 left / right
  • stylesolid / dashed / smooth / marker

坐标轴配置(## axis <position>

cdl
## axis x
type: category
labelRotate: 45

## axis y left
min: 0
max: 200
tickCount: 5
labelFormatter: "${value}万"
splitLine: true

位置xyy2(右轴)、x2(顶轴)或 left/right/top/bottom

配置项

  • type:轴类型(category/value/time/log
  • min/max:范围
  • tickCount:刻度数量
  • labelFormatter:标签格式化(${value} 插值)
  • labelRotate:标签旋转角度
  • splitLine:是否显示网格线

交互声明(@interaction

cdl
@interaction "tooltip:shared legend:true zoom:inside brush:true"
# 或联动
@interaction "tooltip:shared brush:{connect:['chart2']}"

键值对

  • tooltipsingle / shared / none
  • legendtrue / false
  • zoomtrue / inside / slider
  • brushtrue{connect:[]}

AST 类型定义(TypeScript)

typescript
type ChartType = 
    | 'line' | 'bar' | 'pie' | 'scatter' | 'area'
    | 'radar' | 'heatmap' | 'combo' | 'gauge'
    | 'candlestick' | 'boxplot' | 'sankey' 
    | 'treemap' | 'wordcloud' | 'liquid' | 'map';

interface CDLFile {
    version?: string;
    data: DataDefinition[];
    charts: ChartDefinition[];
}

interface DataDefinition {
    type: 'data';
    name: string;
    lang: 'sql' | 'dax' | 'data';
    config: DataSourceConfig;
    query: string;
}

interface DataSourceConfig {
    source?: string;
    timeout?: number;
    cache?: number;
    params?: Record<string, any>;
}

interface ChartDefinition {
    type: 'chart';
    name?: string;
    chartType: ChartType;
    dataSources: string[];
    x?: string;
    y?: string;
    group?: string;
    series?: SeriesConfig[];
    axis?: AxisConfig[];
    interaction?: InteractionConfig;
    where?: string;
    hints: ChartHint;
}

interface SeriesConfig {
    field: string;
    as?: string;
    type?: ChartType;
    color?: string;
    axis?: 'left' | 'right';
    style?: string;
}

interface AxisConfig {
    position: 'x' | 'y' | 'x2' | 'y2' | 'left' | 'right' | 'top' | 'bottom';
    type?: string;
    min?: number | string;
    max?: number | string;
    tickCount?: number;
    labelFormatter?: string;
    labelRotate?: number;
    splitLine?: boolean;
}

interface InteractionConfig {
    tooltip?: 'single' | 'shared' | 'none';
    legend?: boolean;
    zoom?: boolean | 'inside' | 'slider';
    brush?: boolean | { connect: string[] };
}

interface ChartHint {
    style?: string;
    color?: string;
    animation?: string;
    title?: string;
    subtitle?: string;
    layout?: string;
    theme?: 'light' | 'dark' | 'auto';
    grid?: boolean;
}

多图表分隔

使用 --- 分隔多个图表:

cdl
# 图表一
...

---

# 图表二
...

设计原则

  1. 极简主义:最少关键字表达意图
  2. 自然映射:标题→图表,表格→数据,关键词→类型
  3. 渐进增强:核心简单,高级特性可选
  4. AI 优先:结构化、可预测、易生成
  5. ECharts 友好:极简表达,完整映射
  6. 舍弃低频:不追求 100% 配置覆盖,保留 80% 高频场景

文档版本:v0.6(规划中)
更新日期:2026-03-14