Echarts 自定义工具栏事件 || 柱状图和饼状图之间的切换

1 篇文章 0 订阅
  1. echarts中有柱状图和折线图之间的切换,却没有柱状图和饼图直接的切换
  2. 不过echarts有自定义工具栏方法,我们可以通过自定工具栏的方法来实现echarts没有的图形切换
  3. toolbox 中的 feature 自定方法 (title: 鼠标放上去显示的文字;icon: 自定工具栏的图标的path路径;onclick: 点击事件)
    在这里插入图片描述
  4. 在点击事件写自己要处理成的图形代码就行了, 下面有我的一个完整实例
  5. 如何引入echarts,在我的上一篇文章中,

完整代码:

<template>
  <div class="app-container">
    <div class="main-content">
      <el-row :gutter="5">
        <el-col :span="17">
          <div ref="tjcishujiange" style="width: 98%;height: calc(100vh - 231px)"></div>
        </el-col>
        <el-col :span="6">
          <div class="tables-content">
            <div style="background: #FFFFFF;height: 44px;text-align: center;line-height: 44px;font-size: 18px">
              测试
            </div>
            <el-table
              :data="data_jhyf_list"
              height="calc(100vh - 275px)"
              max-height="calc(100vh - 275px)"
              style="width: 100%">
              <el-table-column prop="month" label="区间" show-overflow-tooltip/>
              <el-table-column prop="number1" label="数量" show-overflow-tooltip/>
            </el-table>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>

export default {
  name: "TjCishujiange",
  data() {
    return {
      data_jhyf_list: [],
      options: {},
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 请求数据
    getList() {
      this.data_jhyf_list = [
        {month: '0-1', number1: '1500'},
        {month: '2-3', number1: '1200'},
        {month: '4-5', number1: '1100'},
        {month: '6-7', number1: '1000'},
        {month: '8-9', number1: '900'},
        {month: '10-11', number1: '800'},
        {month: '12-13', number1: '700'},
      ]
      this.$nextTick(function () {
        this.dataHandle()
        this.randerEcharts()
      })
    },
    // 查询
    handleQuery() {
      this.getList()
    },
    //  饼状图数据处理
    dataHandle() {
      let echartsData = this.data_jhyf_list.map(item => {
        return {
          value: item.number1,
          name: item.month
        }
      })
      let that = this
      that.options = {
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            let value = params.data.value
            return `${params.seriesName} <br/>${params.name}: ${value} (${params.percent}%)`
          },
        },
        toolbox: {
          show: true,
          feature: {
            dataView: {show: true, readOnly: false},
            myTool: {
              show: true,
              title: '切换为柱状图',
              icon: 'path://M163.84 880.64h122.88v-737.28h-122.88v737.28z m-40.96-778.24h204.8v819.2h-204.8v-819.2z m286.72 204.8h204.8v614.4h-204.8v-614.4z m40.96 573.44h122.88v-532.48h-122.88v532.48z m245.76-368.64h204.8v409.6h-204.8v-409.6z m40.96 368.64h122.88v-327.68h-122.88v327.68z',
              onclick: function () {
                that.dataZhuzhuan()
                that.randerEcharts()
              }
            },

            myTool1: {
              show: true,
              title: '切换为饼状图',
              icon: 'path://M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m460.1856 489.8816H537.6V52.0192a461.2096 461.2096 0 0 1 434.5856 437.8624z m0 51.2a458.752 458.752 0 0 1-93.3888 249.4464L582.0416 541.0816zM512 972.8a460.8 460.8 0 0 1-25.6-921.6v464.2816a25.8048 25.8048 0 0 0 9.8304 20.48L845.6192 829.44a459.776 459.776 0 0 1-333.6192 143.36z',
              onclick: function () {
                that.dataHandle()
                that.randerEcharts()
              }
            },
            restore: {show: true},
            saveAsImage: {show: true}
          }
        },
        legend: {
          orient: 'vertical',
          left: 'left',
          type: 'scroll',
        },
        series: [
          {
            name: '数量',
            type: 'pie',
            radius: '50%',
            data: echartsData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    },
    // 柱状图数据处理
    dataZhuzhuan() {
      let wb_data = this.data_jhyf_list
      let xList = wb_data.map(item => item.month)
      let data = wb_data.map(item => item.number1)
      let that = this
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },

        legend: {
          orient: 'vertical',
          left: 'left',
          type: 'scroll',
        },
        toolbox: {
          show: true,
          feature: {
            dataView: {show: true, readOnly: false},
            myTool1: {
              show: true,
              title: '切换为饼状图',
              icon: 'path://M163.84 880.64h122.88v-737.28h-122.88v737.28z m-40.96-778.24h204.8v819.2h-204.8v-819.2z m286.72 204.8h204.8v614.4h-204.8v-614.4z m40.96 573.44h122.88v-532.48h-122.88v532.48z m245.76-368.64h204.8v409.6h-204.8v-409.6z m40.96 368.64h122.88v-327.68h-122.88v327.68z',
              onclick: function () {
                that.dataZhuzhuan()
                that.randerEcharts()
              }
            },
            myTool: {
              show: true,
              title: '切换为饼状图',
              icon: 'path://M512 0a512 512 0 1 0 512 512A512 512 0 0 0 512 0z m460.1856 489.8816H537.6V52.0192a461.2096 461.2096 0 0 1 434.5856 437.8624z m0 51.2a458.752 458.752 0 0 1-93.3888 249.4464L582.0416 541.0816zM512 972.8a460.8 460.8 0 0 1-25.6-921.6v464.2816a25.8048 25.8048 0 0 0 9.8304 20.48L845.6192 829.44a459.776 459.776 0 0 1-333.6192 143.36z',
              onclick: function () {
                that.dataHandle()
                that.randerEcharts()
              }
            },
            restore: {show: true},
            saveAsImage: {show: true}
          }
        },
        xAxis: {
          type: 'category',
          data: xList
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true,
          },
        },
        series: [
          {
            data: data,
            type: 'bar',
            name: '数量',
            barMaxWidth: 22
          }
        ]
      }
      this.options = option
    },
    //渲染图表
    randerEcharts() {
      let tjcishujiange = this.echarts.init(this.$refs.tjcishujiange)
      tjcishujiange.resize();
      tjcishujiange.clear()
      tjcishujiange.setOption(this.options, true)
    },
  },
}
</script>
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ECharts是一个基于JavaScript的开源可视化库,用于构建交互式的图表和数据可视化界面。它提供了丰富的图表类型和配置选项,可以满足各种数据可视化需求。 要实现ECharts自定义柱状图渐变色,可以通过设置渐变色的方式来实现。具体步骤如下: 1. 首先,在ECharts的配置项中找到柱状图的系列(series)配置项。 2. 在系列配置项中,找到柱状图的样式配置项(itemStyle)。 3. 在样式配置项中,找到渐变色配置项(color)。 4. 将渐变色配置项设置为一个数组,数组中每个元素表示一个渐变色。 5. 每个渐变色可以使用对象来表示,对象包含起始颜色(colorStops)和结束颜色(colorStops)。 6. 在起始颜色和结束颜色中,可以设置颜色值和位置(offset),用于控制渐变的起始和结束位置。 以下是一个示例代码,演示了如何实现ECharts自定义柱状图渐变色: ```javascript option = { // 其他配置项... series: [{ type: 'bar', data: [10, 20, 30, 40, 50], itemStyle: { color: [ { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'red' // 起始颜色 }, { offset: 1, color: 'blue' // 结束颜色 } ] } ] } }] }; ``` 关于ECharts的legend渐变色,目前ECharts官方并没有直接提供设置legend渐变色的配置项。但是你可以通过自定义legend的方式来实现渐变色效果。 以下是一个示例代码,演示了如何通过自定义legend来实现渐变色效果: ```javascript option = { // 其他配置项... legend: { data: ['柱状图'], formatter: function (name) { return '{a|' + name + '}'; }, textStyle: { rich: { a: { color: { type: 'linear', x: 0, y: 0, x2: 1, y2: 0, colorStops: [ { offset: 0, color: 'red' // 起始颜色 }, { offset: 1, color: 'blue' // 结束颜色 } ] } } } } }, series: [{ type: 'bar', name: '柱状图', data: [10, 20, 30, 40, 50] }] }; ``` 通过上述代码,你可以将legend的文本设置为渐变色,从而实现legend的渐变色效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值