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

目录
  • Echarts使用动态数据的两种方式
    • 1.通过computed
    • 2.在data中定义option
  • vue Echarts几种常用图表动态数据切换
    • 1.柱状图
    • 2.平滑折线面积图
    • 3.折线图堆叠
    • 4.饼状图

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部分:

<div ref="echartMain1" id="echart-main"></div>

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部分:

<el-row :gutter="20" class="tittle">
          <el-col :span="16">
            <div class="text1">近12月保函数据</div>
          </el-col>
         <!-- <el-col :span="2">
            <div :class="{active:isActive2===1}" @click="getDataMon12(1)" class="cursor-pointer">保函数量</div>
          </el-col>
          <el-col :span="2">
            <div :class="{active:isActive2===2}" @click="getDataMon12(2)" class="cursor-pointer">保费收入</div>
          </el-col>
          <el-col :span="2">
            <div :class="{active:isActive2===3}" @click="getDataMon12(3)" class="cursor-pointer">担保金额</div>
          </el-col> -->
</el-row>
<div ref="echartDemo1" class="chart-box" style="height: 400px"></div>

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部分:

<el-row :gutter="20" class="tittle">
          <el-col :span="16">
            <div class="text1">近12月保函数据</div>
          </el-col>
         <!-- <el-col :span="2">
            <div :class="{active:isActive2===1}" @click="getDataMonLine(1)" class="cursor-pointer">保函数量</div>
          </el-col>
          <el-col :span="2">
            <div :class="{active:isActive2===2}" @click="getDataMonLine(2)" class="cursor-pointer">保费收入</div>
          </el-col>
          <el-col :span="2">
            <div :class="{active:isActive2===3}" @click="getDataMonLine(3)" class="cursor-pointer">担保金额</div>
          </el-col> -->
</el-row>
<div ref="echartDemo2" class="chart-box" style="height: 400px"></div>

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: "02担保"},
        //     ],
        //     ym: "2021-07"},
        //     {detailList: [
        //     {countOrder: 6, guaranteeOrgName: "08担保"},
        //     {countOrder: 0, guaranteeOrgName: "01担保"},
        //     {countOrder: 0, 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: 1, guaranteeOrgName: "汇友保险"},
        //     {countOrder: 0, guaranteeOrgName: "02担保"},
        //   ],
        //     ym: "2021-08"},
        //     {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: 1, guaranteeOrgName: "阳光保险"},
        //   {countOrder: 0, guaranteeOrgName: "中联银"},
        //   {countOrder: 0, guaranteeOrgName: "汇友保险"},
        //   {countOrder: 0, guaranteeOrgName: "02担保"},
        // ],
        //     ym: "2021-09"},
        //       {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-10"},
        //       {detailList: [
        //       {countOrder: 0, guaranteeOrgName: "08担保"},
        //       {countOrder: 0, guaranteeOrgName: "01担保"},
        //       {countOrder: 1, 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-11"},
        //       {detailList: [
        //       {countOrder: 0, guaranteeOrgName: "08担保"},
        //       {countOrder: 0, guaranteeOrgName: "01担保"},
        //       {countOrder: 0, 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-12"},
        //       {detailList: [
        //       {countOrder: 12, guaranteeOrgName: "08担保"},
        //       {countOrder: 4, guaranteeOrgName: "01担保"},
        //       {countOrder: 4, guaranteeOrgName: "中原银行股份有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "07担保"},
        //       {countOrder: 7, guaranteeOrgName: "06担保"},
        //       {countOrder: 3, guaranteeOrgName: "06担保"},
        //       {countOrder: 1, guaranteeOrgName: "河南华诚工程担保有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "长安责任保险股份有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "阳光保险"},
        //       {countOrder: 0, guaranteeOrgName: "中联银"},
        //       {countOrder: 0, guaranteeOrgName: "汇友保险"},
        //       {countOrder: 0, guaranteeOrgName: "02担保"},
        //     ],ym: "2022-01"},
        //       {detailList: [
        //       {countOrder: 0, guaranteeOrgName: "08担保"},
        //       {countOrder: 0, guaranteeOrgName: "01担保"},
        //       {countOrder: 0, 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: "2022-02"},
        //       {detailList: [
        //       {countOrder: 0, guaranteeOrgName: "08担保"},
        //       {countOrder: 0, guaranteeOrgName: "01担保"},
        //       {countOrder: 0, 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: "2022-03"},
        //       {detailList: [
        //       {countOrder: 0, guaranteeOrgName: "08担保"},
        //       {countOrder: 6, guaranteeOrgName: "01担保"},
        //       {countOrder: 0, guaranteeOrgName: "中原银行股份有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "07担保"},
        //       {countOrder: 0, guaranteeOrgName: "06担保"},
        //       {countOrder: 0, guaranteeOrgName: "06担保"},
        //       {countOrder: 0, guaranteeOrgName: "河南华诚工程担保有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "长安责任保险股份有限公司"},
        //       {countOrder: 0, guaranteeOrgName: "阳光保险"},
        //       {countOrder: 1, guaranteeOrgName: "中联银"},
        //       {countOrder: 0, guaranteeOrgName: "汇友保险"},
        //       {countOrder: 0, guaranteeOrgName: "02担保"},
        //     ],ym: "2022-04"},
        //       {detailList: [
        //       {countOrder: 3, guaranteeOrgName: "08担保"},
        //       {countOrder: 13, guaranteeOrgName: "01担保"},
        //       {countOrder: 0, 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: "2022-05"}
        //             ]
          this.twelvemonthDataLine.forEach((value, index) => {
            if (filterType == 1) {
              monTop121.push(value.ym);
              dataTop121.push(value.detailList);

            }
            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.initChartLine(monTop121, dataTop121)
          } else if (filterType == 2) {
            this.initChartLine(monTop122, dataTop122);
          } else if (filterType == 3) {
            this.initChartLine(monTop123,dataTop123);
          }
        });
    },
     /**
     * 层叠折线图
     */
    initChartLine(mon, data) {
      // console.log('monTop121:',mon);
      let series = [];
      let leggend = [];
      for (let i = 0; i < data.length; i++) {
        let countOrders = [];
        leggend.push(data[i].guaranteeOrgName);
        // countOrders=[0,1,5,2,4,5,3,1,4,9,14,2]
        var arr = data[i];
        arr.forEach((item, index) => {
          countOrders.push(item.countOrder);
        });
        // 将数据组装到series数组,供option配置使用
        series.push({
          // 每个担保机构,对应一条折线图
          name: data[i].guaranteeOrgName,
          type: "line",
          data: countOrders // 每个担保机构,对应的横坐标的值组成的数组
        });
      }
      console.log(leggend);
      console.log("series是:", series);
      var option = {
        tooltip: {
          trigger: "axis"
        },
        title: {
          subtext: "保函数量(件)",
          left: "5%",
          top: "0%",
          textAlign: "center"
        },
        legend: {
          padding: 10,
          tooltip: {
            show: true
          },
          data: leggend
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: mon
        },
        yAxis: {
          type: "value"
        },
        series: series
      };
      this.chartLine = echarts.init(this.$refs.echartDemo2);
      this.chartLine.setOption(option);
      window.addEventListener("resize", () => {
        this.chartLine.resize();
      });
    },

css部分:

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

4.饼状图

效果图

代码片段

HTML部分:

 <div class="piechart">
        <div class="tittle">
          <div class="text1">交易平台统计</div>
          <el-date-picker v-model="![请添加图片描述](https://img-blog.csdnimg.cn/2c725dfca29f441aa477ce33bad2d9ed.png)
 type="datetimerange" :picker-options="pickerOptions2" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:01', '23:59:59']" style="width: 390px" @change="timeHandle(4)">
          </el-date-picker>
        </div>
        <div ref="echartDemo3" class="chart-box" style="height: 400px"></div>
 </div>

JS部分:

	// 时间选择修改时数据刷新 1为投标保函按月合计 2为出函平台统计的时间选择器 3为出函机构统计的时间选择器 4为交易平台统计的时间选择器
    timeHandle(num) {
      var that = this;
      if (num == 1) {
        this.getMonthData(12, this.value1);
        //  this.value1=""
      } else if (num == 2) {
        this.getIssuinGuarantee("", this.value2);
        // this.value2=""
      } else if (num == 3) {
        this.getCensusData("", this.value3);
        // this.value3=""
      } else {
        this.getTradeData("", this.value4);
        // this.value4=""
      }
    },
    getIssuinGuarantee(days, time) {
      if (days) {
        this.isActive3 = days;
        // console.log('days高亮的是:',days,this.isActive3);
      }
      this.$http({
        url: "/sys/data/guaranteeorgplatformstatistic",
        method: "post",
        data: {
          dataType: 1,
          days: days,
          startTime: !time[0] ? "" : time[0],
          endTime: !time[1] ? "" : time[1]
        }
      }).then(res => {
        // console.log("出函平台数据:", res.data);
        this.issuinGuaranteeData = res.data;
        //后台响应数据是
         this.issuinGuaranteeData=[
         {
	        countOrder: 3
			countOrderRatio: 21.42
			sumBzjAmount: 3000
			sumBzjAmountRatio: 21.42
			sumBzjAmountToWanYuan: 0.3
			sumGuaranteeFee: 600
			sumGuaranteeFeeRatio: 10.9
			sumGuaranteeFeeToWanYuan: 0.06
			traddingOrgId: "303c3dfaa3c54864a1b67f5fee694231"
			traddingOrgName: "驻马店公共资源交易中心"
         },
         {
        	countOrder: 4
			countOrderRatio: 28.57
			sumBzjAmount: 4000
			sumBzjAmountRatio: 28.57
			sumBzjAmountToWanYuan: 0.4
			sumGuaranteeFee: 1400
			sumGuaranteeFeeRatio: 25.45
			sumGuaranteeFeeToWanYuan: 0.14
			traddingOrgId: "57f2e9ceb8834fe7ac37e06f9dab7aef"
			traddingOrgName: "通许县公共资源交易中心"
         },
         {
         	countOrder: 2
			countOrderRatio: 14.28
			sumBzjAmount: 2000
			sumBzjAmountRatio: 14.28
			sumBzjAmountToWanYuan: 0.2
			sumGuaranteeFee: 1000
			sumGuaranteeFeeRatio: 18.18
			sumGuaranteeFeeToWanYuan: 0.1
			traddingOrgId: "8af00e2ff8ea458da5b122495ee83303"
			traddingOrgName: "杞县公共资源交易中心"
         },
         {
         	countOrder: 5
			countOrderRatio: 35.73
			sumBzjAmount: 5000
			sumBzjAmountRatio: 35.73
			sumBzjAmountToWanYuan: 0.5
			sumGuaranteeFee: 2500
			sumGuaranteeFeeRatio: 45.47
			sumGuaranteeFeeToWanYuan: 0.25
			traddingOrgId: "c83acb963a4140438984b196abcec46c"
			traddingOrgName: "巩义公共资源交易中心"
         }

         ]
        this.initChartBar3(this.issuinGuaranteeData);
      });
    },
/**
     * 3饼图
     */
    initChartBar3(data) {
      var temp1 = [];
      var temp2 = [];
      var temp3 = [];
      data.forEach((value, index) => {
        temp1.push({
          name: value.guaranteeOrgName,
          value: value.countOrderRatio
        });
        temp2.push({
          name: value.guaranteeOrgName,
          value: value.sumGuaranteeFeeRatio
        });
        temp3.push({
          name: value.guaranteeOrgName,
          value: value.sumBzjAmountRatio
        });
      });
      // console.log(temp1)
      // console.log(temp2)
      // console.log(temp3)
      var option = {
        tooltip: {
          trigger: "item",
          formatter: "{b} : {c} ({d}%)"
        },
        title: [
          {
            subtext: "保函数量占比",
            left: "16.67%",
            top: "5%",
            textAlign: "center"
          },
          {
            subtext: "保函金额占比",
            left: "50%",
            top: "5%",
            textAlign: "center"
          },
          {
            subtext: "担保金额占比",
            left: "83.33%",
            top: "5%",
            textAlign: "center"
          }
        ],
        series: [
          {
            type: "pie",
            radius: "55%",
            center: ["50%", "50%"],
            data: temp1,
            right: "66.6667%"
          },
          {
            type: "pie",
            radius: "55%",
            center: ["50%", "50%"],
            data: temp2,
            left: "33.3333%",
            right: "33.3333%"
          },
          {
            type: "pie",
            radius: "55%",
            center: ["50%", "50%"],
            data: temp3,
            left: "66.6667%"
          }
        ]
      };
      this.chartBar3 = echarts.init(this.$refs.echartDemo3);
      this.chartBar3.setOption(option);
      window.addEventListener("resize", () => {
        this.chartBar3.resize();
      });
    },

css部分:

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

---

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • vue项目中vue-echarts讲解及常用图表实现方案(推荐)

    目录 1.图表主题颜色的修改 2.XY轴体颜色和文本颜色修改 3.横向柱状图的显示 4.过滤xy轴文本内容 5.Y轴文本显示在轴线内 6.在柱状条头部添加标签内容 7.自定义图例修改 8.环比图中间显示内容 图表类的项目操作一般常见于管理平台系统,移动端项目中并不是特别常见,不常见不代表没有,在一些商城类应用中,商家需要看到商品的销量分析,盈利分析等,就需要用到图标,比较常用的图标库,像百度的ECHARTS,蚂蚁金服的AntV都是不错的图标库,感兴趣的读者可以直接到他们的官网阅读相关的资料,笔者

  • Vue引入echarts方法与使用介绍

    目录 1. 引入echarts 2. 将echarts设置为全局 3. 在相关组件中使用 在Vue中引入echarts以及使用 今天来告诉大家如何将echart引入vue并巧妙利用钩子函数对表格数据进行渲染. 1. 引入echarts npm install echarts --save 在vue项目中安装 2. 将echarts设置为全局 main.js(项目入口文件) //引入echarts文件 import * as echarts from 'echarts' Vue.prototype

  • Vue 项目中Echarts 5使用方法详解

    目录 前言 创建项目 基本使用 安装 使用方法 柱状图 动态排序柱状图 前言 Echarts 是一个纯JavaScript的图表库,兼容绝大部分的浏览器,底层依赖轻量级的canvas类库ZRender,提供直观,生动,可交互,可高度个性化定制的数据可视化图表. 创建项目 先使用vue-cli创建一个新的项目,配置按照自己的需要选择,默认的也可 vue create vue_echarts cd vue_echarts npm run serve 基本使用 安装 首先安装echarts npm i

  • Vue中使用 Echarts5.0 遇到的一些问题(vue-cli 下开发)

    目录 Vue使用Echarts5.0的一些问题 问题 解决方案一 解决方案二 为什么会出现这种情况? vue使用echarts 5.0“export ‘default‘ (imported as ‘echarts‘) was not found in ‘echarts‘ Vue使用Echarts5.0的一些问题 问题 最新版的 Echarts5.0 使用 import echarts from 'echarts' 导入,会发现导出的 echarts 是 undefined 的情况,无法正常使用.

  • Vue中使用Echarts可视化图表宽度自适应的完美解决方案

    目录 一.问题阐述: 二.解决思路: 三.最终效果: 一.问题阐述: 开发H5项目中应用到Echarts数据可视化,由于H5主要使用在手机,屏幕宽度大大限制了图表的展现,问题如下. 上图中x轴的数据是动态的,从2022年切换到2021年数据变化,此时如果x轴的数据过多就会显得格外拥挤. 二.解决思路: 我的解决方案是,在放置Echarts的容器(div)外层再套一层容器(div),外层容器宽度固定设置手机屏幕宽,并设置溢出显示滚动条(width: 100%;overflow: auto),内层E

  • vue+echarts5实现中国地图的示例代码

    使用echarts5.0版本实现中国地图,E charts在5.0版本之后,地图不能直接引入了,需要自己去下载. 地图文件下载地址:下载地址(http://datav.aliyun.com/portal/school/atlas/area_selector#&lat=30.772340792178525&lng=103.94573258937584&zoom=9.5) 注意: 将下载后的json文件放置/public目录下 map类型的地图 <template> <

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

    目录 Echarts使用动态数据的两种方式 1.通过computed 2.在data中定义option vue Echarts几种常用图表动态数据切换 1.柱状图 2.平滑折线面积图 3.折线图堆叠 4.饼状图 Echarts使用动态数据的两种方式 在使用Echarts时我们数据一般不是静态写死的,而是通过后端接口动态获取的,在此总结两种在vue框架下Echarts使用动态数据的方式. 1.通过computed computed: {     options() {       let that

  • Java 中 Form表单数据的两种提交方式

    1 GET - 从指定的服务器中获取数据 1.1 GET方法 使用GET方法时,查询字符串(键值对)被附加在URL地址后面一起发送到服务器,例如:http://localhost:8080//customer/customer_info?res=json&mt=0&custId=1 1.2 特点 (1) GET请求能够被缓存 (2) GET请求会保存在浏览器的浏览记录中 (3) 以GET请求的URL能够保存为浏览器书签 (4) GET请求有长度限制(不能多于1024字节) (5) GET请

  • Java动态代理的两种实现方式详解【附相关jar文件下载】

    本文实例讲述了Java动态代理的两种实现方式.分享给大家供大家参考,具体如下: 一说到动态代理,我们第一个想到肯定是大名鼎鼎的Spring AOP了.在AOP的源码中用到了两种动态代理来实现拦截切入功能:jdk动态代理和cglib动态代理.两种方法同时存在,各有优劣.jdk动态代理是由java内部的反射机制来实现的,cglib动态代理是通过继承来实现的,底层则是借助asm(Java 字节码操控框架)来实现的(采用字节码的方式,给A类创建一个子类B,子类B使用方法拦截的技术拦截所以父类的方法调用)

  • redis数据的两种持久化方式对比

    一.概念介绍 redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Apend Only File). RDB方式 RDB方式是一种快照式的持久化方法,将某一时刻的数据持久化到磁盘中. •redis在进行数据持久化的过程中,会先将数据写入到一个临时文件中,待持久化过程都结束了,才会用这个临时文件替换上次持久化好的文件.正是这种特性,让我们可以随时来进行备份,因为快照文件总是完整可用的. •对于RDB方式,redis会单独创建(fork)一个子进程来进行持久化,而

  • php中模拟POST传递数据的两种方法分享

    方法1 复制代码 代码如下: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://192.168.1.135/turntable/get_jump.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $array); curl_exec($ch); curl_close($ch); 方法2 复制代码 代码如下: $d

  • 深入理解java动态代理的两种实现方式(JDK/Cglib)

    什么是代理模式? 代理模式:在调用处不直接调用目标类进行操作,而是调用代理类,然后通过代理类来调用目标类进行操作.在代理类调用目标类的前后可以添加一些预处理和后处理操作来完成一些不属于目标类的功能. 为什么要使用代理模式? 通过代理模式可以实现对目标类调用的控制.在目标类调用前/后进行一些不属于目标类的操作,如:数据验证.预处理.后处理.异常处理等 什么是静态代理什么是动态代理? 静态代理:代理类只能实现对"特定接口的实现类"进行代理 动态代理:代理类可以实现对多种类的代理 jdk代理

  • vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

    目录 准备 封装 unicode引用封装 font-class引用封装 symbol引用封装 引入 全局引入 局部引入 使用 在线使用 有时候会因网络问题影响用户体验:直接放在 本地使用 ,如果过多使用也会显得繁琐,所以就可以将其封装成一个组件,也方便维护.​ 封装基于阿里巴巴图标库的项目图标. 准备 将项目内的图标下载至本地 在了路径 src/assets 下新建文件夹 iconfont ,用来存放字体图标的本地文件 解压下载到本地的字体图标文件,放到 iconfont 文件夹下 如过项目中没

  • 深入Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来接收来自系统和应用中的广播.在Android系统中,广播体现在方方面面,例如当开机完成后系统会产生一条广播,接收到这条广播就能实现开机启动服务的功能:当网络状态改变时系统会产生一条广播,接收到这条广播就能及时地做出提示和保存数据等操作:当电池电量改变时,系统会产生一条广播,接收到这条广播就能在电量低时告知用户

  • vue中echarts的用法及与elementui-select的协同绑定操作

    1.vue中echarts的使用 引入echarts后 let myChart = echarts.init(document.getElementById('dsm'));//dsm为绑定的dom结构 var option = { //backgroundColor:"#111c4e", tooltip: { trigger: 'axis' }, legend: { //图表上方的图例显隐 data:['光合有效辐射'], textStyle: { color: '#fff' } }

  • vue中echarts图表大小适应窗口大小且不需要刷新案例

    我就废话不多说了,大家还是直接看代码吧~ 内容如下: // 基于准备好的dom,初始化echarts实例 let myChart = this.$echarts.init(document.getElementById('myChart')) //根据窗口的大小变动图表 --- 重点 window.onresize = function(){ myChart.resize(); //myChart1.resize(); //若有多个图表变动,可多写 } 代码如下: mounted() { con

随机推荐