[echarts] 饼图折线图柱状图相互转换

柱状图数据转换

barData() {
	let that = this
	let xAxisData = that.list.map((item) => item.name)
	let seriesData = that.list.map((item) => item.value)
	this.options = {
	    title: {
	        ...dOptions.title,
	        text: this.title
	    },
	    tooltip: {
	        trigger: 'axis',
	        axisPointer: {
	            type: 'shadow'
	        }
	    },
	    toolbox: {
	        show: true,
	        feature: {
	            dataView: { readOnly: false },
	            myPie: {
	                show: true,
	                title: '切换为饼状图',
	                icon: 'path://M512 98.304C282.624 98.304 98.304 282.624 98.304 512s184.32 413.696 413.696 413.696c229.376 0 413.696-184.32 413.696-413.696S741.376 98.304 512 98.304zM888.832 491.52l-331.776 0 233.472-233.472C847.872 323.584 884.736 401.408 888.832 491.52zM512 888.832c-208.896 0-376.832-167.936-376.832-376.832 0-208.896 167.936-376.832 376.832-376.832 98.304 0 184.32 36.864 253.952 98.304l-266.24 266.24c-4.096 4.096-4.096 8.192-4.096 12.288 0 12.288 8.192 20.48 20.48 20.48l376.832 0C876.544 729.088 712.704 888.832 512 888.832z',
	                onclick: function () {
	                    that.pieData()
	                    that.initChart()
	                }
	            },
	            magicType: { show: true, type: ['bar', 'line'] },
	            restore: {},
	            saveAsImage: {}
	        }
	    },
	    xAxis: {
	        type: 'category',
	        data: xAxisData
	    },
	    yAxis: {
	        type: 'value'
	    },
	    series: [
	        {
	            data: seriesData,
	            type: 'bar',
	            showBackground: true,
	            backgroundStyle: {
	                color: 'rgba(180, 180, 180, 0.2)'
	            }
	        }
	    ]
	}
	},

折线图数据转换

 lineData() {
	 let that = this
	 let xAxisData = that.list.map((item) => item.name)
	 let seriesData = that.list.map((item) => item.value)
	 this.options = {
	     title: {
	         ...dOptions.title,
	         text: this.title
	     },
	     tooltip: {
	         trigger: 'axis',
	         axisPointer: {
	             type: 'shadow'
	         }
	     },
	     toolbox: {
	         show: true,
	         feature: {
	             dataView: { readOnly: false },
	             myPie: {
	                 show: true,
	                 title: '切换为饼状图',
	                 icon: 'path://M512 98.304C282.624 98.304 98.304 282.624 98.304 512s184.32 413.696 413.696 413.696c229.376 0 413.696-184.32 413.696-413.696S741.376 98.304 512 98.304zM888.832 491.52l-331.776 0 233.472-233.472C847.872 323.584 884.736 401.408 888.832 491.52zM512 888.832c-208.896 0-376.832-167.936-376.832-376.832 0-208.896 167.936-376.832 376.832-376.832 98.304 0 184.32 36.864 253.952 98.304l-266.24 266.24c-4.096 4.096-4.096 8.192-4.096 12.288 0 12.288 8.192 20.48 20.48 20.48l376.832 0C876.544 729.088 712.704 888.832 512 888.832z',
	                 onclick: function () {
	                     that.pieData()
	                     that.initChart()
	                 }
	             },
	             magicType: { show: true, type: ['bar', 'line'] },
	             restore: {},
	             saveAsImage: {}
	         }
	     },
	     xAxis: {
	         type: 'category',
	         data: xAxisData
	     },
	     yAxis: {
	         type: 'value'
	     },
	     series: [
	         {
	             data: seriesData,
	             type: 'line',
	             smooth: true
	         }
	     ]
	 }
	},

饼图数据转换

pieData() {
    let that = this
    this.options = {
        title: {
            ...dOptions.title,
            text: this.title
        },
        toolbox: {
            show: true,
            feature: {
                dataView: { readOnly: false },
                myPie: {
                    show: true,
                    title: '切换为饼状图',
                    icon: 'path://M512 98.304C282.624 98.304 98.304 282.624 98.304 512s184.32 413.696 413.696 413.696c229.376 0 413.696-184.32 413.696-413.696S741.376 98.304 512 98.304zM888.832 491.52l-331.776 0 233.472-233.472C847.872 323.584 884.736 401.408 888.832 491.52zM512 888.832c-208.896 0-376.832-167.936-376.832-376.832 0-208.896 167.936-376.832 376.832-376.832 98.304 0 184.32 36.864 253.952 98.304l-266.24 266.24c-4.096 4.096-4.096 8.192-4.096 12.288 0 12.288 8.192 20.48 20.48 20.48l376.832 0C876.544 729.088 712.704 888.832 512 888.832z',
                    onclick: function () {
                        that.pieData()
                        that.initChart()
                    }
                },
                myBar: {
                    show: true,
                    title: '切换为柱状图',
                    icon: 'path://M6.7,22.9h10V48h-10V22.9zM24.9,13h10v35h-10V13zM43.2,2h10v46h-10V2zM3.1,58h53.7',
                    onclick: function () {
                        that.barData()
                        that.initChart()
                    }
                },
                myLine: {
                    show: true,
                    title: '切换为折线图',
                    icon: 'path://M4.1,28.9h7.1l9.3-22l7.4,38l9.7-19.7l3,12.8h14.9M4.1,58h51.4',
                    onclick: function () {
                        that.lineData()
                        that.initChart()
                    }
                },
                restore: {},
                saveAsImage: {}
            }
        },
        color: ['#007950', '#0097AF', '#376BB9', '#DF993E', '#AD475F'],
        tooltip: {
            trigger: 'item',
            formatter: '{b} : {c} ({d}%)'
        },
        legend: {
            icon: 'circle',
            right: 6,
            top: '20%',
            width: '50%',
            itemWidth: 8,
            itemHeight: 8,
            textStyle: {
                padding: 4,
                rich: {
                    a: {
                        color: '#969AA8',
                        lineHeight: 30,
                        fontSize: 14,
                        width: 90
                    },
                    b: {
                        fontSize: 14,
                        fontWeight: 'bolder',
                        color: '#000'
                    }
                }
            },
            data: that.list,
            formatter: function (name) {
                var total = 0
                var target
                for (var i = 0, l = that.list.length; i < l; i++) {
                    total += that.list[i].value
                    if (that.list[i].name == name) {
                        target = that.list[i].value
                    }
                }
                let nameResult = name.length > 6 ? name.slice(0, 6) + '...' : name
                return '{b|' + ((target / total) * 100).toFixed(2) + '%}\n{a|' + nameResult + '}'
            }
        },
        series: [
            {
                type: 'pie',
                label: {
                    show: false
                },
                radius: [40, 75],
                center: ['25%', '55%'],
                data: that.list,
                animationEasing: 'cubicInOut',
                animationDuration: 2600
            }
        ]
    }
},

参考:

Echarts——饼图折线图柱状图相互转换

您可以使用 echarts 提供的图例组件和切换组件来实现饼图柱状图切换。具体操作如下: 1. 在 option 中设置 legend,包含饼图柱状图的名称。 2. 在 option 中设置 series,分别对应饼图柱状图的数据。 3. 在 option 中设置 toolbox,里面包含一个切换组件,用于切换饼图柱状图。 示例代码如下: ```js option = { ... legend: { data:['饼图', '柱状图'], selected: { '饼图': true, '柱状图': false } }, series: [{ name: '饼图', type: 'pie', data: [ {value: 335, name: 'A'}, {value: 310, name: 'B'}, {value: 234, name: 'C'}, {value: 135, name: 'D'}, {value: 1548, name: 'E'} ], label: { show: true, formatter: '{b}: {c} ({d}%)' } }, { name: '柱状图', type: 'bar', data: [320, 332, 301, 334, 390, 330, 320], label: { show: true, position: 'top' } }], toolbox: { feature: { mySwitch: { show: true, title: { pie: '切换饼图', bar: '切换柱状图' }, iconStyle: { emphasis: { borderColor: '#1E90FF' } }, icon: 'path://M303.557 497.504c-7.81 0-15.679-2.97-21.678-8.969l-157.084-157.084c-11.949-11.949-11.949-31.36 0-43.308s31.36-11.949 43.309 0l135.405 135.404v-358.395c0-16.277 13.199-29.477 29.477-29.477s29.477 13.199 29.477 29.477v358.394l135.404-135.404c11.948-11.949 31.36-11.949 43.308 0s11.949 31.36 0 43.309l-157.084 157.084c-6 6-13.87 8.969-21.679 8.969z', onclick: function (params, charts) { var option = charts.getOption(); if (option.legend[0].selected['饼图']) { option.legend[0].selected = { '饼图': false, '柱状图': true }; option.series[0].name = '柱状图'; option.toolbox.feature.mySwitch.title = { pie: '切换饼图', bar: '切换柱状图' }; } else { option.legend[0].selected = { '饼图': true, '柱状图': false }; option.series[0].name = '饼图'; option.toolbox.feature.mySwitch.title = { pie: '切换柱状图', bar: '切换饼图' }; } charts.setOption(option); } } } } ... }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值