1. 程式人生 > >Echarts折線圖動態獲取資料例項(附java後臺程式碼)

Echarts折線圖動態獲取資料例項(附java後臺程式碼)

// 基於準備好的dom,初始化echarts例項 var myChart = echarts.init(document.getElementById('line')); // 指定圖表的配置項和資料 var option = { title : { text : '收支明細折線圖', subtext : 'demo' }, tooltip : { show : true
, trigger : 'axis' }, legend : { data : [ "金額" ] //圖例 }, dataZoom : [ { type : 'inside', //支援滑鼠滾動縮放 start : 0, //預設資料初始縮放範圍為10%到90% end : 100 } ], toolbox : { show : true
, feature : { mark : { show : true }, dataView : { show : true, readOnly : false }, magicType : { show : true
, type : [ 'bar', 'line' ] }, restore : { show : true }, saveAsImage : { show : true } } }, calculable : true, xAxis : [ { type : 'category', boundaryGap : false, data : [] } ], yAxis : [ { name : '金額 ', //Y軸提示 type : 'value', //min: 0, //max: 30, // interval: 1, //Y軸資料跨度 axisLabel : { formatter : '{value} 元' //Y軸單位 } } ], series : [ { "name" : "金額", "type" : "line", "data" : [], "smooth" : true, //主題--線條平滑 "barWidth" : "70", //柱子寬度 "symbol" : 'emptycircle', //設定折線圖中表示每個座標點的符號;emptycircle:空心圓;emptyrect:空心矩形;circle:實心圓;emptydiamond:菱形 "markPoint" : { data : [ { type : 'max', name : '最大值' }, { type : 'min', name : '最小值' } ] }, "markLine" : { data : [ { type : 'average', name : '平均值' } ] }, //設定柱狀圖和節點的顏色 itemStyle : { normal : { color : '#228B22', //設定折線的顏色 lineStyle : { color : '#228B22' }, //以下為柱狀圖頂部是否顯示,顯示位置和顯示格式的設定了 label : { show : true, textStyle : { color : '#00CD66' }, position : 'botton', formatter : '\n{b}\n{c}(元)' } } } } //第一條折線 ] }; // 使用剛指定的配置項和資料顯示圖表。 myChart.setOption(option); //myChart.showLoading(); var come = 1; getData(come); function getInfo() { var income = $("input[id='option1']:checked").val(); var outcome = $("input[id='option2']:checked").val(); if (income != null && outcome == null) { come = income; } if (income == null && outcome != null) { come = outcome; } if (income != null && outcome != null) { alert("只能選一種檢視!"); } getData(come); } function getData(come) { var b ="${orderCount.beginCreateDate}"; var e ="${orderCount.endCreateDate}"; var begin = new Date(b).toString(); var end = new Date(e).toString(); var option = myChart.getOption(); $.ajax({ type : "post", async : false, //同步執行 url : "${ctx}/order/orderCount/Echarts", data : { isIncome : come, id : "${orderCount.accountid}", type : "${orderCount.accountType}", beginTime:begin, endTime:end }, dataType : "json", //返回資料形式為json success : function(result) { if (result) { option.legend.data = result.legend; option.xAxis[0].data = result.category; option.series[0].data = result.series[0].data; myChart.hideLoading(); myChart.setOption(option); myChart.hideLoading(); } }, error : function(errorMsg) { alert("圖表請求資料失敗!"); //myChart.hideLoading(); myChart.showLoading(); } }); }