echarts生成误差棒图

 首先声明,这个图是从这个哥们这里弄来的。自己再加以修改的。以更加贴合我自己的代码需求

echarts 自定义误差图_贵林之恋的博客-CSDN博客_echarts 误差棒<script>$(function() {// 基于准备好的dom,初始化echarts实例var myChart = echarts.init(document.getElementById('main'));// 指定图表的配置项和数据var categoryData = []; //X轴数据var errorDat...https://blog.csdn.net/zyg1515330502/article/details/81908983

var myChart = echarts.init(document.getElementById('main'));
	 
					// 指定图表的配置项和数据	
					var categoryData = []; //X轴数据
					var errorData = []; //错误的数据
					var barData = []; // 柱状图数据
					var dataCount = 2; //数据数量
					for(var i = 0; i < dataCount; i++) {
						var val = Math.random() * 1000; //random() 方法可返回介于 0 ~ 1 之间的一个随机数。
						categoryData.push(i);
						errorData.push([
							i,
							echarts.number.round(Math.max(0, val - Math.random() * 100)), //max() 方法可返回两个指定的数中带有较大的值的那个数。
							echarts.number.round(val + Math.random() * 80)
						]);
						barData.push(echarts.number.round(val, 2));
					}
					
					console.log(echarts.number.round);
					console.log(errorData);
					console.log(barData);
					
					/*
						params:包含了当前数据信息和坐标系的信息。
						api:是一些开发者可调用的方法集合。
						api.value(...),意思是取出 dataItem 中的数值。例如 api.value(0) 表示取出当前 dataItem 中第一个维度的数值。
						api.coord(...),意思是进行坐标转换计算。例如 var point = api.coord([api.value(0), api.value(1)]) 表示 dataItem 中的数值转换成坐标系上的点。
						api.size(...) 函数,表示得到坐标系上一段数值范围对应的长度。
						shape 属性描述了这个矩形的像素位置和大小。
					*/
					
					function renderItem(params, api) {
						var xValue = api.value(0); //api.value(0) 表示取出当前 dataItem 中第一个维度的数值。
						var highPoint = api.coord([xValue, api.value(1)]); //高点
						var lowPoint = api.coord([xValue, api.value(2)]); //低点
						// var halfWidth = api.size([1, 0])[0] * 0.1; //半宽度
						var halfWidth = 10;
						var style = api.style({
							stroke: api.visual('color'),
							fill: null
						});					
	 
						return {
							type: 'group',
							children: [
								{
									type: 'line',
									shape: {
										x1: highPoint[0] - halfWidth,
										y1: highPoint[1],
										x2: highPoint[0] + halfWidth,
										y2: highPoint[1]
									},
									style: style
								}, 
								{
									type: 'line',
									shape: {
										x1: highPoint[0],
										y1: highPoint[1],
										x2: lowPoint[0],
										y2: lowPoint[1]
									},
									style: style
								}, 
								{
									type: 'line',
									shape: {
										x1: lowPoint[0] - halfWidth,
										y1: lowPoint[1],
										x2: lowPoint[0] + halfWidth,
										y2: lowPoint[1]
									},
									style: style
								}
							]
						};
					}
	 
					option = {
						tooltip: {
							trigger: 'axis',
							axisPointer: {
								type: 'shadow'
							}
						},					
						title: [
							{
								text: '项目:钾',
								left: '45%',
								top: 20,
								textStyle: {
									color: 'green',
									//fontSize: '14',
								},
							}, 
							{
								text: '批号:201711',
								left: '51%',
								top: 20,
								textStyle: {
									color: 'green',
									//fontSize: '14',
								},
							}, 
							{
								text: '仪器',
								//borderColor: '#999',
								//borderWidth: 1,
								textStyle: {
									fontSize: 14
								},
								left: 'center',
								top: '94%'
							},
						],
	//					legend: {
	//						data: ['bar', 'error']
	//					},
						/*dataZoom: [
							{
								type: 'slider',
								start: 50,
								end: 70
							},
							{
								type: 'inside',
								start: 50,
								end: 70
							}
						],*/
						xAxis: {
							data: categoryData,						
						},
						yAxis: {},
						series: [
							{
								type: 'scatter',
								name: 'scatter',
								data: barData,
								itemStyle: {
									normal: {
										color: '#77bef7',
										borderColor:"#5470C6"
									}
								}
							}, 
							{
								type: 'custom',
								name: 'error',
								itemStyle: {
									normal: {
										borderWidth: 1.5
									}
								},
								renderItem: renderItem,
								encode: { //可以定义 data 的哪个维度被编码成什么
									x: 0,// data 中『维度0』对应到 X 轴
									y: [1, 2] // data 中『维度1』和『维度2』对应到 Y轴
								},
								data: errorData,
								z: 100
							},
							{
							      name: 'MA30',
							      type: 'line',
							      data: barData,
							      
							      showSymbol: false,
							      lineStyle: {
							        width: 3,
									 type:'dotted'
							      }
							    }
						]
					};
	 
					// 使用刚指定的配置项和数据显示图表。
					myChart.setOption(option);

Matlab误差棒图绘制是一种用来可视化数据中的误差范围的方法。误差棒图通常由柱状和误差条组成。 绘制误差棒图的第一步是准备数据。假设我们有一组数据集,其中每个数据点都有一个平均值以及上下误差界限。 在Matlab中,可以使用`errorbar`函数来绘制误差棒图。该函数的第一个参数是数据点的位置,第二个参数是平均值,第三个和第四个参数分别是上下误差界限的值。例如,以下代码可以绘制一个简单的误差棒图: ```matlab x = 1:5; % 数据点位置 y = [1.2 1.5 1.3 1.6 1.4]; % 平均值 e = [0.1 0.3 0.2 0.4 0.15]; % 误差界限 errorbar(x, y, e); ``` 在绘制误差棒图时,可以使用额外参数来自定义表的外观。例如,可以使用`'o'`参数来绘制数据点,使用`'LineWidth'`参数来设置误差条的宽度,使用`'MarkerSize'`参数来设置数据点的大小等。 除了基本的误差棒图,Matlab还提供了其他类型的误差棒图。例如,`errorbarxy`函数可以绘制水平和垂直方向上带有误差的散点。`errorbarlogx`和`errorbarlogy`函数可以绘制具有对数坐标轴的误差棒图。 绘制完误差棒图后,可以使用Matlab中的其他函数进一步对表进行修饰、标注和保存。例如,可以使用`xlabel`和`ylabel`函数来添加坐标轴标签,使用`title`函数来添加标题,使用`legend`函数来添加例等。 总而言之,使用Matlab绘制误差棒图是一个简单而强大的方法,可以帮助我们更好地理解数据中的误差范围,进行数据分析和可视化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值