Commit 9396e391 authored by 陈龙's avatar 陈龙

feat: 优化图表代码

parent cc9c46c4
...@@ -14,7 +14,8 @@ const SimgleChart = memo((props) => { ...@@ -14,7 +14,8 @@ const SimgleChart = memo((props) => {
showGridLine = true, showGridLine = true,
deviceAlarmSchemes, deviceAlarmSchemes,
chartType, chartType,
justLine justLine,
showBoxOption
} = props; } = props;
const chartRef = useRef(); const chartRef = useRef();
...@@ -27,7 +28,8 @@ const SimgleChart = memo((props) => { ...@@ -27,7 +28,8 @@ const SimgleChart = memo((props) => {
showMarkLine: true, showMarkLine: true,
showPoint: true, showPoint: true,
chartType, chartType,
justLine justLine,
showBoxOption
}; };
return optionGenerator(dataSource, null, contrast, contrastOption, smooth, config); return optionGenerator(dataSource, null, contrast, contrastOption, smooth, config);
}, [dataSource, smooth, curveCenter]); }, [dataSource, smooth, curveCenter]);
......
...@@ -256,6 +256,7 @@ const HistoryView = (props) => { ...@@ -256,6 +256,7 @@ const HistoryView = (props) => {
const [chartDataSource, setChartDataSource] = useState([]); const [chartDataSource, setChartDataSource] = useState([]);
const [chartType, setChartType] = useState('lineChart'); const [chartType, setChartType] = useState('lineChart');
const [showBoxOption, setShowBoxOption] = useState(true);
// 选择的时间范围值 // 选择的时间范围值
const dateRange = useMemo(() => { const dateRange = useMemo(() => {
if (timeValue === 'customer') { if (timeValue === 'customer') {
...@@ -357,9 +358,13 @@ const HistoryView = (props) => { ...@@ -357,9 +358,13 @@ const HistoryView = (props) => {
if (e.target.value === 'contrast') { if (e.target.value === 'contrast') {
// 同期对比 // 同期对比
onContrastChange(contrastOption); onContrastChange(contrastOption);
setShowBoxOption(false);
setChartType('lineChart');
onCheckboxChange({target: {value: false}}, 'chartType')
} else { } else {
// 自定义 // 自定义
// 不需要处理 // 不需要处理
setShowBoxOption(true)
} }
}; };
...@@ -475,7 +480,7 @@ const HistoryView = (props) => { ...@@ -475,7 +480,7 @@ const HistoryView = (props) => {
return ( return (
<div className={classNames(`${prefixCls}-cover`)} style={isChart && isSingle ? {width: '100%'} : {}}> <div className={classNames(`${prefixCls}-cover`)} style={isChart && isSingle ? {width: '100%'} : {}}>
{ {
isChart && isSingle ? <> isChart && isSingle && showBoxOption ? <>
<div className={classNames(`${prefixCls}-label`)}>曲线形态</div> <div className={classNames(`${prefixCls}-label`)}>曲线形态</div>
<Radio.Group value={chartType} style={{marginRight: 16}} onChange={(e) => { <Radio.Group value={chartType} style={{marginRight: 16}} onChange={(e) => {
let _value = e.target.value; let _value = e.target.value;
...@@ -770,6 +775,7 @@ const HistoryView = (props) => { ...@@ -770,6 +775,7 @@ const HistoryView = (props) => {
/> />
) : ( ) : (
<SimgleChart <SimgleChart
showBoxOption={showBoxOption}
curveCenter={curveCenter} curveCenter={curveCenter}
showGridLine={chartGrid} showGridLine={chartGrid}
prefixCls={prefixCls} prefixCls={prefixCls}
......
...@@ -41,7 +41,8 @@ const nameFormatter = (data, contrast, contrastOption, nameWithSensor) => { ...@@ -41,7 +41,8 @@ const nameFormatter = (data, contrast, contrastOption, nameWithSensor) => {
*/ */
const dataAccessor = (data, contrast, contrastOption) => { const dataAccessor = (data, contrast, contrastOption) => {
const {dataModel} = data; const {dataModel} = data;
const formatStr = contrastOption === 'day' ? '2020-01-01 HH:mm:00' : '2020-01-DD HH:mm:00'; let _currentYear = moment().format('YYYY');
const formatStr = contrastOption === 'day' ? `${_currentYear}-01-01 HH:mm:00` : `${_currentYear}-01-DD HH:mm:00`;
return dataModel.filter(item => item.sensorName !== '是否在线').map((item) => { return dataModel.filter(item => item.sensorName !== '是否在线').map((item) => {
const time = contrast ? moment(item.pt).format(formatStr) : item.pt; const time = contrast ? moment(item.pt).format(formatStr) : item.pt;
return [moment(time).valueOf(), item.pv]; return [moment(time).valueOf(), item.pv];
...@@ -258,6 +259,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -258,6 +259,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
const deviceAlarmSchemes = _.get(config, 'deviceAlarmSchemes', []); const deviceAlarmSchemes = _.get(config, 'deviceAlarmSchemes', []);
const chartType = _.get(config, 'chartType', null); const chartType = _.get(config, 'chartType', null);
const justLine = _.get(config, 'justLine', false); const justLine = _.get(config, 'justLine', false);
const showBoxOption = _.get(config, 'showBoxOption', false);
// 自定义属性 // 自定义属性
const restOption = _.pick(cusOption, ['title', 'legend']); const restOption = _.pick(cusOption, ['title', 'legend']);
...@@ -331,6 +333,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -331,6 +333,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
}; };
const seriesTemplate = (param, unit) => { const seriesTemplate = (param, unit) => {
if (!param) return ''; if (!param) return '';
console.log(param);
const {value, encode} = param; const {value, encode} = param;
// const val = value[encode.y[0]]; // const val = value[encode.y[0]];
const _unit = unit || 'Mpa'; const _unit = unit || 'Mpa';
...@@ -340,7 +343,11 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -340,7 +343,11 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
<span style="color:${color};margin: 0 5px 0 auto;">${value?.toFixed(3) ?? '-'}</span> <span style="color:${color};margin: 0 5px 0 auto;">${value?.toFixed(3) ?? '-'}</span>
<span style="font-size: 12px;">${_unit}</span> <span style="font-size: 12px;">${_unit}</span>
</div>`; </div>`;
return ` return param.componentSubType !== 'candlestick' ? `<div style="display: flex; align-items: center;">
<span>${param.seriesName}</span><span style="display:inline-block;">:</span>
<span style="color: ${COLOR.AVG};margin: 0 5px 0 auto;">${value[1] ?? '-'}</span>
<span style="font-size: 12px;">${_unit}</span>
</div>` : `
<div style="display: flex; align-items: center;"> <div style="display: flex; align-items: center;">
<span>首值</span><span style="display:inline-block;">:</span> <span>首值</span><span style="display:inline-block;">:</span>
<span style="color: ${COLOR.AVG};margin: 0 5px 0 auto;">${value[1] ?? '-'}</span> <span style="color: ${COLOR.AVG};margin: 0 5px 0 auto;">${value[1] ?? '-'}</span>
...@@ -366,6 +373,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -366,6 +373,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
const tooltipAccessor = (unit) => { const tooltipAccessor = (unit) => {
return { return {
formatter: function (params, ticket, callback) { formatter: function (params, ticket, callback) {
debugger
let tooltipHeader = ''; let tooltipHeader = '';
let tooltipContent = ''; let tooltipContent = '';
if (isArray(params)) { if (isArray(params)) {
...@@ -377,7 +385,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -377,7 +385,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
tooltipHeader = headTemplate(params); tooltipHeader = headTemplate(params);
tooltipContent += seriesTemplate(params, unit); tooltipContent += seriesTemplate(params, unit);
} }
debugger
return ` return `
<div> <div>
${tooltipHeader} ${tooltipHeader}
...@@ -463,7 +471,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -463,7 +471,7 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
// } // }
}; };
// 增加箱线图的逻辑,单曲线才存在 // 增加箱线图的逻辑,单曲线才存在
if (!justLine && chartType) { if (!justLine && chartType && showBoxOption) {
if (chartType === 'boxChart') { if (chartType === 'boxChart') {
const otherData = dataSource?.[0]?.dataModel.map(item => { const otherData = dataSource?.[0]?.dataModel.map(item => {
const {firstPV, lastPV, maxPV, minPV, pt} = item; const {firstPV, lastPV, maxPV, minPV, pt} = item;
...@@ -497,17 +505,22 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -497,17 +505,22 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
} else { } else {
let _maxData = []; let _maxData = [];
let _minData = []; let _minData = [];
const formatStr = contrastOption === 'day' ? '2020-01-01 HH:mm:00' : '2020-01-DD HH:mm:00';// 用来做同期对比,把日期拉到同一区间 let _currentYear = moment().format('YYYY');
const formatStr = contrastOption === 'day' ? `${_currentYear}-01-01 HH:mm:00` : `${_currentYear}-01-DD HH:mm:00`;// 用来做同期对比,把日期拉到同一区间
let _maxValues = [];
dataSource?.[0]?.dataModel.forEach(item => { dataSource?.[0]?.dataModel.forEach(item => {
const {firstPV, lastPV, maxPV, minPV, pt} = item; const {firstPV, lastPV, maxPV, minPV, pt} = item;
_maxValues.push(maxPV);
let time = contrast ? moment(pt).format(formatStr) : pt; let time = contrast ? moment(pt).format(formatStr) : pt;
_maxData.push([moment(time).valueOf(), maxPV - minPV]); _maxData.push([moment(time).valueOf(), (maxPV - minPV).toFixed(2)]);
_minData.push([moment(time).valueOf(), minPV]); _minData.push([moment(time).valueOf(), minPV]);
}); //当存在othersData的时候,只是单曲线 }); //当存在othersData的时候,只是单曲线
// xAxis = {type: 'category', data: series[0].data.map(item => moment(item[0]).format('YYYY-MM-DD HH:mm:ss'))}; // xAxis = {type: 'category', data: series[0].data.map(item => moment(item[0]).format('YYYY-MM-DD HH:mm:ss'))};
xAxis = {type: 'time'}; xAxis = {type: 'time'};
decorateAxisGridLine(xAxis, showGridLine); decorateAxisGridLine(xAxis, showGridLine);
let _unit = ''
series = series.map(item => { series = series.map(item => {
_unit = item.unit;
let _item = {...item, symbol: 'none'}; let _item = {...item, symbol: 'none'};
/* _item.data = _item?.data?.map(d => { /* _item.data = _item?.data?.map(d => {
return d[1] || null return d[1] || null
...@@ -533,6 +546,30 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth ...@@ -533,6 +546,30 @@ const optionGenerator = (dataSource, cusOption, contrast, contrastOption, smooth
symbol: 'none' symbol: 'none'
}); });
}) })
tooltip = {
formatter: (e) => {
return `<div>
${headTemplate(e[0])}
<div>
<div style="display: flex; align-items: center;">
<span>${e[0].seriesName}</span><span style="display:inline-block;">:</span>
<span style="color: ${COLOR.NORMAL};margin: 0 5px 0 auto;">${e[0]?.value?.[1] ?? '-'}</span>
<span style="font-size: 12px;">${_unit}</span>
</div>
<div style="display: flex; align-items: center;">
<span>最小值</span><span style="display:inline-block;">:</span>
<span style="color: ${COLOR.AVG};margin: 0 5px 0 auto;">${e[1]?.value?.[1] ?? '-'}</span>
<span style="font-size: 12px;">${_unit}</span>
</div>
<div style="display: flex; align-items: center;">
<span>最大值</span><span style="display:inline-block;">:</span>
<span style="color: ${COLOR.AVG};margin: 0 5px 0 auto;">${_maxValues[e[2].dataIndex] ?? '-'}</span>
<span style="font-size: 12px;">${_unit}</span>
</div>
</div>
</div>`
}
}
} }
} }
restOption.dataZoom = [ restOption.dataZoom = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment