<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="../js/skin1/build/dist/echarts.js" charset="UTF-8"></script> </head> <body> <!-- 爲ECharts準備一個具有大小(寬高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基於準備好的dom,初始化echarts實例 var myChart = ec.init(document.getElementById('main')); // 指定圖表的配置項和數據 var option = { title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data: ['銷量'] }, xAxis: { data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // 使用剛指定的配置項和數據顯示圖表。 myChart.setOption(option); </script>
以上代碼會報以下錯誤:javascript
咱們能夠按以下步驟操做:html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="../js/skin1/build/dist/echarts.js" charset="UTF-8"></script> </head> <body> <!-- 爲ECharts準備一個具有大小(寬高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> require.config({ paths: { echarts: '../js/skin1/build/dist' } } ); require( [ 'echarts', 'echarts/chart/bar', 'echarts/chart/line' ], DrawEChart //異步加載的回調函數繪製圖表 ); function DrawEChart(ec) { // 基於準備好的dom,初始化echarts實例 var myChart = ec.init(document.getElementById('main')); // 指定圖表的配置項和數據 var option = { title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data: ['銷量'] }, xAxis: { data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // 使用剛指定的配置項和數據顯示圖表。 myChart.setOption(option); } </script> </body> </html>