Echarts实现折线图效果

在这里插入图片描述

let colorArr = ['#FAD237', '#3469FC', '#9013FE', '#E5579A', '#34D129'];
let gradientArr = ['250,210,55', '52,105,252', '144,19,254', '229,87,154', '52,209,41'];
initLineChart();

// 图表调用并赋值
function initLineChart() {
	let data1 = {
		xAxisData: ['2012', '2013', '2014', '2015', '2016', '2017', '2018'],
		seriesData: [{
			name: '实际用电曲线',
			data: [861, 559, 343, 604, 459, 728, 844]
		}, {
			name: '中长期聚合曲线',
			data: [533, 454, 360, 770, 607, 328, 449]
		}, {
			name: '长期聚合曲线',
			data: [461, 583, 421, 455, 515, 739, 583]
		}]
	};
	basicLineChart1(this.$Echarts.init(document.getElementById('line1')), data1);
	ladderLineChart(this.$Echarts.init(document.getElementById('line2')), data1);
	setLineImgChart(this.$Echarts.init(document.getElementById('line3')), data1);
	basicLineChart2(this.$Echarts.init(document.getElementById('line4')), data1);
}


/**
 * 基础折线图
 * @param 	chart:echart容器
 * 			data: 折线图数据
 */
export function basicLineChart1(chart, chartData) {
	let dataArr = [];
	for(let i = 0; i < chartData.seriesData.length; i++) {
		let obj = chartData.seriesData[i];
		dataArr.push({
			name: obj.name,
			type: 'line',
			symbolSize: 0,
			smooth: true,
			lineStyle: {
				color: 'rgb(' + gradientArr[i] + ')',
				width: 2,
				type: 'solid'
			},
			areaStyle: {
				color: {
					type: 'linear',
					x: 0,
					y: 0,
					x2: 0,
					y2: 1,
					colorStops: [{
						offset: 0,
						color: 'rgba(' + gradientArr[i] + ',.5)'
					}, {
						offset: 1,
						color: 'rgba(' + gradientArr[i] + ',0)'
					}]
				}
			},
			data: obj.data
		})
	}

	let option = {
		color: colorArr,
		grid: {
			left: 20,
			right: 20,
			bottom: 30,
			top: 50,
			containLabel: true
		},
		legend: {
			icon: 'rect',
			right: 'center',
			top: 0,
			itemWidth: 15,
			itemHeight: 10,
			itemGap: 35,
			textStyle: {
				color: "#fff"
			},
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'cross',
				lineStyle: {
	                color: {
	                    type: 'linear',
	                    x: 0,
	                    y: 0,
	                    x2: 0,
	                    y2: 1,
	                    colorStops: [{
	                        offset: 0,
	                        color: 'rgba(0, 255, 233,0)'
	                    }, {
	                        offset: 0.5,
	                        color: 'rgba(255, 255, 255,1)',
	                    }, {
	                        offset: 1,
	                        color: 'rgba(0, 255, 233,0)'
	                    }],
	                }
	            }
			}
		},
		xAxis: {
			type: 'category',
			data: chartData.xAxisData,
			axisTick: {
				show: false,
			},
			axisLine: {
				lineStyle: {
					color: 'rgba(255,255,255,0.2)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		yAxis: {
			type: 'value',
			axisTick: {
				show: false,
			},
			axisLine: {
				show: false,
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted',
					color: 'rgba(255,255,255,0.1)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		series: dataArr
	};
	chart.setOption(option);
}

/**
 * 基础折线图
 * @param 	chart:echart容器
 * 			data: 折线图数据
 */
export function basicLineChart2(chart, chartData) {
	let dataArr = [];
	for(let i = 0; i < chartData.seriesData.length; i++) {
		let obj = chartData.seriesData[i];
		dataArr.push({
			name: obj.name,
			type: 'line',
			symbol: 'circle',// 实心圆
			symbolSize: 12,
            itemStyle: {
//				color: {
//					type: 'radial',
//					colorStops: [{
//						offset: 0,
//						color: colorArr[i]
//					}, {
//						offset: 0.5,
//						color: colorArr[i]
//					}, {
//						offset: 0.5,
//						color: 'transparent',
//					}, {
//						offset: 1,
//						color: 'transparent',
//					}]
//				},
				borderColor: colorArr[i],
				borderWidth: 1,
			},
			lineStyle: {
				width: 2,
			},
			data: obj.data
		})
	}

	let option = {
		color: colorArr,
		grid: {
			left: 20,
			right: 20,
			bottom: 30,
			top: 50,
			containLabel: true
		},
		legend: {
			right: 'center',
			top: 0,
			itemWidth: 15,
			itemHeight: 10,
			itemGap: 35,
			textStyle: {
				color: "#fff"
			},
		},
		tooltip: {
			trigger: 'axis',
		},
		xAxis: {
			type: 'category',
			data: chartData.xAxisData,
			axisTick: {
				show: false,
			},
			axisLine: {
				lineStyle: {
					color: 'rgba(255,255,255,0.2)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		yAxis: {
			type: 'value',
			axisTick: {
				show: false,
			},
			axisLine: {
				show: false,
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted',
					color: 'rgba(255,255,255,0.1)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		series: dataArr
	};
	chart.setOption(option);
}

/**
 * 更改拐点图标
 * @param 	chart:echart容器
 * 			data: 折线图数据
 */
export function setLineImgChart(chart, chartData) {
	let dataArr = [];
	for(let i = 0; i < chartData.seriesData.length; i++) {
		let obj = chartData.seriesData[i];
		dataArr.push({
			name: obj.name,
			type: 'line',
			symbolSize: 30,
			symbolOffset: [3, 0],
			symbol: 'image://img/icon.png',
			lineStyle: {
				color: 'rgb(' + gradientArr[i] + ')',
				width: 2,
				type: 'solid'
			},
			data: obj.data
		})
	}

	let option = {
		color: colorArr,
		grid: {
			left: 20,
			right: 20,
			bottom: 30,
			top: 50,
			containLabel: true
		},
		legend: {
			icon: 'rect',
			right: 'center',
			top: 0,
			itemWidth: 15,
			itemHeight: 10,
			itemGap: 35,
			textStyle: {
				color: "#fff"
			},
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				lineStyle: {
	                color: {
	                    type: 'linear',
	                    x: 0,
	                    y: 0,
	                    x2: 0,
	                    y2: 1,
	                    colorStops: [{
	                        offset: 0,
	                        color: 'rgba(0, 255, 233,0)'
	                    }, {
	                        offset: 0.5,
	                        color: 'rgba(255, 255, 255,1)',
	                    }, {
	                        offset: 1,
	                        color: 'rgba(0, 255, 233,0)'
	                    }],
	                }
	            }
			}
		},
		xAxis: {
			type: 'category',
			data: chartData.xAxisData,
			axisTick: {
				show: false,
			},
			axisLine: {
				lineStyle: {
					color: 'rgba(255,255,255,0.2)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		yAxis: {
			type: 'value',
			axisTick: {
				show: false,
			},
			axisLine: {
				show: false,
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted',
					color: 'rgba(255,255,255,0.1)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		series: dataArr
	};
	chart.setOption(option);
}

/**
 * 阶梯图
 * @param 	chart:echart容器
 * 			chartData: 图表数据
 */
export function ladderLineChart(chart, chartData) {
	let seriesData = [];
	for(let i = 0; i < chartData.seriesData.length; i++) {
		let obj = chartData.seriesData[i];
		seriesData.push({
			name: obj.name,
			type: 'line',
            step: "start",
            showAllSymbol: true,
            symbol: 'circle',
            symbolSize: 10,
            itemStyle: {
                color: colorArr[i],
                borderWidth: 3,
                borderColor: "rgba(255,255,255,.3)",
            },
			data: obj.data
		})
	}
	let option = {
		grid: {
			left: 20,
			right: 20,
			bottom: 30,
			top: 50,
			containLabel: true
		},
		legend: {
			icon: 'rect',
			right: 'center',
			top: 0,
			itemWidth: 15,
			itemHeight: 10,
			itemGap: 35,
			textStyle: {
				color: "#fff"
			},
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				lineStyle: {
	                color: {
	                    type: 'linear',
	                    x: 0,
	                    y: 0,
	                    x2: 0,
	                    y2: 1,
	                    colorStops: [{
	                        offset: 0,
	                        color: 'rgba(0, 255, 233,0)'
	                    }, {
	                        offset: 0.5,
	                        color: 'rgba(255, 255, 255,1)',
	                    }, {
	                        offset: 1,
	                        color: 'rgba(0, 255, 233,0)'
	                    }],
	                }
	            }
			}
		},
		xAxis: {
			type: 'category',
			data: chartData.xAxisData,
			axisTick: {
				show: false,
			},
			axisLine: {
				lineStyle: {
					color: 'rgba(255,255,255,0.2)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		yAxis: {
			type: 'value',
			axisTick: {
				show: false,
			},
			axisLine: {
				show: false,
			},
			splitLine: {
				show: true,
				lineStyle: {
					type: 'dotted',
					color: 'rgba(255,255,255,0.1)'
				}
			},
			axisLabel: {
				textStyle: {
					color: 'rgba(255,255,255,.7)'
				}
			}
		},
		series: seriesData
	};
	chart.setOption(option);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值