json数据:
{ "YD1": 0, "YD2": 0, "YD3": 0, "YD4": 0, "YD5": 0, "YD6": 0, "YD7": 0, "YD8": 0, "YD9": 0, "YD10": 0, "YD11": 0, "YD12": 0, "GH1": 0, "GH2": 0, "GH3": 0, "GH4": 0, "GH5": 0, "GH6": 0, "GH7": 0, "GH8": 0, "GH9": 0, "GH10": 0, "GH11": 0, "GH12": 0, "KD1": 0, "KD2": 0, "KD3": 0, "KD4": 0, "KD5": 0, "KD6": 0, "KD7": 0, "KD8": 0, "KD9": 0, "KD10": 0, "KD11": 0, "KD12": 0, "TYGQ1": 0, "TYGQ2": 0, "TYGQ3": 0, "TYGQ4": 0, "TYGQ5": 0, "TYGQ6": 0, "TYGQ7": 0, "TYGQ8": 0, "TYGQ9": 0, "TYGQ10": 0, "TYGQ11": 0, "TYGQ12": 0, }
ajax:
var bardatas; //定义全局变量 function CMOneImg(){ $.ajax( { type : "POST", url : '/ailk-visual/grossprofit/mendian2all.do?random='+Math.random(), dataType:'json', data:{//+'&monthId=201701&oid=5101011167302' monthId : 201701 00, mdId :'5101011167302', }, success : function(data) { // console.log(JSON.stringify(data)); bardatas = data; }, error: function(data){ console.log(data); } }); }
第一种方法:
js:
var ydArr = [],kdArr=[]; function addJsonArr(){ for (var item in bardatas) {//bardatas指要循环的json对象 // console.log(item);//得到键 // console.log(dataJson[item]);//得到键对应的值 if(item.indexOf('YD')==0){//移动 以YD开头的添加到jsonArr数组中 var obj01 = {obj1:bardatas[item]}; ydArr.push(obj01); } if(item.indexOf('KD')==0){//款待 以YD开头的添加到jsonArr数组中 var obj02 = {obj1:bardatas[item]}; kdArr.push(obj02); } /* //转成数组 if(item.indexOf('KD')==0){//款待 以YD开头的添加到jsonArr数组中 var obj02 = bardatas[item]; //去掉obj1: kdArr.push(obj02); } */ } console.log(ydArr); console.log(kdArr); var datas={}; datas.ydArr = ydArr;datas.kdArr = kdArr; StoreSalesLine('StoreSalesLine002', months, datas);//折线图 ①首先定义全局变量months=[ ],②ajax中success请求months = data.sort().reverse();③数据处理 }
echarts:
function Line(id, months, datas){ x轴: data:months series:[ { 数据一:data:datas.ydArr },{ 数据二:data:datas.kdArr } ] }
第二种方法:
js:
function CLine(){ var datas = {}; var yd = [], kd = [], gh = [], tygq = []; var months = []; yd.push(bardatas.YD1);yd.push(bardatas.YD2);yd.push(bardatas.YD3);yd.push(bardatas.YD4);yd.push(bardatas.YD5);yd.push(bardatas.YD6); yd.push(bardatas.YD7);yd.push(bardatas.YD8);yd.push(bardatas.YD9);yd.push(bardatas.YD10);yd.push(bardatas.YD11);yd.push(bardatas.YD12); kd.push(bardatas.KD1);kd.push(bardatas.KD2);kd.push(bardatas.KD3);kd.push(bardatas.KD4);kd.push(bardatas.KD5);kd.push(bardatas.KD6); kd.push(bardatas.KD7);kd.push(bardatas.KD8);kd.push(bardatas.KD9);kd.push(bardatas.KD10);kd.push(bardatas.KD11);kd.push(bardatas.KD12); gh.push(bardatas.GH1);gh.push(bardatas.GH2);gh.push(bardatas.GH3);gh.push(bardatas.GH4);gh.push(bardatas.GH5);gh.push(bardatas.GH6); gh.push(bardatas.GH7);gh.push(bardatas.GH8);gh.push(bardatas.GH9);gh.push(bardatas.GH10);gh.push(bardatas.GH11);gh.push(bardatas.GH12); tygq.push(bardatas.TYGQ1);tygq.push(bardatas.TYGQ2);tygq.push(bardatas.TYGQ3);tygq.push(bardatas.TYGQ4);tygq.push(bardatas.TYGQ5);tygq.push(bardatas.TYGQ6); tygq.push(bardatas.TYGQ7);tygq.push(bardatas.TYGQ8);tygq.push(bardatas.TYGQ9);tygq.push(bardatas.TYGQ10);tygq.push(bardatas.TYGQ11);tygq.push(bardatas.TYGQ12); var yd1= /[YD0-9] /; // 正则,只包含YD和任意0-9的数字 var kd1= /[KD0-9] /; var gh1= /[GH0-9] /; var tygq1= /[TYGQ0-9] /; for(var key in bardatas){ if(key.yd1){ //第一组数据 移动 yd.push(bardatas[key]); } if(key.kd1){ //第二组数据 款贷 kd.push(bardatas[key]); } if(key.gh1){ //第三组数据 古话 gh.push(bardatas[key]); } if(key.tygq1){ //第四组数据 天一搞青 tygq.push(bardatas[key]); } } datas.yd = yd; datas.kd = kd; datas.gh = gh; datas.tygq = tygq; StoreSalesLine('StoreSalesLine002', months, datas);//折线 console.log(JSON.stringify('移动:'+yd)); console.log(JSON.stringify('款贷:'+kd)); console.log(JSON.stringify('古话:'+gh)); console.log(JSON.stringify('天一搞青:'+tygq)); }
echarts:
function Line(id, months, datas){ x轴: data:months series:[ { 数据一:data:datas.yd },{ 数据二:data:datas.kd } ] }