echarts结合element ui实现点击按钮显示不同的图表

58 篇文章 0 订阅

echarts结合element ui实现点击按钮显示不同的图表

1.业务介绍


通过点击不同的按钮显示不同的图表
在这里插入图片描述

最后整体效果是这样的:
在这里插入图片描述

2.实现思路

(1)两个按钮进行切换

  <div class="callBtn">
        <el-row>
          <el-button @click="changeOutColor" :type="outColor"
            >呼出</el-button
          >
          <el-button @click="changeInColor" :type="inColor">呼入</el-button>
        </el-row>
      </div>

      

 data() {
    return {

      outColor: "danger",
      inColor: "",
    };
  },
methods:{
 changeOutColor() {
      if (this.outColor == "") {
        this.outColor = "danger";
        this.inColor = "";
      }
    },
    changeInColor() {
      if (this.inColor == "") {
        this.inColor = "danger";
        this.outColor = "";
      }
    },
}


//样式
  .el-button--danger:hover,
    .el-button--danger:focus {
      background: #ff6d6d;
      border-color: #ff6d6d;
      color: #ffffff;
    }

.el-button:hover,
.el-button:focus {
  color: black;
  border-color: #dcdfe6;
  background-color: #fff;
}

这样就可以两个按钮间相互切换了的,样式都可以根据自己的需求进行修改的

(2)将按钮和图表关联起来


这里我们选的是v-show指令实现

   <div class="callBtn">
        <el-row>
          <el-button @click="changeOutColor(1)" :type="outColor"
            >呼出</el-button
          >
          <el-button @click="changeInColor(2)" :type="inColor">呼入</el-button>
        </el-row>
      </div>


<div class="callChartAaea">
        <div
          id="callOutChart"
          v-show="showChart == 1"
          :style="{
            height: '400px',
            width: '400px',
            boxShadow:
              '0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)',
          }"
        ></div>
        <div
          id="callInChart"
          v-show="showChart == 2"
          :style="{
            height: '400px',
            width: '400px',
            boxShadow:
              '0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)',
          }"
        ></div>
      </div>


  mounted() {
    this.drawLine();
  },
  data() {
    return {
      outColor: "danger",
      inColor: "",
      showChart: 1,
    };
  },
  methods: {
    drawLine() {
      // 基于准备好的dom,初始化echarts实例
 
      let callOutChart = this.$echarts.init(
        document.getElementById("callOutChart")
      );
      let callInChart = this.$echarts.init(
        document.getElementById("callInChart")
      );
   
      // 绘制图表
      callOutChart.setOption({
        //标题
        title: {
          text: "测试案例",
          left: "left",
        },
        //背景色
        backgroundColor: "#fff",
        //提示框组件
        tooltip: {
          trigger: "item",
          formatter: "{b}:  ({d}%)",
          backgroundColor: "#fff",
          textStyle: {
            color: "black",
          },
          // extraCssText: "box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);",
        },
        //数据集
        dataset: {
          source: [
            // ["product","正常状态","无法接通","号码错误","拒绝接听","无人接听","家属接听","患方不合作","其他"]
            ["随访", 548],
            ["复诊", 35],
          ],
        },
        //图例组件
        legend: {
          bottom: 1,
          left: "center",
          itemWidth: 10,
          itemHeight: 10,
          itemStyle: {
            borderCap: "round",
          },
          icon: "circle",
        },
        //系列
        series: [
          {
            type: "pie",
            radius: ["40%", "60%"],
            label: {
              formatter: "{b}:  ({d}%)",
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            color: ["#3AA1FF", "#36CBCB"],
          },
        ],
      });

       callInChart.setOption({
        //标题
        title: {
          text: "测试案例",
          left: "left",
        },
        //背景色
        backgroundColor: "#fff",
        //提示框组件
        tooltip: {
          trigger: "item",
          formatter: "{b}:  ({d}%)",
          backgroundColor: "#fff",
          textStyle: {
            color: "black",
          },
          // extraCssText: "box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);",
        },
        //数据集
        dataset: {
          source: [
            ["业务分类A", 548],
            ["业务分类B", 37],
            ["业务分类C",44]
          ],
        },
        //图例组件
        legend: {
          bottom: 1,
          left: "center",
          itemWidth: 10,
          itemHeight: 10,
          itemStyle: {
            borderCap: "round",
          },
          icon: "circle",
        },
        //系列
        series: [
          {
            type: "pie",
            radius: ["40%", "60%"],
            label: {
              formatter: "{b}:  ({d}%)",
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            color: ["#3AA1FF", "#36CBCB","#4ECB73"],
          },
        ],
      });


    changeOutColor(val) {
      this.showChart = val;
      if (this.outColor == "") {
        this.outColor = "danger";
        this.inColor = "";
      }
    },
    changeInColor(val) {
      this.showChart = val;
      if (this.inColor == "") {
        this.inColor = "danger";
        this.outColor = "";
      }
    },
  },

我们在点击方法时传个参判断就可以将按钮和图表联系起来,搞完收工。仅提供参考思路,有见解的可以提出来

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现在同一张 ECharts 图上点击 el-radio-button 类型的按钮切换不同的后端数据,您可以使用 Element UI 的 radio-button 组件来创建按钮,并在按钮的 change 事件中发起 Ajax 请求来获取后端数据,然后更新图表数据。具体的步骤如下: 1. 创建一个包含所有需要切换的数据的数组。 2. 在 ECharts 的 option 中设置一个初始数据,用于图表的初始化。 3. 使用 Element UI 的 radio-button 组件来创建按钮,并绑定 change 事件。 4. 在 change 事件中,使用 Ajax 请求来获取对应的后端数据。 5. 在请求成功后,使用 ECharts 的 setOption() 方法来更新图表数据,将获取到的数据传入 setOption() 方法中即可。 6. 每次点击按钮时,都需要重新发起 Ajax 请求,并在请求成功后调用 setOption() 方法来更新图表数据。 以下是一个示例代码: ```vue <template> <div> <el-radio-group v-model="currentData"> <el-radio-button v-for="(data, index) in dataArr" :key="index" :label="data.name"> {{ data.name }} </el-radio-button> </el-radio-group> <div ref="chart" style="height: 400px;"></div> </div> </template> <script> import echarts from 'echarts'; import axios from 'axios'; export default { data() { return { dataArr: [ {name: 'data1', url: '/api/data1'}, {name: 'data2', url: '/api/data2'} ], currentData: 'data1', chart: null, option: { // 设置初始数据 series: [{ name: 'data1', data: [] }], // 其他配置 ... } }; }, mounted() { // 初始化图表 this.chart = echarts.init(this.$refs.chart); this.chart.setOption(this.option); // 第一次加载数据 this.loadData(); }, methods: { loadData() { // 获取当前选中的数据 var data = this.dataArr.find(d => d.name === this.currentData); // 发起 Ajax 请求 axios.get(data.url).then(res => { // 更新图表数据 this.chart.setOption({ series: [{ name: data.name, data: res.data }] }); }); } }, watch: { currentData(val) { // 监听数据选项的变化,重新加载数据 this.loadData(); } } } </script> ``` 在这个示例中,我们使用了 Vue CLI 创建了一个 Vue 组件,并使用了 Element UI 的 radio-button 组件来创建按钮。在组件的 data 中,我们定义了一个包含两个数据对象的数组 dataArr,其中每个对象都包含了数据名称和后端数据 URL。然后在 ECharts 的 option 中设置了一个初始数据(dataArr[0]),并使用了 mounted 钩子来初始化图表和加载初始数据。在组件的 methods 中,我们定义了一个 loadData 方法,用于根据当前选中的数据来加载对应的后端数据,并更新图表数据。在组件的 watch 中,我们监听了数据选项的变化,每次选项变化时都会重新加载对应的后端数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值