File size: 8,576 Bytes
25067e7
 
216c0a4
 
 
 
25067e7
 
 
216c0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
---
title: ChartPipeline
emoji: 📊
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
---

# ChartPipeline Framework

![ChartPipeline Logo](./assets/logo.png)

## Hugging Face Space

This repository includes a Docker-based Hugging Face Space entrypoint in `app.py`.
The Space runs a self-contained chart-engine demo that renders SVG charts from
bundled D3/ECharts templates and does not require API keys or external resource
pools.

Local smoke test:

```bash
python modules/chart_engine/chart_engine.py \
  --input examples/chart_engine_sample.json \
  --name donut_plain_chart_01 \
  --output /tmp/chartpipeline_sample.svg
```

Full pipeline runs still need a private `config.py`. Start from
`config.example.py`, set `OPENAI_API_KEY`/`OPENAI_BASE_URL` and resource path
environment variables, then copy it to `config.py` on the target machine. Do not
commit or upload a real `config.py`.

## 简介

ChartPipeline 是一个模块化的数据可视化生成框架,将原始数据转换为图表。框架包含九个模块,自动推荐图表类型、提取数据洞察、生成标题、配色方案和视觉元素。

## 架构

```
输入数据 → [1.图表类型推荐] → [2.数据洞察] → [3.标题生成] → [4.布局推荐] → [5.色彩推荐] → [6.图像推荐] → [7.图表生成] → [8.标题样式化] → [9.布局优化] → 最终输出
```

模块1-6:通过JSON对象传递数据,每个模块添加特定字段  
模块7-8:基于JSON配置生成SVG元素  
模块9:合成最终可视化作品

## 数据格式

### 输入格式

```json
{
  "description": "A global restaurant chain analyzes market share in major cities to inform expansion strategy and resource allocation.",
  "data": {
    "data": [
      {
        "City": "New York",
        "Revenue": 8500.0
      },
      {
        "City": "Tokyo",
        "Revenue": 9200.0
      },
      {
        "City": "Sao Paulo",
        "Revenue": 6800.0
      }
    ],
    "columns": [
      {
        "name": "City",
        "description": "City where the restaurant is located",
        "data_type": "categorical",
        "unit": ""
      },
      {
        "name": "Revenue",
        "description": "Annual restaurant industry revenue",
        "data_type": "numerical",
        "unit": "$m"
      }
    ],
    "type_combination": "categorical + numerical"
  },
  "metadata": {
    "title": "Restaurant Industry Revenue: New York vs. Tokyo vs. Sao Paulo",
    "description": "A global restaurant chain analyzes market share in major cities to inform expansion strategy and resource allocation.",
    "main_insight": "Tokyo consistently leads in restaurant industry revenue, demonstrating a strong and growing market compared to New York and Sao Paulo."
  }
}
```

### 处理后格式

```json
{
  "description": "数据描述",
  "data": { "data": [...], "columns": [...], "type_combination": "..." },
  "metadata": { "title": "...", "description": "...", "main_insight": "..." },
  "titles": {
    "main_title": "主标题",
    "sub_title": "副标题"
  },
  "secondary_data": [],
  "variables": {
    "width": 600,
    "height": 600,
    "has_rounded_corners": false,
    "has_shadow": false,
    "has_spacing": false,
    "has_gradient": false,
    "has_stroke": false
  },
  "typography": {
    "title": { "font_family": "Arial", "font_size": "28px", "font_weight": 700 },
    "description": { "font_family": "Arial", "font_size": "16px", "font_weight": 500 },
    "label": { "font_family": "Arial", "font_size": "16px", "font_weight": 500 },
    "annotation": { "font_family": "Arial", "font_size": "12px", "font_weight": 400 }
  },
  "processed": true,
  "datafacts": [
    {
      "type": "value",
      "subtype": "max",
      "data_points": [...],
      "score": 0.92,
      "annotation": "数据洞察注释",
      "reason": "洞察理由"
    }
  ],
  "colors": {
    "field": { "Tokyo": "#9e1214", "New York": "#6381f1" },
    "other": { "primary": "#9e1214", "secondary": "#5ea9f6" },
    "available_colors": [...],
    "background_color": "#E2F1F6",
    "text_color": "#414141"
  },
  "colors_dark": {
    "field": { "Tokyo": "#fb484a", "New York": "#b5c4fb" },
    "other": { "primary": "#fb484a", "secondary": "#abd4fe" },
    "available_colors": [...],
    "background_color": "#203a43",
    "text_color": "#a38e8e"
  },
  "images": {
    "other": { "primary": "base64_encoded_image_data" }
  }
}
```

## 模块详解

### 1. 图表类型推荐模块 (chart_type_recommender)

分析数据特征并推荐合适的图表类型。

**输入**: 初始数据对象

**输出**:
```json
{
  "chart_type": [
    {
      "type": "vertical_stacked_bar_chart",
      "confidence": 0.92,
      "reasoning": "选择堆叠柱状图是因为需要比较不同时间段内两种状态的分布情况,同时展示总量变化趋势"
    }
  ]
}
```

### 2. 数据洞察模块 (datafact_generator)

分析数据并提取关键洞察。

**输入**: 包含chart_type的数据对象

**输出**:
```json
{
  "datafacts": [
    {
      "type": "trend",
      "score": 0.95,
      "annotation": "紧急状态宣布数量总体呈上升趋势",
      "reason": "从1976年至2022年,每十年宣布的国家紧急状态总数从1个增长到平均每十年超过15个"
    }
  ]
}
```

### 3. 标题生成模块 (title_generator)

基于数据内容和洞察生成标题和副标题。

**输入**: 包含chart_type和datafacts的数据对象

**输出**:
```json
{
  "titles": {
    "main_title": "美国国家紧急状态持续增长",
    "sub_title": "1976年以来宣布的紧急状态中大多数仍然有效"
  }
}
```

### 5. 色彩推荐模块 (color_recommender)

为可视化推荐配色方案。

**输入**: 包含chart_type、datafacts、titles和layout的数据对象

**输出**:
```json
{
  "colors": {
    "field": { "US": "blue", "China": "red" },
    "other": { "primary": "#E63946", "secondary": "#457B9D" },
    "available_colors": ["#A9D700", "#FFD700"],
    "background_color": "#FFFFFF",
    "text_color": "#1D3557"
  }
}
```

### 6. 图像推荐模块 (image_recommender)

推荐与图表内容相关的图像和图标元素。

**输入**: 包含chart_type、datafacts、titles、layout和colors的数据对象

**输出**:
```json
{
  "images": {
    "field": {
      "Still active": "data:image/svg+xml;base64,...",
      "Ended": "data:image/svg+xml;base64,..."
    },
    "other": {
      "primary": "data:image/svg+xml;base64,...",
      "man": "data:image/svg+xml;base64,..."
    }
  }
}
```

### 7. 图表模板实现引擎 (chart_engine)

基于前面模块的输出生成图表的SVG表示。支持ECharts和D3.js渲染引擎。

**输入**: JSON数据对象

**输出**:
```json
{
  "chart_svg": "<svg width=\"800\" height=\"500\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>"
}
```

### 8. 标题元素生成模块 (title_styler)

为标题和副标题生成SVG元素。

**输入**: JSON数据对象

**输出**:
```json
{
  "title_svg": "<svg width=\"800\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">...</svg>"
}
```

## 使用方式

### 安装

```bash
git clone https://github.com/yourusername/ChartPipeline.git
cd ChartPipeline
pip install -r requirements.txt
```

### 信息图生成(推荐)

```bash
# 批量处理
python pipeline.py --modules infographics_generator --input /path/to/input/data --output /path/to/output --threads 12

# 指定图表类型
python pipeline.py --modules infographics_generator --input /path/to/input/data --output /path/to/output --threads 12 --chart-name donut_chart_03_d3_hand
```

### 完整流水线

```bash
# 单个文件处理(模块1-9)
python pipeline.py --modules all --input /path/to/data.json --output /path/to/output --threads 1
```

### 图表引擎独立使用

```bash
# 生成SVG图表
python -m modules.chart_engine.chart_generation --input /path/to/data.json --output chart.svg --name chart_type_name

# 生成HTML调试版本
python -m modules.chart_engine.chart_generation --input /path/to/data.json --output chart.svg --name chart_type_name --html debug.html
```

### 参数说明

- `--modules`: 模块集合(`infographics_generator` 或 `all`)
- `--input`: 输入数据路径
- `--output`: 输出路径
- `--threads`: 并行线程数
- `--chart-name`: 图表类型名称(可选)
- `--html`: 输出HTML调试文件(仅chart_engine支持)

## 扩展

- [如何编写Chart variation](docs/how_to_write_a_variation.md)  
- [图表类型文档](docs/chart_types_documentation.md)

## 示例

请参考 `test/` 目录下的示例数据和输出。