getChartData.ts
2.5 KB
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
export interface ValueItem {
value: number;
itemStyle?: {
color: string;
};
}
export interface ChartData {
categories: string[];
values: ValueItem[];
}
export interface ChartComponentProps {
data: {
categories: string[];
values: ValueItem[];
};
}
export function extractDataFromText(text: string): ChartComponentProps | null {
const startMarkers = ["```javascript", "```json"];
let dataStartIndex = -1;
let markerLength = 0;
// 查找有效的数据块起始标记
for (const marker of startMarkers) {
const index = text.indexOf(marker);
if (index !== -1) {
dataStartIndex = index;
markerLength = marker.length;
break;
}
}
let parsedData: any = null;
// 如果找到有效数据块
if (dataStartIndex !== -1) {
// 查找数据块结束标记
const dataEndIndex = text.indexOf("```", dataStartIndex + markerLength);
if (dataEndIndex !== -1) {
// 提取并清理数据块内容
const dataBlock = text
.slice(dataStartIndex + markerLength, dataEndIndex)
.trim()
.replace(/'/g, '"') // 转换单引号为双引号
.replace(/(\w+)(?=\s*:)/g, '"$1"'); // 为键添加双引号
try {
// 尝试安全解析数据块内容
parsedData = JSON.parse(dataBlock);
} catch (error) {
return null;
}
}
}
// 如果没有找到数据块,尝试将整个文本解析为 ChartData
if (!parsedData) {
try {
const potentialData: any = JSON.parse(text);
// 验证解析后的数据是否符合 ChartData 结构
if (
potentialData &&
Array.isArray(potentialData.categories) &&
potentialData.categories.every((c: any) => typeof c === "string") &&
Array.isArray(potentialData.values) &&
potentialData.values.every(
(v: any) =>
typeof v?.value === "number" &&
v?.itemStyle &&
typeof v.itemStyle?.color === "string",
)
) {
parsedData = potentialData;
}
} catch (error) {
return null; // 如果解析失败,返回 null
}
}
// 如果成功解析了数据并且符合 ChartData 类型,返回
if (
parsedData &&
Array.isArray(parsedData.categories) &&
Array.isArray(parsedData.values) &&
parsedData.values.every(
(v: any) =>
typeof v?.value === "number" &&
v?.itemStyle &&
typeof v.itemStyle?.color === "string",
)
) {
return { data: parsedData as ChartData };
}
return null; // 如果未满足条件,则返回 null
}