(一)数据格式
data = [
{"RYLB_TEXT": "xxxx1","A01008_BA_2": 15, "A01008_BA_1": 20,},
{ "RYLB_TEXT": "xxxx2","A01008_BA_2": 12,"A01008_BA_1": 18,}
];
orgOptions = {
"keys": [{"col": "RYLB_TEXT",},],
"values": [
{ "name": "",
"cols": [
{ "col": "A01008_BA_1",
"alias": "当前资金余额",'
BAMetaAttributes': {'color2': 'rgba(89, 201, 253, 0.5)'}
},
{"col": "A01008_BA_2",
"alias": "期末可用余额",
'BAMetaAttributes': { 'color2': 'rgba(89, 201, 253, 1)'}
}
]
}]
};
(二)处理数据
因为 ECharts 中 option 需要的数据不同 例如 series 柱状图中 series 是数组类的值 这时候我们就需要将data的数据处理成 {name:xxxx1, type: 'bar', data:[15,12]} 这种格式
(三)方法处理
var years = data.map(item => item[orgOptions.keys[0].col]);
var categories = orgOptions.values[0].cols.map(col => col.alias);
var seriesData = categories.map((category, index) => {
return {
name: category,
type: 'bar',
data: data.map(item => item[orgOptions.values[0].cols[index].col])
};
});
(四)放入option
var option = {
xAxis: {
type: 'category',
data: years
},
yAxis: {
type: 'value'
},
legend:{
top: '10%',
data: categories
},
series: seriesData
};