记录一下自己的代码 ,写的很垃圾 ,有好多的方法麻烦指出
如果接口哪个里面是三条数据 就是三条柱状图:
接口数据格式:
代码:最多是四条数据 就这个图最多是四个柱子在一起:
creatchart() {
const chartDom = document.getElementById('theChart');
const myChart = echarts.init(chartDom);
let maxnum = 0;
this.$api.get('wp/ocable/selectOpticalCable').then(res => {
this.echartsData = res.data;
// console.log('===>', res);
for (const item in res.data) {
// console.log('aaaaaaaaaaaaaaaaaaaaaa', item, res.data[item], res.data);
if (res.data[item].length > maxnum) {
maxnum = res.data[item].length;
}
}
const inalldata = [];
const outalldata = [];
for (let i = 0; i < maxnum; i++) {
// 4GE
inalldata[i] = [];
outalldata[i] = [];
}
const newchartName = [];
for (let i = 0; i < 24; i++) {
newchartName.push(' ' + i + '时-' + (i + 1) + '时');
for (let j = 0; j < maxnum; j++) {
inalldata[j].push([i, '', '']); // 输入功率
outalldata[j].push([i, '', '']); // 输出功率
}
}
// console.log('aaaaaaaaaaaaaaa', inalldata, outalldata);
for (const item in this.echartsData) {
// console.log('aaaaaaaaaaaaaaaaaaaaaa', item, res.data[item], res.data);
// this.echartsData[item].length
// console.log('=================>', this.echartsData[item]);
for (let i = 0; i < this.echartsData[item].length; i++) {
if (Number(item)) {
inalldata[i][Number(item)][1] = Number(this.echartsData[item][i].fieldCntentent);
inalldata[i][Number(item)][2] = this.echartsData[item][i].deviceName;
outalldata[i][Number(item)][1] = Number(this.echartsData[item][i].exporCntentent);
outalldata[i][Number(item)][2] = this.echartsData[item][i].deviceName;
}
}
}
console.log('aaaaaaaaaaaaaaa', inalldata, outalldata);
const series = [];
for (let j = 0; j < maxnum; j++) {
series.push({
name: '输入功率',
type: 'bar',
// barGap: '25%',
barWidth: 18,
itemStyle: {
// barBorderRadius:20,
barBorderRadius: 20,
// lenged文本
opacity: 0.7,
color: function (params) {
return new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[
{
offset: 0,
color: '#00F7FD' // 0% 处的颜色
},
{
offset: 1,
color: '#2F14AB' // 100% 处的颜色
}
],
false
);
}
},
label: {
normal: {
show: true,
position: 'inside',
fontSize: 15,
fontWeight: 'bold',
color: '#fff'
}
},
zlevel: 2,
data: inalldata[j] // 输入功率
});
}
for (let j = 0; j < maxnum; j++) {
series.push({
name: '输出功率',
type: 'bar',
xAxisIndex: 1,
// barGap: '40%',
data: outalldata[j],
zlevel: 1, // 层级
barWidth: 18,
label: {
normal: {
show: true,
position: 'inside',
fontSize: 15,
fontWeight: 'bold',
color: '#F74BFA'
}
},
itemStyle: {
normal: {
barBorderRadius: 20,
color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [
{
offset: 0,
color: '#602CAA'
},
{
offset: 1,
color: '#F74BFA'
}
]),
opacity: 0.8
}
}
});
}
if (maxnum % 2 === 0) {
// 偶数
let offset = Math.floor(maxnum / 2) * -1;
// console.log('jishu', Math.floor(maxnum / 2));
for (let i = 0; i < maxnum; i++) {
series.push({
// 红色的那个圈
name: '输入功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
symbolPosition: 'end',
z: 12,
color: '#00F7FD',
zlevel: 2,
data: inalldata[i]
});
series.push({
// 蓝色的那个圈
name: '输出功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
symbolPosition: 'end',
z: 12,
color: '#602CAA',
zlevel: 2,
data: outalldata[i]
});
series.push({
// 红色的那个圈
name: '输入功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
symbolPosition: 'start',
z: 12,
color: '#00F7FD',
zlevel: 2,
data: inalldata[i]
});
series.push({
// 蓝色的那个圈
name: '输出功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset > 0 ? offset * 130 - 65 : offset * 130 + 65}%`, 0],
symbolPosition: 'start',
z: 12,
color: '#F74BFA',
zlevel: 2,
data: outalldata[i]
});
offset++;
if (offset === 0) {
offset++;
}
}
} else {
// 奇数
let offset = Math.floor(maxnum / 2) * -1;
// console.log('jishu', Math.floor(maxnum / 2));
for (let i = 0; i < maxnum; i++) {
series.push({
// 红色的那个圈
name: '输入功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset * 130}%`, 0],
symbolPosition: 'end',
z: 12,
color: '#00F7FD',
zlevel: 2,
data: inalldata[i]
});
series.push({
// 蓝色的那个圈
name: '输出功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset * 130}%`, 0],
symbolPosition: 'end',
z: 12,
color: '#602CAA',
zlevel: 2,
data: outalldata[i]
});
series.push({
// 红色的那个圈
name: '输入功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset * 130}%`, 0],
symbolPosition: 'start',
z: 12,
color: '#221287',
zlevel: 2,
data: inalldata[i]
});
series.push({
// 蓝色的那个圈
name: '输出功率',
type: 'pictorialBar',
symbolSize: [18, 10],
symbolOffset: [`${offset * 130}%`, 0],
symbolPosition: 'start',
z: 12,
color: '#F74BFA',
zlevel: 2,
data: outalldata[i]
});
offset++;
}
}
const option = {
backgroundColor: '#030230',
legend: {
// 类别关闭打开
data: ['输出功率', '输入功率'],
icon: 'rect',
top: 5,
// selectedMode: false, // 关闭点击
right: 10,
textStyle: {
color: '#fff',
fontWeight: 'normal',
fontSize: 12
}
},
// 提示框
tooltip: {
// trigger: 'item'
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: function (param) {
let resultTooltip =
"<div style='background:;border:1px solid rgba(255,255,255,.2);padding:5px 10px;border-radius:4px;'>" + "<div style='text-align:center;'>" + param[0].name + '</div>';
console.log('=====', param);
function compare(property) {
return function (a, b) {
const value1 = a[property];
const value2 = b[property];
return value1 - value2;
};
}
console.log(param.sort(compare('componentIndex')));
const params = param.sort(compare('componentIndex'));
for (let i = 0; i < maxnum; i++) {
if (param[i].seriesName === '输入功率' && params[i].value[1]) {
resultTooltip +=
"<div style='padding-top:5px;'>" +
"<span style='display:inline-block;border-radius:4px;width:30px;height:10px;background-color:red;border: 2px solid #3eb6f5;'></span>" +
"<span style=''> " +
params[i].value[2] +
params[i].seriesName +
': </span>' +
"<span style=''>" +
params[i].value[1] +
'</span><span></span>' +
'</div>';
}
if (param[i + maxnum].seriesName === '输出功率' && params[i + maxnum].value[1]) {
resultTooltip +=
"<div style='padding-top:5px;'>" +
"<span style='display:inline-block;border-radius:4px;width:30px;height:10px;background-color:red;border: 2px solid #3eb6f5;'></span>" +
"<span style=''> " +
params[i + maxnum].value[2] +
params[i + maxnum].seriesName +
': </span>' +
"<span style=''>" +
params[i + maxnum].value[1] +
'</span><span></span>' +
'</div>';
}
}
return resultTooltip;
}
},
grid: {
top: '10%',
left: '5%',
bottom: '10%',
right: '5%',
containLabel: true
},
animation: true,
xAxis: [
{
name: '时间',
axisTick: {
show: false
},
axisLine: {
symbol: ['none', 'arrow'],
onZero: false,
show: true,
lineStyle: {
color: '#fff'
}
},
axisLabel: {
inside: false,
textStyle: {
color: '#fff',
fontWeight: 'normal',
fontSize: 12
}
},
data: newchartName
},
{
type: 'category',
axisLine: {
show: false,
lineStyle: {
color: '#fff'
}
},
axisTick: {
show: false
},
axisLabel: {
show: false
},
splitArea: {
show: false
},
splitLine: {
show: false
},
data: newchartName
}
],
yAxis: [
// {
// show: true,
// name: '当前故障数',
// nameTextStyle: {
// fontSize: 20,
// color: '#fff'
// },
// type: 'value',
// axisLabel: {
// textStyle: {
// color: '#fff'
// }
// },
// splitLine: {
// lineStyle: {
// color: 'rgba(0,228,255,1)'
// }
// },
// axisLine: {
// show: false
// }
// }
{
smooth: true,
nameGap: 20,
type: 'value',
name: '当前故障数108 个',
position: 'left',
axisLine: {
symbol: ['none', 'arrow'],
// onZero: false,
show: true,
lineStyle: {
color: '#fff',
fontSize: 16
},
axisTick: {
inside: false
}
},
axisLabel: {
textStyle: {
show: false,
color: '#fff'
}
},
splitLine: {
show: true,
lineStyle: {
type: 'dashed',
opacity: 0.5,
color: '#999'
}
}
// axisLabel: {
// formatter: '{value} '
// }
}
],
dataZoom: [
{
show: true,
height: 12,
xAxisIndex: [0, 1],
bottom: '8%',
start: 0,
end: 30,
handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
handleSize: '100%',
handleStyle: {
color: '#d3dee5'
},
textStyle: {
color: '#fff'
},
borderColor: '#90979c'
}
// {
// show: false,
// height: 12,
// // xAxisIndex: [1],
// bottom: '8%',
// start: 0,
// end: 50,
// handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
// handleSize: '100%',
// handleStyle: {
// color: '#d3dee5'
// },
// textStyle: {
// color: '#fff'
// },
// borderColor: '#90979c'
// }
// {
// type: 'inside',
// show: true,
// height: 15,
// start: 0,
// end: 35
// }
],
series: series
};
option && myChart.setOption(option);
// const app = {
// currentIndex: -1
// };
// setInterval(function () {
// const dataLen = option.series[0].data.length;
// // 取消之前高亮的图形
// myChart.dispatchAction({
// type: 'downplay',
// seriesIndex: 0,
// dataIndex: app.currentIndex
// });
// app.currentIndex = (app.currentIndex + 1) % dataLen;
// // console.log(app.currentIndex);
// // 高亮当前图形
// myChart.dispatchAction({
// type: 'highlight',
// seriesIndex: 0,
// dataIndex: app.currentIndex
// });
// // 显示 tooltip
// myChart.dispatchAction({
// type: 'showTip',
// seriesIndex: 0,
// dataIndex: app.currentIndex
// });
// }, 10000);
});
}