示例:接收JSon数据并转为Echarts表格

实现效果如下:

Debug拿到AyjgReportController.java传过来的“last7days”数据如下:

[{counts=98.0, date=06-29, endDate=, money=10.70, moneyDf=0.0, startDate=}, {counts=96.0, date=07-01, endDate=, money=8.64, moneyDf=0.0, startDate=}, {counts=107.0, date=06-28, endDate=, money=12.52, moneyDf=0.0, startDate=}, {counts=120.0, date=07-03, endDate=, money=11.28, moneyDf=0.0, startDate=}, {counts=184.0, date=07-02, endDate=, money=14.89, moneyDf=0.0, startDate=}, {counts=0.0, date=06-30, endDate=, money=0.00, moneyDf=0.0, startDate=}, {counts=109.0, date=07-04, endDate=, money=11.05, moneyDf=0.0, startDate=}]

 

即JSon格式如下:

{
	"last7days": [{
		"counts": 98.0,
		"date": "06-29",
		"endDate":,
		"money": 10.70,
		" moneyDf":,
		"startDate": 
	}, {
		"counts": 96.0,
		"date": "07-01",
		"endDate":,
		"money": 8.64,
		" moneyDf":,
		"startDate": 
	}]
}

思路如下:

将JSon数据一条一条取出,其中counts、date、money分别放在三个数组,

使用Echarts创建两个option,counts数组作为第一个option的series,money作为第二个option的series,date作为共用的xAxis横坐标。

创建数组:

var days_counts = new Array();

var days_money = new Array();

var days_date = new Array();

 

循环取出数据放在数组中:

<c:forEach var="var" items="${last7days}">

    days_counts.push('${var.counts}');

    days_money.push('${var.money}');

    days_date.push('${var.date}');

</c:forEach>

 

创建函数将数组数据生成Echarts表中:

function chart(chart_1, chart_2, data_1, data_2, scale) {

    var chart_01 = echarts.init(document.getElementById(chart_1));

    var chart_02 = echarts.init(document.getElementById(chart_2));

    var option_01 = {}

    var option_02 = {}

    chart_01.setOption(option_01);

    chart_02.setOption(option_02);

}

chart_1、和chart_2是两个html  div定位元素,(因为要做两张表格,一个朝上一个朝下,一个div放一个表格)

data_1、data_2是分别是money和counts,即柱状图的数据,

scale是横坐标轴坐标。

 

最后调用函数

chart('main_01', 'main_02', days_money, days_counts, days_date);

 

function函数如下:

function chart(chart_1, chart_2, data_1, data_2, scale) {
			// 基于准备好的dom,初始化echarts实例
			var chart_01 = echarts.init(document.getElementById(chart_1));
			var chart_02 = echarts.init(document.getElementById(chart_2));

			// 指定图表的配置项和数据
			var option_01 = {

				tooltip : {
					show : false,
				},

				xAxis : {
					data : scale,
					show : false,

				},
				yAxis : {
					inverse : false,
					name : '(万元)          ',
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				series : [ {
					name : '金额',
					type : 'bar',
					data : data_1,
					barWidth : 15,//柱图宽度
					color : "#59c570",
					itemStyle : {
						normal : {
							//柱形图圆角,初始化效果
							barBorderRadius : [ 10, 10, 0, 0 ],
							label : {
								show : true,
								position : 'top',
								textStyle : {
									color : '#333'
								},
							}
						},
					}
				} ]
			};
			var option_02 = {

				tooltip : {
					show : false,
				},

				xAxis : {
					data : scale,
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				yAxis : {
					inverse : true,
					name : '(个)         ',
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				series : [ {
					name : '人数',
					type : 'bar',
					data : data_2,
					barWidth : 15,//柱图宽度
					color : "#76c3e5",
					itemStyle : {
						normal : {
							//柱形图圆角,初始化效果
							barBorderRadius : [ 0, 0, 10, 10 ],
							label : {
								show : true,
								position : 'bottom',
								textStyle : {
									color : '#333'
								},
							}
						}
					},
				} ]
			};

			// 使用刚指定的配置项和数据显示图表。
			chart_01.setOption(option_01);
			chart_02.setOption(option_02);
		};

 

ayjg.jsp中的JavaScript代码如下,

	<script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts.js"></script>
	<script
		src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts.common.js"></script>
	<script type="text/javascript">
		function chart(chart_1, chart_2, data_1, data_2, scale) {
			// 基于准备好的dom,初始化echarts实例
			var chart_01 = echarts.init(document.getElementById(chart_1));
			var chart_02 = echarts.init(document.getElementById(chart_2));

			// 指定图表的配置项和数据
			var option_01 = {

				tooltip : {
					show : false,
				},

				xAxis : {
					data : scale,
					show : false,

				},
				yAxis : {
					inverse : false,
					name : '(万元)          ',
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				series : [ {
					name : '金额',
					type : 'bar',
					data : data_1,
					barWidth : 15,//柱图宽度
					color : "#59c570",
					itemStyle : {
						normal : {
							//柱形图圆角,初始化效果
							barBorderRadius : [ 10, 10, 0, 0 ],
							label : {
								show : true,
								position : 'top',
								textStyle : {
									color : '#333'
								},
							}
						},
					}
				} ]
			};
			var option_02 = {

				tooltip : {
					show : false,
				},

				xAxis : {
					data : scale,
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				yAxis : {
					inverse : true,
					name : '(个)         ',
					axisLabel : {
						show : true,
						textStyle : {
							color : '#cecccc'
						}
					},
					axisLine : {
						lineStyle : {
							color : '#cecccc',
						}
					}
				},
				series : [ {
					name : '人数',
					type : 'bar',
					data : data_2,
					barWidth : 15,//柱图宽度
					color : "#76c3e5",
					itemStyle : {
						normal : {
							//柱形图圆角,初始化效果
							barBorderRadius : [ 0, 0, 10, 10 ],
							label : {
								show : true,
								position : 'bottom',
								textStyle : {
									color : '#333'
								},
							}
						}
					},
				} ]
			};

			// 使用刚指定的配置项和数据显示图表。
			chart_01.setOption(option_01);
			chart_02.setOption(option_02);
		};
		
		//获取动态数据
		var days_counts = new Array();
		var days_money = new Array();
		var days_date = new Array();
		var weeks_counts = new Array();
		var weeks_money = new Array();
		var weeks_date = new Array();
		var year_counts = new Array();
		var year_money = new Array();
		var year_date = new Array();
		<c:forEach var="var" items="${last7days}">
		days_counts.push('${var.counts}');
		days_money.push('${var.money}');
		days_date.push('${var.date}');
		</c:forEach>
		<c:forEach var="var" items="${last4Weeks}">
		weeks_counts.push('${var.counts}');
		weeks_money.push('${var.money}');
		weeks_date.push('${var.date}');
		</c:forEach>
		<c:forEach var="var" items="${last6Months}">
		year_counts.push('${var.counts}');
		year_money.push('${var.money}');
		year_date.push('${var.date}');
		</c:forEach>

		var scale = [ "04-17", "04-18", "04-19", "04-20", "04-21", "04-22",
				"04-23" ]; //坐标刻度
		chart('main_01', 'main_02', days_money, days_counts, days_date);
		chart('main_03', 'main_04', weeks_money, weeks_counts, weeks_date);
		chart('main_05', 'main_06', year_money, year_counts, year_date);
		more(".btn-more", 10);
		more(".btn-item-more", 70);
	</script>

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值