可以正确实现的代码:
import React, { memo, useRef, useEffect, useState } from 'react';
import { Modal, Row, Col } from 'antd';
import * as echarts from 'echarts';
import './style.less'
function ModalComponent(props) {
const {isModalVisible, setModal, appCodeDetail} = props;
const complexContainer = useRef();
const correlationContainer = useRef();
const [complexInstance, setComplexInstance] = useState(null);
const [correlationInstance, setCorrelationInstance] = useState(null);
useEffect(() => {
if (complexContainer.current && correlationContainer.current && !complexInstance && !correlationInstance) {
const complexEchart = echarts.init(complexContainer.current);
const correlationEchart = echarts.init(correlationContainer.current);
setComplexInstance(complexEchart);
setCorrelationInstance(correlationEchart);
const complexOption = {
tooltip: {
trigger: 'axis'
},
legend: {
data: ['complext', 'coveragelines', 'dependency', 'interface'],
bottom: '3%',
},
// grid: {
// left: '3%',
// right: '4%',
// bottom: '3%',
// containLabel: true
// },
xAxis: {
type: 'category',
boundaryGap: false,
data: appCodeDetail.complexityDatas.xList
},
yAxis: {
type: 'value',
},
series: appCodeDetail.complexityDatas.yList
};
const correlationOption = {
tooltip: {
trigger: 'axis'
},
xAxis: {
type: 'category',
boundaryGap: false,
data: appCodeDetail.correlationDatas.xList
},
yAxis: {
type: 'value',
},
series: appCodeDetail.correlationDatas.yList
};
complexEchart.setOption(complexOption);
correlationEchart.setOption(correlationOption);
window.addEventListener("resize", function () {
complexEchart.resize();
correlationEchart.resize();
});
}
}, [props])
return (
<div>
<Modal
title="<AppCode度量详情>"
width={1200}
visible={isModalVisible}
// onOk={handleOk}
onCancel={() => {
setModal();
}}
footer={null}
>
<div>
<Row>
<Col span={24} className='title'>{"复杂度(<评分>)"}</Col>
</Row>
<Row className='board' gutter={[24,16]}>
<Col span={8}>
<span>代码行:</span>
</Col>
<Col span={8}>
<span>圈复杂度:</span>
</Col>
<Col span={8}>
<span>有效方法行:</span>
</Col>
<Col span={8}>
<span>强弱依赖:</span>
</Col>
<Col span={8}>
<span>接口调用:</span>
</Col>
</Row>
<Row>
<div ref={complexContainer} style={{width: '100%', height: 400}}></div>
</Row>
<Row>
<Col span={24} className='title'>{"相关度(<评分>)"}</Col>
</Row>
<Row className='board' gutter={[24,16]}>
<Col span={24}>
<span>链路覆盖情况:</span>
</Col>
<Col span={24}>
<span>项目覆盖情况:</span>
</Col>
</Row>
<Row>
<div ref={correlationContainer} style={{width: '100%', height: 400}}></div>
</Row>
</div>
</Modal>
</div>
)
}
export default memo(ModalComponent);
- 容器的初始化对于函数式组件基本都是放在useEffect这个钩子函数中对echarts实例进行初始化。但是初始化要注意如果容器是放在Modal中,初次渲染的时候容器还没渲染,ref.current还为undefined,所以得加if判断
- 容器必须得加高度(height),不然图显示不出来,这点非常重要,网上很多没说明这点的,都会导致图出不来 另外一个demo代码:
import React, { memo, useRef, useEffect, useState } from 'react';
// import { Modal, Row, Col } from 'antd';
import * as echarts from 'echarts';
// import './style.less'
// import { useEffect } from 'react';
function RefComponent(props) {
const {isModalVisible, setModal, appCodeDetail} = props;
const container = useRef();
const [cnt, setCnt] = useState(0);
useEffect(() => {
console.log(container.current, 'current');
var myChart = echarts.init(container.current, 'default');
const option = {
title: {
text: 'Stacked Line'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'Email',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Union Ads',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ads',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'Direct',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'Search Engine',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
};
// myChart.setOption(option);
myChart.setOption(option, true);
// window.addEventListener("resize", function () {
// complexityEchart.resize();
// });
// setModal(true);
// setCnt(cnt + 1);
}, [props])
return (
<div ref={container} style={{height: 400}}></div>
// <div id="chart-container"></div>
)
}
export default RefComponent;