Vue双Y轴折线柱状图

4 篇文章 0 订阅

通过设置Y轴坐标位置和定位索引来实现双Y轴的效果。

实现效果:

 代码:

<template>
	<div :class="className" :style="{height:height,width:width}" />
</template>

<script>
	import * as echarts from 'echarts'
	require('echarts/theme/macarons') // echarts theme
	import resize from './mixins/resize'

	export default {
		mixins: [resize],
		props: {
			className: {
				type: String,
				default: 'chart'
			},
			width: {
				type: String,
				default: '90%'
			},
			height: {
				type: String,
				default: '320px'
			},
			autoResize: {
				type: Boolean,
				default: true
			},
			chartData: {
				type: Object,
				required: true
			},
		},
		data() {
			return {
				chart: null
			}
		},
		watch: {
			chartData: {
				deep: true,
				handler(val) {
					this.setOptions(val)
				}
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.initChart()
			})
		},
		beforeDestroy() {
			if (!this.chart) {
				return
			}
			this.chart.dispose()
			this.chart = null
		},
		methods: {
			initChart() {
				this.chart = echarts.init(this.$el, 'macarons')
				this.setOptions(this.chartData)
			},
			setOptions({
				expectedData,
				actualData
			} = {}) {
				this.chart.setOption({
					title: {
						text: ''
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'cross',
							label: {
								backgroundColor: '#6a7985'
							}
						}
					},
					/*legend: {
						data: ['维修数', '维修合格数'],
						icon: 'roundRect',
						right: '0',
						top: '10',
						textStyle: { //图例文字的样式
							color: '#fff',
							fontSize: 12,
							//字体风格,'normal','italic','oblique'
							fontStyle: 'normal',
						},
					},*/
					grid: {
						left: '0%',
						right: '0%',
						bottom: '0%',
						containLabel: true,
					},
					xAxis: {
						type: 'category',
						data: ['3月', '4月', '5月', '6月', '7月', '8月', '9月'],
                        axisLine: {
							show: false,//隐藏刻度线
							lineStyle: {//字体颜色
								color: '#878787',
						},
					},
					},
					yAxis: [
						{ //左y轴
							type: 'value',
							name: '数量',
							// nameLocation: 'middle',
							splitLine: {
								show: false,
							}, //隐藏对称线
							axisLabel: {
								margin: 13,
								textStyle: {
									color: '#657584'
								}
							},
							splitNumber: 5
							// min: 0,
							// max: 100
						},
						{ //右y轴
							type: 'value',
							name: '比例',
							position: "right",//定位右y轴
							formatter: "{value}%",
							splitLine: {
								show: false,
							}, //隐藏对称线
							axisLabel: {
								margin: 10,
								textStyle: {
									color: '#657584'
								}
							},
							splitNumber: 5,
							// min: 0,
							// max: 4000,
							// interval: 800,
							nameTextStyle: {
								// padding: 4,
								padding: [4, 30, 4, 4] //对字体调整
							}
						}
					],
					series: [{
							data: [160, 230, 224, 218, 135, 147, 251],
							type: 'bar',
							barWidth: '40%',
							showBackground: false,
							label: {//显示在顶部的数值
								show: true,
								position: "top",
							},
							itemStyle: {
								borderRadius: [2, 2, 0, 0], //柱体圆角   
								color: new echarts.graphic.LinearGradient(
									0, 1, 0, 0, [{ //只要修改前四个参数就ok
											offset: 0,
											color: '#003f97'
										}, //柱图渐变色
										{
											offset: 1,
											color: '#00C6FB'
										}
									]
								),
							},
							backgroundStyle: {
								color: 'rgba(180, 180, 180, 0.2)'
							}
						}, {
							data: [70, 90, 90, 60, 90, 60, 70],
							type: 'line',
							smooth: false, //true是曲线 false是直线
							symbol: 'circle', //拐点样式
							symbolSize: 12, //拐点大小
							label: {//显示在顶部的数值
								show: true,
								position: "top",
								formatter: "{c}%"
							},
							itemStyle: {
								normal: {
									lineStyle: {
										width: 2, //折线宽度
										color: "#FFBF00" //折线颜色
									},
									color: '#FFBF00', //拐点颜色
									borderColor: '#FFBF00', //拐点边框颜色
									borderWidth: 2 //拐点边框大小
								},
								emphasis: {
									color: '#ff9705' //hover拐点颜色定义
								}
							},
							yAxisIndex: 1//定位右y轴
						}

					]
				})
			}
		}
	}
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue-Echarts多列柱状图是使用Vue框架和Echarts图表库来实现的一种图表类型。它可以显示多个列的柱状图,每个列代表不同的数据系列。在Vue-Echarts中,可以通过高度封装的方式来使用Echarts折线图柱状图功能。引用的混入文件可以帮助实现窗口改变时,使Echarts图表自适应。 对于多列柱状图,每一列的数据可以通过series中的配置来设置。在3D柱状图中,数据分为底部切片、中间柱子和顶部切片三个部分。可以通过配置不同的系列来表示不同的数据列。 具体实现多列柱状图的步骤如下: 1. 使用Vue-Echarts库来创建一个Vue组件,并引入相关依赖。 2. 在Vue组件中,使用Echarts的option配置对象来设置图表的样式、数据和系列。 3. 在option中的series属性中,通过配置不同的系列来表示多个数据列。 4. 按照需求配置其他的Echarts选项,如xy轴、图例等。 5. 在Vue组件中渲染并显示Echarts图表。 通过以上步骤,可以实现一个Vue-Echarts多列柱状图。在图表中,每一列代表一个数据系列,可以展示多个数据的对比情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue-echarts-element-stylus:高度封装的elemnet表格组件,高度封装echarts,excel表格上传,excel表格下载](https://download.csdn.net/download/weixin_42133452/18559339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue2.x-echarts公共组件封装--简易篇(3d柱状图,炫酷仪表盘,折线,曲线图)](https://blog.csdn.net/vscode_js/article/details/125913485)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半度纳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值