ECharts实现立体柱形图

目录

一、效果图预览

二、准备

 1.JavaScript源码运行文件,用以运行代码

 2.需要文本编辑器如notepad++

三、编写图像属性代码

1.编辑界面预览

2.完整代码

四、运行


一、效果图预览

 

二、准备

 1.JavaScript源码运行文件,用以运行代码

需要运行文件的自行下载

http://链接:https://pan.baidu.com/s/1shmtM36HgRqC0S1OXpNWpg?pwd=yozs提取码:yozs --来自百度网盘超级会员V2的分享

https://pan.baidu.com/s/1shmtM36HgRqC0S1OXpNWpg?pwd=yozs

下载的运行文件预览:

 2.需要文本编辑器如notepad++

好用的文本编辑器能够使编辑和查错更方便,当然也可以用记事本进行编辑。

三、编写图像属性代码

1.编辑界面预览

2.完整代码

<!DOCTYPE html>
<html>

	<head>
    		<meta charset="utf-8" />
    		<!-- 引入刚刚下载的 ECharts 文件 -->
    		<script src="echarts.min.js"></script>
  	</head>

	<body>
  		<!-- 为 ECharts 准备一个定义了宽高的 DOM -->
  		<div id="main" style="width: 1000px;height:400px;"></div>

<script type="text/javascript">
		// 基于准备好的dom,初始化echarts实例
		var myChart = echarts.init(document.getElementById('main')); 
		
		// 绘制正侧面
		var positiveShape = echarts.graphic.extendShape({
			shape: {
				x: 0,
				y: 0
			},
			buildPath: function (ctx, shape) {
				const xAxisPoint = shape.xAxisPoint
				ctx.moveTo(shape.x+20, shape.y);      //右上
				ctx.lineTo(shape.x - 20, shape.y );  //左上
				ctx.lineTo(xAxisPoint[0] - 20 , xAxisPoint[1] );   //左下
				ctx.lineTo(xAxisPoint[0] +20, xAxisPoint[1]);    //右下
				ctx.closePath();
			}
		});
		
		// 绘制右侧面
		  var  rightShape = echarts.graphic.extendShape({
			shape: {
				x: 0,
				y: 0
			},
		   buildPath: function (ctx, shape) {
				const xAxisPoint = shape.xAxisPoint
				ctx.moveTo(shape.x+25, shape.y-13);  // 右上
				ctx.lineTo(shape.x + 20, shape.y );
				ctx.lineTo(xAxisPoint[0] +20 , xAxisPoint[1] );
				ctx.lineTo(xAxisPoint[0]+25 , xAxisPoint[1]-13);   //右下
				ctx.closePath();
			}
		});
		
		// 绘制顶部
		  var topShape = echarts.graphic.extendShape({
			shape: {
				x: 0,
				y: 0
			},
		   buildPath: function (ctx, shape) {
				
				ctx.moveTo(shape.x+20, shape.y);      //右下
				ctx.lineTo(shape.x + 25, shape.y - 13);   //右上
				ctx.lineTo(shape.x - 15 , shape.y - 13);   //左上
				ctx.lineTo(shape.x - 20, shape.y );   //左下
				ctx.closePath();
			}
		});
		
		// 注册三个面图形
				echarts.graphic.registerShape('positiveShape', positiveShape)
				echarts.graphic.registerShape('rightShape', rightShape)
				echarts.graphic.registerShape('topShape', topShape)
				
		// 指定图表的配置项和数据
		var option = {
			backgroundColor: new echarts.graphic.LinearGradient(
			 
			 0, 0, 0, 1,
			 [
				{ 
					offset: 0,
					color: '#5f5e61'
				},
				{
					offset: .4,
					color: '#F8F8FF'
				},
				{
					offset: .6,
					color: '#F8F8FF'
				},
				{
					offset: 1,
					color: '#5f5e61'
				}
			]// (x1,y1) 点到点 (x2,y2) 之间进行渐变
		 ),
			tooltip: {},
			
			legend: {
				type:'plain',
				data: ['满意率'],
				top: 'middle',
				orient: 'vertical',
				icon: 'roundRect',
				x:'right',      //可设定图例在左(left)、右(right)、居中(center)、填写数字(填写100代表的是100px)
				y:'center',     //可设定图例在上(top)、下(bottom)、居中(center)、填写数字(填写100代表的是100px)
				padding:[0,25,0,0],   //可设定图例[距上方距离,距右方距离,距下方距离,距左方距离]
				itemHeight: 15,
				itemWidth:15,
				borderWidth:1,
				color:"000",
				itemStyle:{
					
					 borderColor:'#000',
				},
				borderColor:'black'
  
			},//标签
		
			xAxis: {
				show: true,
				type:'category',
				//坐标轴轴线相关设置。
				axisLine: {
					lineStyle: {
						//坐标轴颜色
						color:'#000000'   
						}
				},
				//坐标轴刻度相关设置。
				axisTick:{
					show:false
				},
				
				axisLabel:{
					show: true,
					textStyle: {
						color: '#000000', //坐标轴文字颜色
					}
				},
				axisTick:{
					 show:true,//是否展示轴刻度
					 inside:true//坐标轴刻度是否朝内
				},
				data: ['客户服务', '公共秩序服务', '公共维修服务', '上门维修服务', '绿化养护服务', '公共保洁服务','社区文化建设']				
			},
			
			
			yAxis: {
				//坐标轴轴线相关设置。
				show:'category',
				type:'value',
				//自动计算的坐标轴最大间隔大小
				maxInterval:100,
				//坐标轴轴线相关设置。
				axisLine: { 
					show: true, 
					lineStyle: {   
					//坐标轴线的颜色。
					color: '#000000'
					}
				},
				axisTick: {
					show: false
				},
				splitLine:{    //坐标轴在 grid 区域中的分隔线。
					show:true,
					lineStyle: {     //分隔线颜色
							color:'#000000',
							width:0.5   //分隔线线宽。
					}
				},
				
				axisTick:{
					show:true,//是否展示轴刻度
					inside:true,//坐标轴刻度是否朝内
					 
					 
				},
			},
			series: [
				{
					name: '满意率',
					type: 'custom',
					barWidth:28,   //柱条的宽度
					color:'#DAA520',
					
					/*labelLayout(params, api) {
						return {
							x: location[0],
							y: location[1],
							verticalAlign: 'top',
							align: 'left'
							
						}
						
					},*/
					

					
					renderItem: function(params, api) {
						const location = api.coord([api.value(0), api.value(1)])
						return {
							type: 'group',
							children: [{
								//正面
								type: 'positiveShape',
								shape: {
									api,
									x: location[0],
									y: location[1],
									xAxisPoint: api.coord([api.value(0), 0])
								},
								style: {
									fill: '#DAA520'
									
								}
							}, 
							{
							type: 'rightShape',
							shape: {
								api,
									x: location[0],
									y: location[1],
									xAxisPoint: api.coord([api.value(0), 0])
							},
							style: {
								fill: '#444444'
							}
							}, 
							{//顶部
								type: 'topShape',
								shape: {
									api,
									x: location[0],
									y: location[1],
									xAxisPoint: api.coord([api.value(0), 0])
								},
								style: {
									fill: '#D8860B'
								}
							}]
						}
					},
					data: [{
							value: 20.4,
							itemStyle: {
							color: '#DAA520',
							}
						}, 
						{
							value: 27.4,
							itemStyle: {
							color:'#DAA520',
							}	
						},
						{
							value: 90,
							itemStyle: {
							color:'#DAA520',
							}
						},
						{
							value: 20.4,
							itemStyle: {
							color:'#DAA520',
							}
						},
						{
							value: 80,
							itemStyle: {
							color:'#DAA520',
							}
						},
						{
							value: 80,
							itemStyle: {
							color:'#DAA520',
							}
						},
						{
							value: 80,
							itemStyle: {
							color:'#DAA520',
							}
					}],

					
				},
				{	
					//柱形顶端显示x轴数据
					type: "bar",
					label: {
						normal: {
							show: true,
							position: "top",
							fontSize: 14,
							color: "#000000",
						},
					},
					itemStyle: {
					color: "transparent",
					},
					data:['20.4','27.4','90','20.4','80','80','80'],
				},
			
			]
		};
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    </script>

	</body>

</html>

编辑完成后需要保存为html文件,否则无法运行。 

四、运行

 注意:html文件需要和js文件在同一个目录下,否则图像代码编辑当中引入ECharts文件时就需要指定路径。

 浏览器打开html文件即可查看运行结果。

想要了解和学习更多图形配置项可浏览Echarts官网

Documentation - Apache ECharts

最后,如果内容对你有帮助,不妨点个赞!!

猜你喜欢

转载自blog.csdn.net/weixin_61569821/article/details/127645581