带阴影可切换年月日的折线图/曲线图

1. 实现带阴影的折线图,并可切换年月日进行展示不同的数据

带阴影的折线图
效果展示:

带阴影且可进行年月日切换的折线图/曲线图

HTML

<el-card class="box-card">
    <div slot="header" class="clearfix">
        <span>报警趋势统计</span>
		<div class="select_time" >
			<span class="active" data-type=""></span>
			<span data-type=""></span>
			<span data-type=""></span>
		</div>
    </div>
    <div id="alarm_trend"></div>
</el-card>

CSS:样式可根据自己情况自己写,“年月日”是通过子绝父相定位

        .el-card {
            font-size: 20px;
            width: 90%;
            margin: 30px auto;
            border: none;
            background-color: #051a38;
            overflow: hidden;
            color: #fff;
            -webkit-transition: .3s;
            transition: .3s;
        }
        .el-card__header {
            border-bottom: 1px solid #5dd2f1;
            box-sizing: border-box;
			padding: 10px 10px;
        }
		#alarm_trend {
			width: 100%;
			height: 400px;
			/* background:pink; */
		}
		.clearfix {
			position: relative;
		}
		.select_time {
			width: 210px;
			/* border-radius: 5px; */
			float: right;
			background: #1e3d64;
			left: 74%;
    		top: 221%;
			position: absolute;
	        z-index: 11;
		}

		.select_time span {
			display: block;
			width: 70px;
			height: 35px;
			float: left;
			/* background: #4095e5; */
			text-align: center;
			line-height: 35px;
			color: #1677df;
		}
		.select_time .active {
			background-color: #4c9bfd;
  			color: #74f8fd;
		}

JS:绘制折线图的模板,获取数据后直接调用该函数进行图表的渲染

			// 折线图
			gradientChart(id, xData,serialData) {
				echarts.dispose(document.getElementById(id));

				let myChart = echarts.init(document.getElementById(id));
				myChart.setOption({
					color:['#88ffc3','#4992ff'],
					tooltip: {
						// 通过坐标轴来触发
						trigger: "axis"
					},
					legend: {
						// 距离容器
						// right: "50%",
						// left:center,
						// 修饰图例文字的颜色
						textStyle: {
							color: "#fff"
						}
					},
					grid: {
						top: "20%",
						left: "5%",
						right: "5%",
						bottom: "3%",
						show: true,
						borderColor: "#012f4a",
						containLabel: true
					},

					xAxis: {
						type: 'category',
						axisTick: {
							show: true,
								length:5, 
						},
						interval: 1,
						axisLabel: {
							color: "#62b292",
							fontSize:"1rem"
						},
						axisLine: {
							show: false
						},
						data: xData,
					},
					yAxis: {
						type: 'value',
						name: '',
						axisLabel: {
							textStyle: {
								//坐标轴颜色
								color: "#62b292",
								fontSize:"1rem"
							}
						},
						//坐标轴线样式
						splitLine: {
							show: true,
							lineStyle: {
								type: 'solid', //solid实线;dashed虚线
								color: "#2b5da7"
							}
						},
					},
					series: [
						{
							name: "终端",
							type: 'line',
							smooth: true, //true曲线; false折线
							itemStyle: {
								normal: {
									label : {
									show: true,
									color:'#4992ff'
									},
								}
							},
							areaStyle: {
								//折线图颜色半透明
								color: {
									type: 'linear',
									x: 0,
									y: 0,
									x2: 0,
									y2: 1,
									colorStops: [{
										offset: 0, color: 'rgba(134, 251, 192, 0.5)' // 0% 处的颜色
									}, {
										offset: 1, color: 'rgba(134, 251, 192, 0.1)' // 100% 处的颜色
									}],
									global: false // 缺省为 false
								}
								
							},
							data: serialData[0]
						},
						{
							name: "辅控",
							type: 'line',
							smooth: true, //true曲线; false折线
							itemStyle: {
								normal: {
									label: {
										show: true,
										color:'rgba(134, 251, 192)'
									},
								}
							},
							areaStyle: {
								//折线图颜色半透明
								color: {
									type: 'linear',
									x: 0,
									y: 0,
									x2: 0,
									y2: 1,
									colorStops: [{
										offset: 0, color: 'rgba(36,173,254, 0.5)' // 0% 处的颜色
									}, {
										offset: 1, color: 'rgba(52,112,252, 0.1)' // 100% 处的颜色
									}],
									global: false // 缺省为 false
								}
							},
							data: serialData[1]
						}
					]
				});
				// 当我们浏览器缩放的时候,图表也等比例缩放
				window.addEventListener("resize", function() {
					// 让我们的图表调用 resize这个方法
					myChart.resize();
				});
			},

JS:获取图表数据,渲染图表

			// 报警趋势统计
			alarmTrendChart(type) {
				if(type) {
					this.time_type = type
				} else {
					this.time_type = '年'
				}
				var that = this;
				this.httpPost('admin-alarm-api/alarm-trend-statistics', {
					admin_user_id: admin_user_id,
					admin_user_token: admin_user_token,
					time_type: this.time_type
				}, function(msg, data) {
					that.gradientChart('alarm_trend',data.x_data,data.needData)
				})

				
			},

JS:年月日的切换

		mounted: function() {
			this.alarmTrendChart()
			// 年月日的切换
			var that = this
			$(".select_time").on("click", "span", function() {
				// 此时要注意这个索引号的问题
				index = $(this).index() - 1;
				// 点击当前a 高亮显示 调用active
				$(this)
				.addClass("active")
				.siblings("span")
				.removeClass("active");
				
				that.alarmTrendChart(this.dataset.type)

			});
		},
erchart 折线图库是一种用于在网页上绘制折线图的 JavaScript 库。它可以通过监听选择项的变化来实现根据、月、日来切换数据显示的折线图。 首先,我们需要一个包含、月、日选择的表单,可以使用 HTML 的 \<select> 元素来实现。在每个选择项的\<option> 标签中,我们可以设置一个值来表示选择的时间粒度,例如,“year”表示,”month“表示月,”day“表示日。 然后,我们需要在 JavaScript 中监听选择项的变化。可以使用事件监听器,当选择项的值发生变化时,触发一个函数来重新绘制折线图。在这个函数中,我们可以根据选择的时间粒度,向后台请求相应的数据。 接下来,我们需要通过使用 erchart 折线图库的 API 来绘制折线图。我们可以在 HTML 文件中添加一个 \<canvas> 元素,作为折线图的容器。然后,在 JavaScript 中,我们可以使用 erchart 提供的 API 来创建一个实例,并将数据和配置传递给它。 在数据准备好之后,我们可以将数据传递给折线图实例,并根据时间粒度的选择进行相应的处理。对于,我们可以将每一的数据合并为一个点,显示为折线图上的一个坐标。对于月,我们可以将每个月的数据显示为折线图上的一个坐标。对于日,我们可以将每天的数据显示为折线图上的一个坐标。 最后,我们可以在绘制折线图时,设置一些样式,如颜色、线条粗细等,来美化折线图的显示效果。另外,我们还可以添加一些交互功能,如鼠标悬停时显示数据点的具体数值等。 总结来说,使用 erchart 折线图库可以实现根据选择项的、月、日来切换数据显示的折线图。我们只需要监听选择项的变化,重新请求相应的数据,然后使用 erchart 提供的 API 绘制折线图即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值