性能优化

This commit is contained in:
veveue 2018-09-27 19:35:47 +08:00
parent 72449c7705
commit 4edf5c5e3e
5 changed files with 38 additions and 105 deletions

22
src/components/Echarts/echarts.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -17,13 +17,6 @@ export default class App extends Component {
return false; return false;
} }
componentWillReceiveProps(nextProps) {
if (nextProps.option !== this.props.option) {
// 解决数据改变时页面闪烁的问题
this.refs.chart.injectJavaScript(renderChart(nextProps, false));
}
}
render() { render() {
const messageFn = this.props.onMessage || null; const messageFn = this.props.onMessage || null;
return ( return (
@ -32,7 +25,7 @@ export default class App extends Component {
ref="chart" ref="chart"
scrollEnabled={false} scrollEnabled={false}
scalesPageToFit={false} scalesPageToFit={false}
injectedJavaScript={renderChart(this.props, true)} injectedJavaScript={renderChart(this.props)}
style={{ style={{
height: this.props.height || 400, height: this.props.height || 400,
}} }}

View File

@ -1,21 +1,18 @@
import toString from "../../util/toString"; export default function renderChart(props) {
export default function renderChart(props, isFirst) {
const height = `${props.height || 400}px`; const height = `${props.height || 400}px`;
const width = props.width ? `${props.width}px` : "auto"; const width = props.width ? `${props.width}px` : "auto";
if (isFirst) {
return ` return `
setTimeout(()=>{
document.getElementById('main').style.height = "${height}"; document.getElementById('main').style.height = "${height}";
document.getElementById('main').style.width = "${width}"; document.getElementById('main').style.width = "${width}";
myChart = echarts.init(document.getElementById('main')); var myChart = echarts.init(document.getElementById('main'), null, {renderer: 'svg'});
myChart.setOption(${toString(props.option)}); myChart.on('legendselectchanged',function(params){
`; params = params || {};
} if(typeof window.postMessage === 'function'){
return ` window.postMessage(JSON.stringify(params))
document.getElementById('main').style.height = "${height}"; }
document.getElementById('main').style.width = "${width}"; })
myChart.clear(); myChart.setOption(${JSON.stringify(props.option)});
myChart.resize(); }, 100)
myChart.setOption(${toString(props.option)});
`; `;
} }

File diff suppressed because one or more lines are too long

View File

@ -1,13 +0,0 @@
export default function toString(obj) {
let result = JSON.stringify(obj, function(key, val) {
if (typeof val === 'function') {
return `~--demo--~${val}~--demo--~`;
}
return val;
});
do {
result = result.replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, '').replace(/\\\"/g,"\"");//最后一个replace将release模式中莫名生成的\"转换成"
} while (result.indexOf('~--demo--~') >= 0);
return result;
}