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);
}
Echarts实现折线图效果
于 2023-07-07 15:28:06 首次发布