Browse Source

feat: 数据统计多轴交互调整

master
wally 4 years ago
parent
commit
cdebaf3543
  1. 14
      src/components/statistical/stastistical-chart.vue
  2. 165
      src/config/chart.js
  3. 229
      src/utils/statistical.js

14
src/components/statistical/stastistical-chart.vue

@ -8,12 +8,12 @@
<script setup> <script setup>
import { computed, defineExpose, defineProps, onMounted, ref, watchEffect } from 'vue'; import { computed, defineExpose, defineProps, onMounted, ref, watchEffect } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
import { generateChartOption, generateDefaultYAxis, generateYAxis } from 'utils/statistical';
import * as echarts from 'echarts/core'; import * as echarts from 'echarts/core';
import { DataZoomComponent, GridComponent, LegendComponent, ToolboxComponent, TooltipComponent } from 'echarts/components'; import { DataZoomComponent, GridComponent, LegendComponent, ToolboxComponent, TooltipComponent } from 'echarts/components';
import { LineChart } from 'echarts/charts'; import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features'; import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers'; import { CanvasRenderer } from 'echarts/renderers';
import { generateChartOption } from 'utils/statistical';
echarts.use([ echarts.use([
ToolboxComponent, ToolboxComponent,
@ -36,6 +36,10 @@ let myChart = null;
// //
onMounted(() => { onMounted(() => {
isMounted.value = true; isMounted.value = true;
window.addEventListener('resize', () => {
if (!myChart) return;
myChart.resize();
});
}); });
watchEffect(() => { watchEffect(() => {
@ -52,14 +56,14 @@ function initChart(data) {
chartDom.style.height = `${canvasHeight}px`; chartDom.style.height = `${canvasHeight}px`;
myChart && myChart.dispose(); myChart && myChart.dispose();
myChart = echarts.init(chartDom); myChart = echarts.init(chartDom);
render(data, generateDefaultYAxis()); render(data);
myChart.on('legendselectchanged', event => { myChart.on('legendselectchanged', event => {
render(data, generateYAxis(event.selected)); render(data, event.selected);
}); });
} }
function render(data, yAxis) { function render(data, selected) {
// myChart && myChart.clear(); // myChart && myChart.clear();
if (!data || !data.length) { if (!data || !data.length) {
noData.value = true; noData.value = true;
@ -70,7 +74,7 @@ function render(data, yAxis) {
if (!myChart) { if (!myChart) {
initChart(); initChart();
} }
const option = generateChartOption(data, yAxis); const option = generateChartOption(data, selected);
option && myChart.setOption(option); option && myChart.setOption(option);
} }

165
src/config/chart.js

@ -0,0 +1,165 @@
/* eslint-disable max-len */
export const colors = ['#5470C6', '#91CC75', '#EE6666', '#5470C6', '#91CC75', '#EE6666', '#5470C6', '#91CC75'];
// 默认legend
export const defaultSelectedLegend = {
'钢腐蚀电流(nA)': true,
'铜腐蚀电流(nA)': true,
'铝腐蚀电流(nA)': true,
'锌腐蚀电流(nA)': true,
'SO2(ppb)': false,
'盐分阻抗(Ω)': false,
'环温(℃)': false,
'环湿(RH%)': false,
};
// y轴定义
export const yAxisData = [
{
type: 'value',
name: 'SO2(ppb)',
position: 'left',
show: false,
axisLine: {
show: true,
lineStyle: { color: colors[0] },
},
axisLabel: { formatter: '{value}ppb' },
},
{
type: 'value',
name: '盐分阻抗(Ω)',
show: false,
position: 'left',
offset: 70,
axisLine: {
show: true,
lineStyle: { color: colors[1] },
},
axisLabel: { formatter: '{value}Ω' },
},
{
type: 'value',
name: '环温(℃)',
show: false,
position: 'left',
offset: 135,
axisLine: {
show: true,
lineStyle: { color: colors[2] },
},
axisLabel: { formatter: '{value}°C' },
},
{
type: 'value',
name: '环湿(RH%)',
show: false,
position: 'left',
offset: 200,
axisLine: {
show: true,
lineStyle: { color: colors[3] },
},
axisLabel: { formatter: '{value}RH%' },
},
{
type: 'value',
name: '锌腐蚀电流(nA)',
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[4] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '铜腐蚀电流(nA)',
offset: 100,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[5] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '铝腐蚀电流(nA)',
offset: 200,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[6] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '钢腐蚀电流(nA)',
offset: 300,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[7] },
},
axisLabel: { formatter: '{value}' },
},
];
/**
* 生成默认数据
* @param {Object} data
* @returns {[{data: (number|[]|string|*), name: string, type: string},{data: (number|[]|BufferSource|string|*), name: string, type: string, yAxisIndex: number},{data: ([]|string|*), name: string, type: string, yAxisIndex: number},{data: ([]|string|*), name: string, type: string, yAxisIndex: number},{data: [], name: string, type: string, yAxisIndex: number},null,null,null]}
*/
export function generateDefaultSeries(data) {
return [
{
name: 'SO2(ppb)',
type: 'line',
data: data.so2,
},
{
name: '盐分阻抗(Ω)',
type: 'line',
yAxisIndex: 1,
data: data.salt,
},
{
name: '环温(℃)',
type: 'line',
yAxisIndex: 2,
data: data.environmentTemperature,
},
{
name: '环湿(RH%)',
type: 'line',
yAxisIndex: 3,
data: data.environmentHumidity,
},
{
name: '锌腐蚀电流(nA)',
type: 'line',
yAxisIndex: 4,
data: data.corrosionXIN,
},
{
name: '铜腐蚀电流(nA)',
type: 'line',
yAxisIndex: 5,
data: data.corrosionTONG,
},
{
name: '铝腐蚀电流(nA)',
type: 'line',
yAxisIndex: 6,
data: data.corrosionLV,
},
{
name: '钢腐蚀电流(nA)',
type: 'line',
yAxisIndex: 7,
data: data.corrosionGANG,
},
];
}

229
src/utils/statistical.js

@ -1,7 +1,7 @@
/* eslint-disable max-len,object-curly-newline */ /* eslint-disable max-len,object-curly-newline */
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import max from 'lodash/max';
const colors = ['#5470C6', '#91CC75', '#EE6666', '#5470C6', '#91CC75', '#EE6666', '#5470C6', '#91CC75']; import { colors, defaultSelectedLegend, generateDefaultSeries, yAxisData } from '@/config/chart';
/** /**
* 生成chart所需参数 * 生成chart所需参数
@ -43,125 +43,62 @@ function generateParams(data) {
return result; return result;
} }
const yAxisData = [
{
type: 'value',
name: 'SO2(ppb)',
position: 'left',
axisLine: {
show: true,
lineStyle: { color: colors[0] },
},
axisLabel: { formatter: '{value}ppb' },
},
{
type: 'value',
name: '盐分阻抗(Ω)',
// min: 0,
// max: 250,
position: 'left',
offset: 70,
axisLine: {
show: true,
lineStyle: { color: colors[1] },
},
axisLabel: { formatter: '{value}Ω' },
},
{
type: 'value',
name: '环温(℃)',
// min: 0,
// max: 25,
position: 'left',
offset: 135,
axisLine: {
show: true,
lineStyle: { color: colors[2] },
},
axisLabel: { formatter: '{value}°C' },
},
{
type: 'value',
name: '环湿(RH%)',
// min: 0,
// max: 25,
position: 'left',
offset: 200,
axisLine: {
show: true,
lineStyle: { color: colors[3] },
},
axisLabel: { formatter: '{value}RH%' },
},
{
type: 'value',
name: '锌腐蚀电流(nA)',
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[4] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '铜腐蚀电流(nA)',
offset: 100,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[5] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '铝腐蚀电流(nA)',
offset: 200,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[6] },
},
axisLabel: { formatter: '{value}' },
},
{
type: 'value',
name: '钢腐蚀电流(nA)',
offset: 300,
position: 'right',
axisLine: {
show: true,
lineStyle: { color: colors[7] },
},
axisLabel: { formatter: '{value}' },
},
];
/** /**
* 生产y轴内容 * 生产y轴内容
* @param {Object} selectedLegend * @param {Object} selectedLegend
* @returns {({axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string})[]} * @returns {({axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string})[]}
*/ */
export function generateYAxis(selectedLegend) { export function generateYAxis(selectedLegend) {
console.log(selectedLegend); let leftIndex = 0;
return yAxisData.filter(item => selectedLegend[item.name]); let rightIndex = 0;
yAxisData.forEach(item => {
item.show = selectedLegend[item.name];
if (item.show) {
if (item.position === 'left') {
item.offset = 70 * leftIndex;
leftIndex += 1;
} else {
item.offset = 85 * rightIndex;
rightIndex += 1;
}
}
});
return yAxisData;
} }
/** /**
* 生成初始状态的y轴 * 生成series数据
* @returns {({axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string}|{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string})[]} * @param {Object} data
* @param {Object[]} yAxis
* @returns {({data: ([]|number|string|*), name: string, type: string}|{data: ([]|number|BufferSource|string|*), name: string, type: string, yAxisIndex: number}|{data: ([]|string|*), name: string, type: string, yAxisIndex: number}|{data: ([]|string|*), name: string, type: string, yAxisIndex: number}|{data: [], name: string, type: string, yAxisIndex: number})[]|*[]}
*/ */
export function generateDefaultYAxis() { function generateSeries(data, yAxis) {
const selectedLegend = [ const seriesArr = generateDefaultSeries(data);
{ const showArr = seriesArr.filter(item => yAxis.find(y => y.name === item.name));
'钢腐蚀电流(nA)': true, const hideArr = seriesArr.filter(item => !yAxis.find(y => y.name === item.name));
'铜腐蚀电流(nA)': true, const result = [...showArr, ...hideArr];
'铝腐蚀电流(nA)': true, result.forEach((item, index) => {
'锌腐蚀电流(nA)': true, item.yAxisIndex = index;
}, });
]; return result || [];
return yAxisData.filter(item => selectedLegend.find(selected => selected[item.name])); }
function genereteGrid(yAxis) {
const left = [];
const right = [];
yAxis.forEach(item => {
if (item.show) {
if (item.position === 'left') {
left.push(item.offset || 0);
} else {
right.push(item.offset || 0);
}
}
});
return {
left: max(left) + 45,
right: max(right) + 45,
};
} }
/** /**
@ -171,20 +108,23 @@ export function generateDefaultYAxis() {
* @returns {{yAxis: [{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},null,null,null], xAxis: [{data: *[], axisTick: {alignWithLabel: boolean}, type: string}], color: string[], grid: {right: string}, legend: {data: string[]}, series: [{data: *[], name: string, type: string},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},null,null,null], tooltip: {axisPointer: {type: string}, trigger: string}, toolbox: {feature: {saveAsImage: {show: boolean}, restore: {show: boolean}, dataView: {show: boolean, readOnly: boolean}}}}} * @returns {{yAxis: [{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, offset: number, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},{axisLabel: {formatter: string}, axisLine: {lineStyle: {color: string}, show: boolean}, name: string, position: string, type: string},null,null,null], xAxis: [{data: *[], axisTick: {alignWithLabel: boolean}, type: string}], color: string[], grid: {right: string}, legend: {data: string[]}, series: [{data: *[], name: string, type: string},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},{data: *[], name: string, type: string, yAxisIndex: number},null,null,null], tooltip: {axisPointer: {type: string}, trigger: string}, toolbox: {feature: {saveAsImage: {show: boolean}, restore: {show: boolean}, dataView: {show: boolean, readOnly: boolean}}}}}
*/ */
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export function generateChartOption(rawData, yAxis) { export function generateChartOption(rawData, selected = defaultSelectedLegend) {
console.log(yAxis);
const data = generateParams(rawData); const data = generateParams(rawData);
const yAxis = generateYAxis(selected);
const series = generateSeries(data, yAxis);
const grid = genereteGrid(yAxis);
const option = { const option = {
color: colors, color: colors,
tooltip: { tooltip: {
trigger: 'axis', trigger: 'axis',
axisPointer: { type: 'cross' }, axisPointer: {
}, type: 'cross',
grid: { snap: true,
right: '30%', },
left: '20%',
}, },
grid,
legend: { legend: {
selected,
data: ['SO2(ppb)', '盐分阻抗(Ω)', '环温(℃)', '环湿(RH%)', '锌腐蚀电流(nA)', '铜腐蚀电流(nA)', '铝腐蚀电流(nA)', '钢腐蚀电流(nA)'], data: ['SO2(ppb)', '盐分阻抗(Ω)', '环温(℃)', '环湿(RH%)', '锌腐蚀电流(nA)', '铜腐蚀电流(nA)', '铝腐蚀电流(nA)', '钢腐蚀电流(nA)'],
}, },
dataZoom: [{ type: 'inside' }, { type: 'slider' }], dataZoom: [{ type: 'inside' }, { type: 'slider' }],
@ -195,57 +135,8 @@ export function generateChartOption(rawData, yAxis) {
data: data.time, data: data.time,
}, },
], ],
yAxis: yAxisData, yAxis,
series: [ series,
{
name: 'SO2(ppb)',
type: 'line',
data: data.so2,
},
{
name: '盐分阻抗(Ω)',
type: 'line',
yAxisIndex: 1,
data: data.salt,
},
{
name: '环温(℃)',
type: 'line',
yAxisIndex: 2,
data: data.environmentTemperature,
},
{
name: '环湿(RH%)',
type: 'line',
yAxisIndex: 3,
data: data.environmentHumidity,
},
{
name: '锌腐蚀电流(nA)',
type: 'line',
yAxisIndex: 4,
data: data.corrosionXIN,
},
{
name: '铜腐蚀电流(nA)',
type: 'line',
yAxisIndex: 5,
data: data.corrosionTONG,
},
{
name: '铝腐蚀电流(nA)',
type: 'line',
yAxisIndex: 6,
data: data.corrosionLV,
},
{
name: '钢腐蚀电流(nA)',
type: 'line',
yAxisIndex: 7,
data: data.corrosionGANG,
},
],
}; };
return option; return option;
} }

Loading…
Cancel
Save