echart + vue堆叠柱状图和计算总数以及改变echart的下载图标

echart + vue堆叠柱状图和计算总数以及改变echart的下载图标

根据自己在项目中的爬坑,记录一些我认为的难点,不断的总结这些问题,来提高对echart的掌握程度

如下图所示

在这里插入图片描述

代码展示

// 引入ECharts
<ECharts :options="optionsData"></ECharts>
// echart 数据的渲染
this.optionsData = {
	color: ['#f2725e', '#5baeeb', '#f7b966', '#31c49a', '#448bcb', '#e76581', '#5ad5e7', '#818cf3'],
	tooltip: {
		show: true
	},
	legend: {
		bottom: 0,
		data:  ['常模', '整体', '已选']
	},
	grid: {
		top: 40,
		left: '3%',
		right: '4%',
    	bottom: '50',
    	containLabel: true
	},
	xAxis: {
		type: 'category',
			axisLabel: {
			interval: 0
		},
		boundaryGap: true,
		data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
	},
	yAxis: [
	{
		type: 'value',
		axisLabel: {
			formatter: '{value}'
		},
		max: 50
		}
	],
	toolbox: {
		show: true,
		feature: {
			// 自定义保存icon
			saveAsImage: {
				icon:'image://http://s.yishengya.cn/public/dist/pc/v2/img/down-ta.png',
	        	show: true,
	        	excludeComponents: ['toolbox'],
	        	pixelRatio: 2
			},
			iconStyle: {
				borderColor: '#ccc',
				borderWidth: 1
			}
		},
		showTitle: false,
	    itemSize:100,
	    top: '-40'
	},
	series: [
	{
		data: [12, 15, 11.23, 16.01, 10, 11, 16],
		label: {
			normal: {
				borderWidth: 2,
				formatter: '男',
				show: true
			}
		},
		name: '常模',
		stack: '常模',
		type: 'bar'
    },
    {
		data: [12, 15, 11.23, 16.01, 10, 15, 12],
		itemStyle: {
			opacity: '0.7'
		},
		label: {
			normal: {
				borderWidth: 2,
				formatter: '女',
				show: true
			}
		},
		name: '常模',
		stack: '常模',
		type: 'bar'
	},
    // 计算常模柱状图的总和显示在图表上方
	{
		data: [24, 30, 22.46, 32.02, 20, 26, 18],
		itemStyle: {
			normal: {
				color: 'rgba(128, 128, 128, 0)'
			}
		},
		label: {
			normal: {
				formatter: '{c}',
				// offset: [50, 80],
				position: 'insideBottom',
				show: true,
				textStyle: {
					color: '#4c4c4c'
				}
			}
		},
		name: '常模',
		stack: '常模',
		type: 'bar'
	},
	{
		data: [11, 15, 16, 8, 17, 6, 9],
		label: {
			normal: {
				borderWidth: 2,
				formatter: '男',
				show: true
			}
		},
		name: '整体',
		stack: '整体',
		type: 'bar'
	},
    {
      data: [20, 9, 11, 8, 9, 18, 4],
      itemStyle: {
        opacity: '0.7'
      },
      label: {
        normal: {
          borderWidth: 2,
          formatter: '女',
          show: true
        }
      },
      name: '整体',
      stack: '整体',
      type: 'bar'
    },
    // 计算整体柱状图的总和显示在图表上方
	{
		data: [31, 34, 27, 16, 26, 24, 13],
		itemStyle: {
			normal: {
				color: 'rgba(128, 128, 128, 0)'
			}
		},
		label: {
			normal: {
				formatter: '{c}',
				// offset: [50, 80],
				position: 'insideBottom',
				show: true,
				textStyle: {
					color: '#4c4c4c'
				}
			}
		},
		name: '整体',
		stack: '整体',
		type: 'bar'
	},
	{
		data: [12, 15, 14, 18, 9, 12, 19],
		label: {
			normal: {
				borderWidth: 2,
				formatter: '男',
				show: true
			}
		},
		name: '已选',
		stack: '已选',
		type: 'bar'
	},
	{
		data: [12, 11, 9, 9, 18, 11, 5],
		itemStyle: {
			opacity: '0.7'
		},
		label: {
			normal: {
				borderWidth: 2,
				formatter: '女',
				show: true
			}
		},
		name: '已选',
		stack: '已选',
		type: 'bar'
	},
	// 计算已选柱状图的总和显示在图表上方
	{
		data: [24, 26, 23, 27, 27, 23, 24],
		itemStyle: {
			normal: {
				color: 'rgba(128, 128, 128, 0)'
			}
		},
		label: {
			normal: {
				formatter: '{c}',
				// offset: [50, 80],
				position: 'insideBottom',
				show: true,
				textStyle: {
					color: '#4c4c4c'
				}
			}
		},
		name: '已选',
		stack: '已选',
		type: 'bar'
	}
  ]
}

第一次写,欢迎指正

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 中,要为堆叠柱状图增加比例标签,你可以使用 Chart.js 这个流行的图表库。下面是一个简单的示例代码,演示了如何使用 Chart.js 在 Vue 3 中创建一个堆叠柱状图,并添加比例标签: 1. 首先,确保你的项目中已经安装了 Chart.js 和 vue-chartjs。你可以使用以下命令进行安装: ```bash npm install chart.js vue-chartjs ``` 2. 在你的组件中导入所需的库和模块: ```javascript import { Bar, mixins } from 'vue-chartjs'; const { reactiveProp } = mixins; ``` 3. 创建一个名为 "StackedBarChart" 的组件,并扩展 `Bar` 类和 `reactiveProp`。 ```javascript export default { extends: Bar, mixins: [reactiveProp], props: { options: { type: Object, default: null, }, }, mounted() { this.renderChart(this.chartData, this.options); }, }; ``` 4. 在模板中使用 `<stacked-bar-chart>` 标签,并传递数据和选项作为属性: ```html <template> <div> <stacked-bar-chart :chart-data="chartData" :options="chartOptions"></stacked-bar-chart> </div> </template> ``` 5. 在组件的 `data` 中定义你的图表数据和选项: ```javascript data() { return { chartData: { labels: ['A', 'B', 'C', 'D'], datasets: [ { label: 'Data 1', data: [10, 20, 30, 40], backgroundColor: '#f87979', }, { label: 'Data 2', data: [30, 20, 10, 50], backgroundColor: '#7acbf9', }, ], }, chartOptions: { scales: { x: { stacked: true }, y: { stacked: true, ticks: { callback: function (value) { return value + '%'; }, }, }, }, }, }; }, ``` 在上面的代码中,我们定义了两个数据集(Data 1 和 Data 2),并使用了不同的颜色。在 `chartOptions` 中,我们将 `stacked` 属性设置为 `true` 来启用堆叠效果,并使用 `callback` 函数将 y 轴刻度值转换为百分比形式显示。 这样就可以在 Vue 3 中创建一个堆叠柱状图,并添加比例标签了。你可以根据自己的需求调整数据和选项。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值