fix the bug when option contains function

This commit is contained in:
somonus 2016-10-27 14:14:35 +08:00
parent d7577f445d
commit b3152edbf2
2 changed files with 10 additions and 1 deletions

View File

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

8
src/util/toString.js Normal file
View File

@ -0,0 +1,8 @@
export default function toString(obj) {
return JSON.stringify(obj, function(key, val) {
if (typeof val === 'function') {
return `~--demo--~${val}~--demo--~`;
}
return val;
}).replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, '');
}