具体的说是 Echarts.js 中使用的 JSON 格式
参考:
https://www.echartsjs.com/examples/edit ... area-stack
option = {
title: {
text: '堆叠区域图'
},
tooltip : {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
legend: {
data:['邮件营销','联盟广告','视频广告','直接访问','搜索引擎']
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis : [
{
type : 'category',
boundaryGap : false,
data : ['周一','周二','周三','周四','周五','周六','周日']
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'邮件营销',
type:'line',
stack: '总量',
areaStyle: {},
data:[120, 132, 101, 134, 90, 230, 210]
},
{
name:'联盟广告',
type:'line',
stack: '总量',
areaStyle: {},
data:[220, 182, 191, 234, 290, 330, 310]
},
{
name:'视频广告',
type:'line',
stack: '总量',
areaStyle: {},
data:[150, 232, 201, 154, 190, 330, 410]
},
{
name:'直接访问',
type:'line',
stack: '总量',
areaStyle: {normal: {}},
data:[320, 332, 301, 334, 390, 330, 320]
},
{
name:'搜索引擎',
type:'line',
stack: '总量',
label: {
normal: {
show: true,
position: 'top'
}
},
areaStyle: {normal: {}},
data:[820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
Parser 的部分不自己写,使用现成的工具 —— JSON 模块
但会遇到一些格式约束的问题,比如有很多关键字没有带双引号,有很多是单引号,用正则做一些替换之后可以正确转换。
use Encode;
use File::Slurp;
use Data::Dump qw/dd dump/;
use JSON qw/encode_json decode_json from_json/;
STDOUT->autoflush(1);
my $s = read_file("original.json");
# 处理关键字未引用的问题
$s =~s/(\w+):/"$1":/g;
$s =~s/(\w+)\s+:/"$1":/g;
# 单引号转双引号
$s =~s/'/"/g;
$s =~s/option =//;
$s =~s/};/}/;
#print encode('gbk', decode('utf8', $s));
dd from_json($s, {utf8=>1});
要求是,输出 Perl 的数据格式时,该缩进的时候缩进,尽可能接近原来JSON的风格。在遇到一序列数组的时候,比如
data : ['周一','周二','周三','周四','周五','周六','周日'],不要每个数值都换行缩进:
legend => {
data => [
"\x{90AE}\x{4EF6}\x{8425}\x{9500}",
"\x{8054}\x{76DF}\x{5E7F}\x{544A}",
"\x{89C6}\x{9891}\x{5E7F}\x{544A}",
"\x{76F4}\x{63A5}\x{8BBF}\x{95EE}",
"\x{641C}\x{7D22}\x{5F15}\x{64CE}",
],
},
反过来,从 Perl 数据格式转为JSON,也要尽量接近最初该JSON的风格,这个输出要手写函数。
(JSON 的 encode_json 和 to_json 函数输出的效果都差很远,即使使用不同的参数。)