您现在的位置是:网站首页> 编程资料编程资料

vue中Echarts使用动态数据的两种实现方式_vue.js_

2023-05-24 947人已围观

简介 vue中Echarts使用动态数据的两种实现方式_vue.js_

Echarts使用动态数据的两种方式

在使用Echarts时我们数据一般不是静态写死的,而是通过后端接口动态获取的,在此总结两种在vue框架下Echarts使用动态数据的方式。

1.通过computed

computed: {     options() {       let that = this;       let option = {           tooltip : {             trigger: 'axis',             formatter: function(item) {               return `支付金额 ${item[0].value}元`             }           },           legend: {             formatter: function(item){               return `${item}: ${that.priceData.todayPrice}`             }           },           grid: {               left: '3%',               right: '4%',               bottom: '3%',               containLabel: true           },           xAxis: {               type: 'category',               boundaryGap: false,               data: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]           },           yAxis: {               type: 'value',               splitLine: { //网格线                 show: false               }           },           series: [{               data: that.priceData.timePriceRange,               type: 'line',               smooth: true,               name: '支付金额',               itemStyle : {                                 normal : {                   color: '#13CE66',                                     lineStyle:{                                         color:'#13CE66'                                     }                                 }                             }           }]       }       return option;     }   }, //初始化 initEcharts(){       let  myChart = Echarts.init(this.$refs.chart);       myChart.setOption(this.options);     }

2.在data中定义option

通过在初始化之前,直接改变option对应属性值

//在data中定义option initEcharts(){       this.option.yAxis[1].max = this.maxRate;       this.option.xAxis.data = this.dateList;       this.option.series[0].data = this.payPriceTrendList;       let  myChart = Echarts.init(this.$refs.chart);       myChart.setOption(this.option);     }

数据变化后需要再次调init方法刷线图表

vue Echarts几种常用图表动态数据切换

几种常用图表 

1.柱状图 

效果图

柱状图

代码片段

HTML部分:

JS部分:

getEchartData() { this.$http .post("/sys/currency/twelvemonthstatistic", { traddingOrgId:id, }) .then(({ data }) => { //示例数据 data=[ { "ym": "2021-06", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-07", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-08", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-09", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-10", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-11", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2021-12", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2022-01", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2022-02", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2022-03", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2022-04", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 }, { "ym": "2022-05", "countOrder": 0, "sumGuaranteeFee": 0, "sumBzjAmount": 0, "sumGuaranteeFeeToWanYuan": 0, "sumBzjAmountToWanYuan": 0 } ] data.forEach((value, index) => { this.chartDate.push(value.ym);//X轴月份 this.chartData.push(value.countOrder);//Y轴数量 }); this.echart(); }); }, echart() { var myChart = echarts.init(this.$refs.echartMain1); let option = { xAxis: [ { type: "category", data: this.chartDate, axisPointer: { type: "shadow", }, }, ], yAxis: [ { type: "value", name: "近12个月的出函数量(笔)", min: 0, max: 500, interval: 100, axisLabel: { formatter: "{value}", }, }, ], series: [ { data: this.chartData, type: "bar", }, ], }; // echarts图表自适应 myChart.setOption(option); window.addEventListener("resize", () => { myChart.resize(); }); }, 

2.平滑折线面积图

效果图

面积折线图

代码片段

HTML部分:

近12月保函数据

JS部分:

/** * 获取面积折线图 filterType 1保函数量 2保费收入 3担保金额 不传返回所有 */ getDataMon12(filterType) { if (filterType) { this.isActive2 = filterType; // console.log('filterType高亮的是:',filterType,this.isActive2); } var monTop121 = []; var monTop122 = []; var monTop123 = []; var dataTop121 = []; var dataTop122 = []; var dataTop123 = []; this.$http .post("/sys/data/twelvemonthstatistic", { dataType: 1, filterType: filterType }) .then(res => { // console.log("面积折线图数据是:", res.data); res.data.forEach((value, index) => { if (filterType == 1) { monTop121.push(value.ym); dataTop121.push(value.countOrder); } else if (filterType == 2) { monTop122.push(value.ym); dataTop122.push(value.sumGuaranteeFeeToWanYuan); } else if (filterType == 3) { monTop123.push(value.ym); dataTop123.push(value.sumBzjAmountToWanYuan); } }); if (filterType == 1) { this.initChartBar1(monTop121, dataTop121); } else if (filterType == 2) { this.initChartBar1(monTop122, dataTop122); } else if (filterType == 3) { this.initChartBar1(monTop123, dataTop123); } }); }, /** * 面积折线图 */ initChartBar1(mon, data) { var option = { tooltip: { trigger: "axis" }, title: { subtext: "保函数量(件)", left: "5%", top: "0%", textAlign: "center" }, xAxis: { type: "category", boundaryGap: false, data: mon }, yAxis: { type: "value" }, series: [ { data: data, type: "line", smooth: true, areaStyle: {} } ] }; this.chartBar1 = echarts.init(this.$refs.echartDemo1); this.chartBar1.setOption(option); window.addEventListener("resize", () => { this.chartBar1.resize(); }); }, 

css部分:

// 选中时的样式 .active { color: #00a950; } 

3.折线图堆叠

效果图

层叠折线图

代码片段

HTML部分:

近12月保函数据

JS部分:

 /** * 获取层叠折线图 filterType 1保函数量 2保费收入 3担保金额 不传返回所有 */ getDataMonLine(filterType) { if(filterType){ this.isActive2=filterType // console.log('filterType高亮的是:',filterType,this.isActive2); } var monTop121 = []; var monTop122 = []; var monTop123 = []; var dataTop121 = []; var dataTop122 = []; var dataTop123 = []; this.$http .post("/sys/data/guaranteeorgtwelvemonthstatistic", { dataType: 1, filterType:filterType, }) .then((res) => { // console.log("折线图数据/投标保函按月统计表格数据是:", res.data); this.twelvemonthDataLine = res.data; // 示例数据 // this.twelvemonthDataLine=[ // { detailList: [ // {countOrder: 0, guaranteeOrgName: "08担保"}, // {countOrder: 0, guaranteeOrgName: "01担保"}, // {countOrder: 4, guaranteeOrgName: "中原银行股份有限公司"}, // {countOrder: 0, guaranteeOrgName: "07担保"}, // {countOrder: 0, guaranteeOrgName: "06担保"}, // {countOrder: 0, guaranteeOrgName: "06担保"}, // {countOrder: 0, guaranteeOrgName: "河南华诚工程担保有限公司"}, // {countOrder: 0, guaranteeOrgName: "长安责任保险股份有限公司"}, // {countOrder: 0, guaranteeOrgName: "阳光保险"}, // {countOrder: 0, guaranteeOrgName: "中联银"}, // {countOrder: 0, guaranteeOrgName: "汇友保险"}, // {countOrder: 0, guaranteeOrgName: "02担保"}, // ], // ym: "2021-06"}, // {detailList: [ // {countOrder: 20, guaranteeOrgName: "08担保"}, // {countOrder: 0, guaranteeOrgName: "01担保"}, // {countOrder: 9, guaranteeOrgName: "中原银行股份有限公司"}, // {countOrder: 0, guaranteeOrgName: "07担保"}, // {countOrder: 0, guaranteeOrgName: "06担保"}, // {countOrder: 0, guaranteeOrgName: "06担保"}, // {countOrder: 0, guaranteeOrgName: "河南华诚工程担保有限公司"}, // {countOrder: 1, guaranteeOrgName: "长安责任保险股份有限公司"}, // {countOrder: 0, guaranteeOrgName: "阳光保险"}, // {countOrder: 0, guaranteeOrgName: "中联银"}, // {countOrder: 0, guaranteeOrgName: "汇友保险"}, // {countOrder: 0, guaranteeOrgName: "0
                
                

-六神源码网