vue3中,echarts在柱形图上加基准线,自定义tooltip

vue3中,echarts在柱形图上加基准线,自定义tooltip

效果图

在这里插入图片描述

代码

index.vue

<template>
	<div class="content">
        <!-- 
		<titleHeader :src="img" :title="title" @is-dialog="handleEvery" class="header-style" />
		<div ref="echartsRef" class="attendance-data"></div>
		<!-- 二级页面 -->
		<everyday-material :is-show="showEveryday" @is-close="handleCloseEveryday"></everyday-material>
	</div>
</template>
<script setup lang="ts" name="absenteeism">
import * as echarts from "echarts";
import { useEcharts } from "@/hooks/useEcharts";
import img from "@/assets/svg/more.svg";
import titleHeader from "@/components//header/titleHeader.vue";
import everydayMaterial from "./modules/everydayMaterial.vue";
import { ref, onMounted, nextTick } from "vue";
const title = ref("模块题目名称");
const echartsRef = ref<HTMLElement>();

// 二级页面
const showEveryday = ref(false);
const handleCloseEveryday = () => {
	showEveryday.value = false;
};
const handleEvery = () => {
	showEveryday.value = true;
};

const chartsView = () => {
	let myChart: echarts.ECharts = echarts.init(echartsRef.value as HTMLElement);
	let option: echarts.EChartsCoreOption = {
		title: {
			text: "达成率",
			left: 20,
			top: 10,
			textStyle: {
				color: "#ccc",
				fontSize: 16
			}
		},
		tooltip: {
			trigger: "axis",
			backgroundColor: "rgba(0, 0, 0, 1)",
			borderColor: "rgba(0, 0, 0, 1)",
			padding: [16, 24],
			textStyle: {
				color: "#fff"
			},
            // 改造弹框
			formatter: (params: any) => {
				// console.log(params); // 打印数据
				let ele: string = ``;
				ele += `<div style="font-size:16px;margin-bottom:5px;">达成率</div>`;
				for (let i = 0; i < params.length; i++) {
					ele += `<div style="display:flex;align-items: center;height:30px;font-size:12px;"><div style="width: 8px;height: 8px;background: ${params[i].color};border-radius: 50%;margin-right: 10px;"></div>${params[i].name}<div style="font-size:20px;font-weight:bold;color:${params[i].color};margin:0 0px 0 10px;">${params[i].data}</div> %</div>`;
				}
				return ele;
			}
		},
		xAxis: {
			type: "category",
			data: ["大型材料", "支护用品", "五小电器", "坑木类", "其他"]
		},
		yAxis: {
			size: "16px",
			type: "value",
			axisLabel: {
				formatter(value: string | number | any) {
					if (value > 0) {
						return value + "%";
					}
					return Math.abs(value) + "%";
				}
			}
		},
		series: [
			{
				type: "bar",
				data: [80, 65, 80, 90, 110],
				// data: [80, 65, 80, 90, 110].map(item => {
				// 	if (item > 80) {
				// 		return {
				// 			value: item,
				// 			itemStyle: {
				// 				color: "#F33"
				// 			}
				// 		};
				// 	}
				// 	return item;
				// }),
				itemStyle: {
					normal: {
						color: "#0075FF",
						barBorderRadius: [10, 10, 0, 0]
					}
				},
				showBackground: true,
				backgroundStyle: {
					color: "rgba(180, 180, 180, 0.2)"
				},
				markLine: {
					symbol: ["none", "none"], //去掉箭头
					lineStyle: {
						normal: {
							type: "dashed",
							color: "#00FFBB" //基准线颜色
						}
					},
					data: [
						{
							name: "基准线名称",
							yAxis: 45 // 基准线数据
						}
					],
					label: {
						show: true,
						// position: "insideStartBottom",
						color: "#00FFBB" // 基准线文字颜色
					}
				}
			}
		]
	};
	useEcharts(myChart, option);
};
onMounted(() => {
	nextTick(() => {
		chartsView();
	});
});
</script>
<style scoped lang="scss">
.content {
	margin-bottom: 8px;
	background: rgb(0 0 0 / 30%);
	border-radius: 10px;
	span {
		font-size: 16px;
		color: #ffffff;
	}

	// .header-style {
	// 	padding-top: 16px;
	// 	padding-bottom: 16px;
	// }
}
.attendance-data {
	height: calc(100% - 54px);
	overflow-y: auto;
}
</style>

src/components//header/titleHeader.vue

<template>
	<div class="header-container">
		<div class="title-img">
			<!-- title文字 -->
			<span>{{ props.title }}</span>
			<!-- title单选 -->
			<slot name="right"></slot>
			<!-- title图片 -->
			<img @click="isDialog" class="header" :src="props.src" v-show="props.src != ''" />
		</div>
		<div class="mark">
			<slot name="mark" />
		</div>
	</div>
</template>

<script lang="ts" setup>
const props = defineProps({
	src: {
		type: String,
		required: false,
		default: ""
	},
	title: {
		type: String,
		required: true,
		default: ""
	}
});

// 展示弹框
const emit = defineEmits(["is-dialog"]);
const isDialog = () => {
	emit("is-dialog", "");
};
</script>

<style lang="scss" scoped>
.header-container {
	padding: 24px 24px 6px;
	.title-img {
		display: flex;
		font-family: "PingFang SC";
		font-size: 22px;
		color: rgb(255 255 255 / 100%);
		align-items: center;
		> span {
			margin-right: auto;
		}
		> img {
			cursor: pointer;
			margin-left: 24px;
			height: 20px;
		}
	}
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Vue使用echarts柱形图预警变色,可以通过以下步骤实现: 1. 安装echarts ``` npm install echarts --save ``` 2. 在Vue组件引入echarts ``` import echarts from 'echarts' ``` 3. 在Vue组件定义echarts图表的配置项和数据 ``` data() { return { chartData: { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar' }] } } } ``` 4. 在Vue组件的mounted钩子函数初始化echarts图表,并设置预警变色 ``` mounted() { const chart = echarts.init(this.$refs.chart) chart.setOption(this.chartData) chart.setOption({ series: [{ itemStyle: { normal: { color: function(params) { if (params.data >= 150) { return '#FF5722' } else { return '#2196F3' } } } } }] }) } ``` 以上代码,我们通过设置itemStyle.normal.color属性来设置每个数据项的颜色。在这里,我们设置当数据项的值大于等于150时,颜色为橙色;否则,为蓝色。 5. 在Vue组件的template添加echarts图表 ``` <template> <div ref="chart" style="height: 400px;"></div> </template> ``` 通过以上步骤,我们就可以在Vue使用echarts柱形图预警变色了。 ### 回答2: 在Vue使用Echarts柱形图预警变色的方法如下: 1. 首先,在Vue项目安装echarts插件。可以使用npm或者yarn命令安装,例如:npm install echarts --save 2. 在Vue组件导入echarts并创建一个echarts实例。 3. 在data定义柱形图的数据和预警的阈值。 4. 在mounted或者created生命周期钩子函数,使用echarts的setOption方法设置柱形图的配置。 5. 在柱形图的配置,设置柱子的颜色。 6. 在柱形图的配置,根据预警标准,判断柱子是否需要变色,并设置相应的颜色。 下面是一个简单的示例代码,在Vue组件实现柱形图的预警变色: <template> <div id="chart"></div> </template> <script> import echarts from 'echarts' export default { data() { return { chartData: [120, 200, 150, 80, 70, 110, 130], // 柱形图的数据 warningThreshold: 100 // 预警阈值 } }, mounted() { this.renderChart() }, methods: { renderChart() { let chart = echarts.init(document.getElementById('chart')) chart.setOption({ xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G'] }, yAxis: { type: 'value' }, series: [ { type: 'bar', data: this.chartData, itemStyle: { normal: { color: function(params) { // 根据预警阈值判断柱子是否需要变色 if (params.value >= this.warningThreshold) { return 'red' } else { return 'blue' } }.bind(this) } } } ] }) } } } </script> 以上代码,使用echarts.init方法创建了一个echarts实例,并将其渲染到指定的DOM元素。在配置项的series属性,定义了一系列柱形图的配置,其的itemStyle属性用于设置柱子的颜色。通过判断柱子的值是否大于等于预警阈值,并返回相应的颜色来实现预警变色的效果。需要注意的是,在itemStyle的color配置项,使用了bind方法绑定了this指向,以便在其访问this.warningThreshold。 通过上述步骤,就可以在Vue实现echarts柱形图的预警变色功能。 ### 回答3: 在Vue使用echarts柱形图预警变色可以通过以下步骤实现: 1. 首先,引入echarts并在Vue组件进行相关配置。可以使用npm安装echarts,并在Vue组件引入echarts: ``` import echarts from 'echarts' ``` 2. 在Vue组件,创建一个DOM元素作为echarts图表的容器。可以使用Vue的ref属性来获取该元素的引用,并定义一个变量来保存echarts实例: ``` <template> <div ref="chartContainer"></div> </template> <script> export default { data() { return { chart: null } }, mounted() { this.initChart() }, methods: { initChart() { this.chart = echarts.init(this.$refs.chartContainer) // 省略其他配置 } } } </script> ``` 3. 在echarts的配置选项,通过设置itemStyle来实现预警柱形的颜色变化。可以根据需求设置不同的阈值和预警颜色: ``` initChart() { this.chart = echarts.init(this.$refs.chartContainer) const option = { // 省略其他配置 series: [{ type: 'bar', data: [10, 20, 30, 40, 50], itemStyle: { normal: { color: function(params) { // 根据条件判断是否触发预警变色 if (params.value > 30) { return 'red'; // 设置预警颜色为红色 } else { return 'blue'; // 设置其他柱形的颜色 } } } } }] } this.chart.setOption(option) } ``` 以上就是在Vue使用echarts柱形图预警变色的简单实现方式。根据具体需求,可以根据实际情况调整代码和配置,实现更灵活的柱形图预警变色效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值