echarts3D柱形图

567 阅读1分钟

记录一下为数不多的前端任务,基于echarts实现3D柱形图

效果图:

image.png

这里是将echarts放入了一个html,也可以将echarts代码摘出来灵活使用,同时x轴y轴的配置项也都在,只是这里没有显示出来,可根据需求自由配置

代码如下:

<html lang="zh-CN" style="height: 100%">
<head>
  <meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
  <div id="container" style="height: 100%"></div>

  
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js"></script>
  <!-- Uncomment this line if you want to dataTool extension
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.0/dist/extension/dataTool.min.js"></script>
  -->
  <!-- Uncomment this line if you want to use gl extension
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts-gl@2/dist/echarts-gl.min.js"></script>
  -->
  <!-- Uncomment this line if you want to echarts-stat extension
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts-stat@latest/dist/ecStat.min.js"></script>
  -->
  <!-- Uncomment this line if you want to use map
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@4.9.0/map/js/china.js"></script>
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@4.9.0/map/js/world.js"></script>
  -->
  <!-- Uncomment these two lines if you want to use bmap extension
  <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=YOUR_API_KEY"></script>
  <script type="text/javascript" src="https://fastly.jsdelivr.net/npm/echarts@5.4.0/dist/extension/bmap.min.js"></script>
  -->

  <script type="text/javascript">
    var dom = document.getElementById('container');
    var myChart = echarts.init(dom, null, {
      renderer: 'canvas',
      useDirtyRect: false
    });
    var app = {};
    
    var option;
	let x=['账号共用','路径探测','单IP多账户共用','境外IP访问','单次访问敏感数据'];
	let y=[90,80,70,15,10];
	const wid = 45;
	const w1 = Math.sin(Math.PI / 15) * wid; //4
	const w2 = Math.sin(Math.PI / 3) * wid; // 6.8
	const snapHeight = wid / 1.5;
	const CubeLeft = echarts.graphic.extendShape({
	   shape: {
		  x: 0,
		  y: 0
	   },
	   buildPath: function (ctx, shape) {
		  // 会canvas的应该都能看得懂,shape是从custom传入的
		  const xAxisPoint = shape.xAxisPoint
		  const c0 = [shape.x, shape.y]
		  const c1 = [shape.x - w2, shape.y]
		  const c2 = [shape.x - w2, xAxisPoint[1]]
		  const c3 = [shape.x, xAxisPoint[1]]
		  ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath()
	   }
	})
	// 绘制右侧面
	const CubeRight = echarts.graphic.extendShape({
	   shape: {
		  x: 0,
		  y: 0
	   },
	   buildPath: function (ctx, shape) {
		  const xAxisPoint = shape.xAxisPoint
		  const c1 = [shape.x, shape.y]
		  const c2 = [shape.x, xAxisPoint[1]]
		  const c3 = [shape.x + w1, xAxisPoint[1] - w2 + snapHeight]
		  const c4 = [shape.x + w1, shape.y - w2 + snapHeight]
		  ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath()
	   }
	})
	// 绘制顶面
	const CubeTop = echarts.graphic.extendShape({
	   shape: {
		  x: 0,
		  y: 0
	   },
	   buildPath: function (ctx, shape) {
		  //
		  const c1 = [shape.x, shape.y]
		  const c2 = [shape.x + w1, shape.y - w2 + snapHeight] //右点
		  const c3 = [shape.x - w2 + w1, shape.y - w2 + snapHeight]
		  const c4 = [shape.x - w2, shape.y]
		  ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath()
	   }
	})
	// 注册三个面图形
	echarts.graphic.registerShape('CubeLeft', CubeLeft)
	echarts.graphic.registerShape('CubeRight', CubeRight)
	echarts.graphic.registerShape('CubeTop', CubeTop)
	option = {
	   title:{
		  text:'',
		  x: 'center',
		  y:'20',
		  textStyle: {
				color: '#fff',
				fontSize: 20
		  }
	   },
	   backgroundColor:'rgba(0,0,0,.8)',
	   tooltip: {
				show:false,
				trigger: 'axis',
				axisPointer: {
					type: 'shadow'
				},
				
			},
			grid: {
				left: '5%',
				right: '5%',
				top: '15%',
				bottom: '10%',
				containLabel: true
			},
			xAxis: {
				type: 'category',
				name:'',
				nameTextStyle:{
					color:'rgba(16, 213, 223, .5)',
					fontSize:12
				},
				data: x,
				axisLine: {
					show: false,
					lineStyle: {
						color: '#3e6f8e',
						width:1
					}
				},
				axisTick: {
					show: false,
					length: 9,
					alignWithLabel: true,
					lineStyle: {
						color: '#AAA'
					}
				},
				axisLine: {
					show: false,
					lineStyle: {
						color: 'rgba(27, 202, 242, .2)',
						width:1
					}
				},
				axisLabel:{
					interval:0,
					rotate:0,
					color: '#FFFFFF'
				},
				splitLine:{
					show: false,
					lineStyle: {
						color: '#ffffff',
						opacity:0.2,
						width:1
					}
				},
			},
			yAxis: {
				name:'',
				min:0,
				max:100,
				type: 'value',
				nameTextStyle:{
					color:'rgba(16, 213, 223, 1)',
					fontSize:12
				},
				axisLine: {
					show: false,
					lineStyle: {
						color: 'rgba(27, 202, 242, .2)',
						width:1
					}
				},
				axiosTick:{
				  show:false
				},
				axisLabel: {
					show: false,
					color: 'rgba(16, 213, 223, .4)',
					fontSize:12
				},
				splitLine:{
					show: false,
					lineStyle: {
						color: 'rgba(5, 80, 123, .1)',
						type:'dashed',
						width:1
					}
				},
			},
			series: [
				{
					  type: 'bar',
					  label: {
						normal: {
						  show: true,
						  position: 'top',
						  fontSize: 12,
						  color: '#FFFFFF',
						  formatter:function(params){
							  return params.value
						  },
						  offset: [0, -10],
						},
					  },
					  tooltip: {
						show:false
					  },
					  itemStyle: {
						color: 'transparent',
					  },
					  data:y,
					},
				{
					type: 'custom',
					renderItem: (params, api) => {
						const location = api.coord([api.value(0), api.value(1)]);
						location[0] = location[0] + wid*0;
						const xlocation = api.coord([api.value(0), 0]);
						xlocation[0] = xlocation[0] + wid*0;
						return {
							type: 'group',
							children: [{
								type: 'CubeLeft',
								shape: {
									api,
									xValue: api.value(0),
									yValue: api.value(1),
									x: location[0],
									y: location[1],
									xAxisPoint: xlocation
								},
								style: {
									fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
										offset: 0,
										color: 'rgba(5,157,230,0.5)',
										opacity:1
									},
										{
											offset: 1,
											color: 'rgba(5,157,230,0.5)'
										}
									])
								}
							}, {
								type: 'CubeRight',
								shape: {
									api,
									xValue: api.value(0),
									yValue: api.value(1),
									x: location[0],
									y: location[1],
									xAxisPoint: xlocation
								},
								style: {
									fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
										offset: 0,
										color: 'rgba(0,172,207,0.5)'
									},
										{
											offset: 1,
											color: 'rgba(0,172,207,0.5)'
										}
									])
								}
							}, {
								type: 'CubeTop',
								shape: {
									api,
									xValue: api.value(0),
									yValue: api.value(1),
									x: location[0],
									y: location[1],
									xAxisPoint: xlocation
								},
								style: {
									fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
										offset: 0,
										color: 'rgba(23,224,254,0.5)'
									},
										{
											offset: 1,
											color: 'rgba(23,224,254,0.5)'
										}
									])
								}
							}]
						}
					},
					color:'blue',
					data: y
				}]
	};

    if (option && typeof option === 'object') {
      myChart.setOption(option);
    }

    window.addEventListener('resize', myChart.resize);
  </script>
</body>
</html>

[代码片段](https://code.juejin.cn/pen/7158716999634255887)

同时分享一个echarts的宝藏网站,此代码是基于网站上的代码修改的,链接如下:

点击查看修改的原始代码