刚接触了柱状图,将自己的一些成果分享一下
实现的效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/bc59bdd54d651262ff659510e9640be4.png)
废话就不多说了,代码展示给大家看
后端代码:
public class Monitor {
private Integer id;
private String monitorName;
private String name;
private Integer count1;
private Integer count2;
}
@RequestMapping(value="/findMonitor",produces="application/json;charset=utf-8")
@ResponseBody
public void findMonitor(Monitor monitor,Map<String, Object> map) throws Exception{
List<Monitor> list=monitorService.findMonitor(monitor);
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
response.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
out.println(jsonArray);
out.flush();
out.close();
}
前端:
<body>
<div id="main"></div>
</body>
<script type="text/javascript">
$(function(){
require.config({
paths: {
echarts: 'http://echarts.baidu.com/build/dist'
}
});
// 使用
require(
[
'echarts',
'echarts/chart/line',//需要折线图则加载line模块
'echarts/chart/bar'//使用柱状图就加载bar模块,按需加载
],
function (ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById('main'),'macarons');
var option = {
title : {
text: '',
subtext: '',
x:'center',
subtarget :'blank',
subtextStyle : {
color :'black',
fontWeight :'normal',
fontSize :'14'
}
},
tooltip : {
trigger : 'axis',
axisPointer : { // 坐标轴指示器,坐标轴触发有效
type : 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
legend : {
data : ["水压","电流","温度"]
},
calculable : true,
xAxis : [
{
type : 'value',
name : '次'
}
],
yAxis : [
{
type : 'category',
name : '监测点名称',
data : (function(){
var arr = [];
$.ajax({
type : "post",
async : false,//同步执行
url : "url",
data : {},
dataType : "json",
success : function(result){
if(result){
for(var i=0;i<result.length;i++){
arr.push(result[i].monitorName);//监测点名称
}
}
},
error : function(errorMsg){
alert("不好意思,图标请求数据失败!");
myChart.hideLoading();
}
})