Skip to content

Commit b3152ed

Browse files
committed
fix the bug when option contains function
1 parent d7577f4 commit b3152ed

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/components/Echarts/renderChart.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import echarts from './echarts.min';
2+
import toString from '../../util/toString';
23

34
export default function renderChart(option) {
45
return `
@@ -30,7 +31,7 @@ export default function renderChart(option) {
3031
// 基于准备好的dom,初始化echarts实例
3132
var myChart = echarts.init(document.getElementById('main'));
3233
// 使用刚指定的配置项和数据显示图表。
33-
myChart.setOption(${JSON.stringify(option)});
34+
myChart.setOption(${toString(option)});
3435
</script>
3536
<body>
3637
<html>

src/util/toString.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function toString(obj) {
2+
return JSON.stringify(obj, function(key, val) {
3+
if (typeof val === 'function') {
4+
return `~--demo--~${val}~--demo--~`;
5+
}
6+
return val;
7+
}).replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, '');
8+
}

0 commit comments

Comments
 (0)