Echart
数据看板首要问题,就是我们要解决数据分组的问题。你可以存储到浏览器的websql中通过sql语句groupBy来进行分组统计,或者你自己用js对数据进行分组。由于数据量有限,也就几百个吧,这里采取js方式对数据进行内存分组统计。
1 | export const groupBy = (list, fn) => { |
1 | //引入柱形图,扇形图,地图用于数据可视化展示 |
统计图的数据处理准备
上面,我们定义了一个groupBy方法,传递参数list数据源,fn匿名函数块。通过此方法来将数据源处理返回 {key1:[ ],key2:[ ],…keyN:[ ]} 字典。
另外我们抽出一个业务公共方法processGroupByProperty:只需传递属性名,我们对每个分组集合进行统计金额。最后返回排名前n的数据.{“group1”:money1,”group2”:money2,groupN:”money”} 用于柱形图显示。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
32processGroupByProperty:function(propertyName){
let dataSource = this.dataList;
let chartBarMap = {}; //图表chart需要用的map
//先分组
let groupDic = groupBy(dataSource, (itemObj) => {
return itemObj[propertyName]
});
//统计总和,把对象装到数组,再对数组排序,再取前10装到map
let groupSortArr = [];
Object.keys(groupDic).forEach(function(key) {
let itemArr = groupDic[key];
let sumActualFee = itemArr.reduce(function(total, currentValue) {
return total + currentValue.actualFee;
}, 0);
let keyStr = key.replace('"', '').replace('"', '');
groupSortArr.push({
"name": keyStr,
"value": sumActualFee
});
});
groupSortArr.sort(function(a, b) {
return b.value - a.value
});
let arrCount = groupSortArr.length > 10 ? 10 : groupSortArr.length;
for (let i = arrCount-1; i >= 0; i--) {
let item = groupSortArr[i];
chartBarMap[item.name] = item.value;
}
return chartBarMap;
},
以下方法局部显示了业务各个柱状图的显示处理调用: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
42webSqlOrder: function() {
let dataSource = this.dataList;
//统计总金额。
let totalActualFee = dataSource.reduce(function(total, currentValue) {
return total + currentValue.actualFee;
}, 0);
let totalValueStr = totalActualFee.toFixed(2);
if(totalValueStr.indexOf(".00") > 0){
totalValueStr = totalValueStr.replace(".00","");
}
let tongjiListItem = [{
title: '订单数量',
icon: 'md-person-add',
count: dataSource.length,
color: '#2d8cf0'
},
{
title: '订单总金额',
icon: 'md-locate',
count: totalValueStr,
color: '#19be6b'
},
];
this.inforCardData = tongjiListItem;
//商品名统计
this.productDescMap = this.processGroupByProperty("productDesc");
this.$refs.orderProductDescChartBar.initial(this.productDescMap);
//服务商top10
this.orderUnionMap = this.processGroupByProperty("orderUnion");
this.$refs.orderUnionChartBar.initial(this.orderUnionMap);
//服务商贡献top10
this.shareUnionMap = this.processGroupByProperty("shareUnion");
this.$refs.shareUnionChartBar.initial(this.shareUnionMap);
//...等等
}
以下方法是统计订单各个类型的订单数情况,用于扇形图分布展示1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//订单状态分类
this.orderTypeList = [];
let that = this;
//分组
let orderStatusTextDic = groupBy(dataSource, (itemObj) => {
return itemObj.orderStatusText
});
//统计总和
Object.keys(orderStatusTextDic).forEach(function(key) {
let itemArr = orderStatusTextDic[key];
let keyStr = key.replace('"', '').replace('"', '');
that.orderTypeList.push({
name: keyStr,
value: itemArr.length
});
});
this.$refs.orderStatusChartPie.initial(this.orderTypeList);
以下方法是统计各个身份的订单金额用于地图分布: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
29tongjiTypeListByProperty:function(propertyName){
let dataSource = this.dataList;
let typeList = [];
//分组
let groupDic = groupBy(dataSource, (itemObj) => {
return itemObj[propertyName]
});
//统计总和
Object.keys(groupDic).forEach(function(key) {
if (key !== undefined) {
let itemArr = groupDic[key];
let sumActualFee = itemArr.reduce(function(total, currentValue) {
return total + currentValue.actualFee;
}, 0);
let keyStr = key.replace('"', '').replace('"', '');
typeList.push({
name: keyStr,
value: sumActualFee
});
}
});
return typeList;
},
//地图分布图
this.areaList = this.tongjiTypeListByProperty("userProvince");
this.$refs.orderChartMap.initial(this.areaList);
柱形图
1 | <template> |
扇形图
1 | <template> |
地图
1 | <template> |
页面布局
以下是柱形图,扇形图,地图 三种统计图组件的显示。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<div class="weui-panel weui-panel_access" >
<div class="weui-panel__bd">
<ChartBar ref="orderShopChartBar" style="height:300px" text="店铺销售金额 Top10" />
</div>
</div>
<div class="weui-panel weui-panel_access" >
<div class="weui-panel__bd">
<ChartPie ref="orderStatusChartPie" style="height:300px" text="订单状态对比" />
</div>
</div>
<div class="weui-panel weui-panel_access" >
<div class="weui-panel__bd">
<ChartMap ref="orderChartMap" style="height:300px" text="订单交易数据分布图" />
</div>
</div>
Package.json配置
1 | "dependencies": { |