vue highcharts 3d环形图 不同高度的环形图

vue highcharts 3d环形图 不同高度的环形图
在这里插入图片描述

  • 安装
npm install highcharts --save
  • 工具 /utils/echartPxToRem.js
export function fontChart(res) {
	let docEl = document.documentElement,
    clientWidth =
      window.innerWidth ||
      document.documentElement.clientWidth ||
      document.body.clientWidth;
  if (!clientWidth) return;
  // 此处的3840 为设计稿的宽度,记得修改!
  let fontSize = clientWidth / 3840;
  return res * fontSize;
}

  • 组件
<template>
	<div :id="id" style="height: 100%; width: 100%"></div>
</template>
<script>
import highcharts from "highcharts";
import { fontChart } from "@/utils/echartPxToRem.js";
export default {
	props: {
		id: {
			type: String,
			required: true,
		},
		dataList: {
			type: Array,
			default: () => [
				{
					name: "待受理",
					y: 10254,
					h: 0,
					bfb: 0,
				},
				{
					name: "已受理",
					y: 6894,
					h: 0,
					bfb: 0,
				},
				{
					name: "已确认",
					y: 7667,
					h: 0,
					bfb: 0,
				},
				{
					name: "已派单",
					y: 4287,
					h: 0,
					bfb: 0,
				},
				{
					name: "其他",
					y: 8687,
					h: 0,
					bfb: 0,
				},
			],
		},
	},
	watch: {
		dataList() {
			this.$nextTick(() => {
				this.initOption();
			});
		},
	},
	mounted() {
		// this.$nextTick(() => {
		this.initOption();
		// });
		window.addEventListener("resize", this.initOption);
	},
	destroyed() {
		window.removeEventListener("resize", this.initOption);
	},
	methods: {
		initOption() {
			let quantity = 0; // 总数
			this.dataList.forEach(item => {
				quantity += item.y;
			});
			this.dataList.forEach(item => {
				item.bfb = parseInt((item.y / quantity) * 100);
				item.h = item.bfb * 1.5 >= 70 ? 70 : item.bfb * 1.5;
				// item.h = parseInt(0.86 * item.bfb); // 最高高度60,根据比例渲染高度
				// console.log(this.dataList, "dataList----->>>");
			});
			// 修改3d饼图绘制过程
			var each = highcharts.each,
				round = Math.round,
				cos = Math.cos,
				sin = Math.sin,
				deg2rad = Math.deg2rad;
			highcharts.wrap(highcharts.seriesTypes.pie.prototype, "translate", function (proceed) {
				proceed.apply(this, [].slice.call(arguments, 1));
				// Do not do this if the chart is not 3D
				if (!this.chart.is3d()) {
					return;
				}
				var series = this,
					chart = series.chart,
					options = chart.options,
					seriesOptions = series.options,
					depth = seriesOptions.depth || 0,
					options3d = options.chart.options3d,
					alpha = options3d.alpha,
					beta = options3d.beta,
					z = seriesOptions.stacking ? (seriesOptions.stack || 0) * depth : series._i * depth;
				z += depth / 2;
				if (seriesOptions.grouping !== false) {
					z = 0;
				}
				each(series.data, function (point) {
					var shapeArgs = point.shapeArgs,
						angle;
					point.shapeType = "arc3d";
					var ran = point.options.h;
					shapeArgs.z = z;
					shapeArgs.depth = depth * 0.75 + ran;
					shapeArgs.alpha = alpha;
					shapeArgs.beta = beta;
					shapeArgs.center = series.center;
					shapeArgs.ran = ran;
					angle = (shapeArgs.end + shapeArgs.start) / 2;
					point.slicedTranslation = {
						translateX: round(cos(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad)),
						translateY: round(sin(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad)),
					};
				});
			});
			(function (H) {
				H.wrap(highcharts.SVGRenderer.prototype, "arc3dPath", function (proceed) {
					// Run original proceed method
					var ret = proceed.apply(this, [].slice.call(arguments, 1));
					ret.zTop = (ret.zOut + 0.5) / 100;
					return ret;
				});
			})(highcharts);
			highcharts.chart(this.id, {
				chart: {
					animation: false,
					backgroundColor: "none",
					type: "pie", //饼图
					margin: [0, 0, 0, 0],
					options3d: {
						enabled: true, //使用3d功能
						alpha: 58, //延y轴向内的倾斜角度
						beta: 0,
					},

					events: {
						load: function () {
							var each = highcharts.each,
								points = this.series[0].points;
							each(points, function (p, i) {
								p.graphic.attr({
									translateY: -p.shapeArgs.ran,
								});
								p.graphic.side1.attr({
									translateY: -p.shapeArgs.ran,
								});
								p.graphic.side2.attr({
									translateY: -p.shapeArgs.ran,
								});
							});
						},
					},
				},
				exporting: {
					enabled: false,
				},
				legend: {
					enabled: true, // 关闭图例
					align: "right", //水平方向位置
					verticalAlign: "top", //垂直方向位置
					layout: "vertical",
					x: fontChart(-10),
					y: fontChart(40),
					width: "300px",
					symbolWidth: fontChart(10),
					symbolHeight: fontChart(10),
					symbolRadius: "50%", // 修改成圆
					itemMarginBottom: fontChart(8),
					useHTML: true,
					labelFormat: "{name}&nbsp;&nbsp;&nbsp;&nbsp;{y}",
					// labelFormatter: function () {
					// 	return (
					// 		'<div style="width: .3125rem;display: inline-block; font-size: 26px">' +
					// 		this.name +
					// 		':&nbsp;&nbsp;</div><div style="color: #00d7da;display: inline-block">' +
					// 		this.y +
					// 		"</div>"
					// 	);
					// },
					itemStyle: {
						color: "#f4f4f6",
						fontSize: "20px",
					},
				},
				title: {
					// enabled: false,
					text: "",
				},
				subtitle: {
					text: "",
				},
				tooltip: {
					style: {
						fontSize: "26px",
					},
				},
				plotOptions: {
					pie: {
						allowPointSelect: false, // 禁用点击
						cursor: "pointer",
						depth: fontChart(15),
						showInLegend: true,
						size: "75%", // 外圈直径大小
						innerSize: fontChart(95), // 内圈直径大小
						center: ["30%", "50%"],
						colors: ["#3DA9C3", "#184DA0", "#7FC8EF", "#F6BE52", "#F08238"],
						dataLabels: {
							useHTML: true,
							enabled: true, //是否显示饼图的线形tip
							distance: 5,
							borderColor: "#007acc",
							align: "center",
							// verticalAlign: 'top',
							position: "right",
							format: "{point.bfb}%",
							// formatter: (point,b) => {
							//   console.log(point,'ponit-->>')
							//   console.log(b,'ponit-->>')
							// },
							color: "#ffffff",
							style: {
								textOutline: "none",
								fontSize: fontChart(13),
							},
						},
					},
				},
				credits: {
					enabled: false, // 禁用版权信息
				},
				series: [
					{
						type: "pie",
						name: "数量",
						data: this.dataList,
					},
				],
			});
		},
	},
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过使用 Vue.js 和 ECharts 来创建一个3D环形饼状。首先,确保你已经在你的项目中安装了 Vue.js 和 ECharts。 接下来,你可以按照以下步骤创建一个3D环形饼状: 1. 在你的 Vue 组件中引入 ECharts: ```javascript import echarts from 'echarts' ``` 2. 创建一个 div 元素作为表的容器: ```html <div id="chartContainer" style="width: 600px; height: 400px;"></div> ``` 3. 在 Vue 组件的 mounted 钩子函数中初始化表: ```javascript mounted() { this.initChart() }, methods: { initChart() { // 使用 echarts.init 初始化表容器 const chartContainer = document.getElementById('chartContainer') const chart = echarts.init(chartContainer) // 配置饼状的数据 const data = [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' } ] // 配置饼状的选项 const option = { tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, series: [ { name: '访问来源', type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: data } ] } // 使用 setOption 方法将配置项应用到表中 chart.setOption(option) } } ``` 以上代码会在表容器中绘制一个3D环形饼状,你可以根据自己的需求修改数据和选项来自定义表的样式和行为。 希望这能帮到你!如果有任何问题,请随时询问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值