Vue使用echarts的完整步驟及解決各種報錯
前言:
Echarts,它是一個與框架無關的 JS 圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計IONIC也能用,下次研究。
因為我的習慣,每次新嘗試做一個功能的時候,總要新創(chuàng)建個小項目,做做Demo。
首先看自己電腦是否安裝了Vue,打開終端命令:vue --version,我以前安裝過Vue,但是不知道為何報錯:
vue/cli Error: Cannot find module ‘@vue/cli-shared-utils‘
注意:如果是全局module出錯,就全局更新,如果是項目中module出錯,就刪除(rimraf node_modules)重新安裝 (npm i)
解決方法(更新或者重裝):
npm update -g @vue/cli # 或者 yarn global upgrade --latest @vue/cli
在自己特定的項目文件夾下cmd打開終端
1.vue create echarts
2.默認習慣創(chuàng)建,選擇package.json之后輸入 n 或者 y ,過程簡略。
cd echarts 到該文件夾下,npm run serve顯示項目運行正常:
正式開始嘗試Echarts
建議大家要學會看官網(wǎng)文檔:https://echarts.apache.org/handbook/zh/get-started/
能學習到兩點:
- 在繪圖前我們需要為 ECharts 準備一個定義了高寬的 DOM 容器。
- 通過 echarts.init 方法初始化一個 echarts 實例并通過 setOption 方法生成一個簡單的柱狀圖。
基于這兩句話進行研究:我通常是在About中嘗試進行寫Demo。
Ctrl + C結(jié)束項目。
在項目終端安裝echarts:npm install echarts --save
請注意,2022年后安裝的echarts都是5.X版本,可以在package.json中看到,不知為何,這個和Vue項目不匹配,導致發(fā)生錯誤,知道原因的麻煩在下面留言。
全局引入:在 main.js 中全局引入 echarts
import echarts from "echarts"; Vue.prototype.$echarts = echarts;
在About.Vue中的<template> </template>寫<div id="main" style="width: 600px; height: 400px"></div>
,如上圖,對應1. 在繪圖前我們需要為 ECharts 準備一個定義了高寬的 DOM 容器。
<div id="main" style="width: 600px; height: 400px"></div>
對應 2.通過 echarts.init 方法初始化一個 echarts 實例并通過 setOption 方法生成一個簡單的柱狀圖。這個需要在script中進行操作。
示例一:
drawChart() { // 基于準備好的dom,初始化echarts實例 這個和上面的main對應 let myChart = this.$echarts.init(document.getElementById("main")); // 指定圖表的配置項和數(shù)據(jù) let option = { title: { text: "ECharts 入門示例", }, tooltip: {}, legend: { data: ["銷量"], }, xAxis: { data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"], }, yAxis: {}, series: [ { name: "銷量", type: "bar", data: [5, 20, 36, 10, 10, 20], }, ], }; // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。 myChart.setOption(option); },
然后新建monted(){},否則會出現(xiàn)init沒有定義等等錯誤。
mounted() { this.drawChart(); },
到這一步,其實示例一已經(jīng)完成,但是我們運行發(fā)現(xiàn)圖片無法顯示,只有一處警告錯誤:"export ‘default’ (imported as ‘echarts’) was not found in ‘echarts’
起著懷疑的態(tài)度進行百度,https://blog.csdn.net/Aom_yt/article/details/110947734 ,給出的原因是“可能還不能支持最新版的echarts5.0” 這句話不一定對,麻煩知道原因的在下面進行評論。
解決方法:
- 卸載: npm uninstall echarts
- 重裝echarts: npm install echarts@4.9.0
- 重新運行項目,發(fā)現(xiàn)成功了。
示例二:
我的需求和目標是將https://echarts.apache.org/examples/zh/editor.html?c=bar-race-country&version=5.2.1導入到我的Vue項目中,基于上面1和2原理,上代碼:
<div id="main2" style="width: 1600px; height: 1400px"></div>
drawChart2() { // 基于準備好的dom,初始化echarts實例 這個和上面的main2對應 let myChart = this.$echarts.init(document.getElementById("main2")); // 指定圖表的配置項和數(shù)據(jù) var ROOT_PATH = "https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/examples"; // var chartDom = document.getElementById("main"); // var myChart = echarts.init(chartDom); var option; const updateFrequency = 2000; const dimension = 0; const countryColors = { Australia: "#00008b", Canada: "#f00", China: "#ffde00", Cuba: "#002a8f", Finland: "#003580", France: "#ed2939", Germany: "#000", Iceland: "#003897", India: "#f93", Japan: "#bc002d", "North Korea": "#024fa2", "South Korea": "#000", "New Zealand": "#00247d", Norway: "#ef2b2d", Poland: "#dc143c", Russia: "#d52b1e", Turkey: "#e30a17", "United Kingdom": "#00247d", "United States": "#b22234", }; $.when( $.getJSON("https://cdn.jsdelivr.net/npm/emoji-flags@1.3.0/data.json"), $.getJSON(ROOT_PATH + "/data/asset/data/life-expectancy-table.json") ).done(function (res0, res1) { const flags = res0[0]; const data = res1[0]; const years = []; for (let i = 0; i < data.length; ++i) { if (years.length === 0 || years[years.length - 1] !== data[i][4]) { years.push(data[i][4]); } } function getFlag(countryName) { if (!countryName) { return ""; } return ( flags.find(function (item) { return item.name === countryName; }) || {} ).emoji; } let startIndex = 10; let startYear = years[startIndex]; option = { grid: { top: 10, bottom: 30, left: 150, right: 80, }, xAxis: { max: "dataMax", axisLabel: { formatter: function (n) { return Math.round(n) + ""; }, }, }, dataset: { source: data.slice(1).filter(function (d) { return d[4] === startYear; }), }, yAxis: { type: "category", inverse: true, max: 10, axisLabel: { show: true, fontSize: 14, formatter: function (value) { return value + "{flag|" + getFlag(value) + "}"; }, rich: { flag: { fontSize: 25, padding: 5, }, }, }, animationDuration: 300, animationDurationUpdate: 300, }, series: [ { realtimeSort: true, seriesLayoutBy: "column", type: "bar", itemStyle: { color: function (param) { return countryColors[param.value[3]] || "#5470c6"; }, }, encode: { x: dimension, y: 3, }, label: { show: true, precision: 1, position: "right", valueAnimation: true, fontFamily: "monospace", }, }, ], // Disable init animation. animationDuration: 0, animationDurationUpdate: updateFrequency, animationEasing: "linear", animationEasingUpdate: "linear", graphic: { elements: [ { type: "text", right: 160, bottom: 60, style: { text: startYear, font: "bolder 80px monospace", fill: "rgba(100, 100, 100, 0.25)", }, z: 100, }, ], }, }; // console.log(option); myChart.setOption(option); for (let i = startIndex; i < years.length - 1; ++i) { (function (i) { setTimeout(function () { updateYear(years[i + 1]); }, (i - startIndex) * updateFrequency); })(i); } function updateYear(year) { let source = data.slice(1).filter(function (d) { return d[4] === year; }); option.series[0].data = source; option.graphic.elements[0].style.text = year; myChart.setOption(option); } }); // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。 myChart.setOption(option); },
mounted() { this.drawChart2(); },
不一一解釋了,只要你弄懂了示例一,這個也能運行出來,同時也能舉一反三。
其他:
在學習Echarts時候,發(fā)現(xiàn)這個用途很廣,很多的人都在使用,也延伸出來了很多包,比如
封裝的D3,簡化了代碼。https://github.com/Finedl/Vue-echart-D3
示例:
HighCharts,在相比echarts有更多的風格等等,如何使用請參考:https://www.highcharts.com/blog/download/
總結(jié)
到此這篇關于Vue使用echarts的文章就介紹到這了,更多相關Vue使用echarts內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue使用openlayers實現(xiàn)繪制圓形和多邊形
這篇文章主要為大家詳細介紹了Vue如何使用openlayers實現(xiàn)繪制圓形和多邊形,文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起動手嘗試一下2022-06-06vue使用axios實現(xiàn)文件上傳進度的實時更新詳解
最近在學習axios,然后項目就用到了,所以這篇文章主要給大家介紹了關于vue中利用axios實現(xiàn)文件上傳進度的實時更新的相關資料,文中先對axios進行了簡單的介紹,方法大家理解學習,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12解決element-ui el-input賦值后不能編輯的問題
這篇文章主要介紹了解決element-ui el-input賦值后不能編輯的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02