Vue使用Echarts實現(xiàn)數(shù)據(jù)可視化的方法詳解
一,Echarts
一個基于 JavaScript 的開源可視化圖表庫
Echarts官網(wǎng)
https://echarts.apache.org/zh/index.html
1.1 獲取ECharts
(1)從 npm 獲?。椖揩@取)
npm install echarts --save
(2)從 CDN 獲取
推薦從 jsDelivr 引用 echarts。
(3)從 GitHub 獲取
apache/echarts 項目的 release 頁面可以找到各個版本的鏈接。點擊下載頁面下方 Assets 中的 Source code,解壓后 dist 目錄下的 echarts.js 即為包含完整 ECharts 功能的文件。
1.2 引入 ECharts
import * as echarts from 'echarts'; // 基于準備好的dom,初始化echarts實例 var myChart = echarts.init(document.getElementById('main')); // 繪制圖表 myChart.setOption({ title: { text: 'ECharts 入門示例' }, tooltip: {}, xAxis: { data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'] }, yAxis: {}, series: [ { name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] } ] });
二,Vue使用Echarts
2.1 Vue環(huán)境
ES6、vue、vuex、vue-router、vue-cli、axios、element-ui
Node >= 10
2.2 main.js引入Echarts
// 引入Echarts import echarts from 'echarts' Vue.prototype.$echarts = echarts
2.3 使用模板
<template> <div id="myChart" :style="{width: '100%', height: '1000px'}" /> </template> <script> export default { mounted() { this.drawLine() }, methods: { drawLine() { // 基于準備好的dom,初始化echarts實例 const myChart = this.$echarts.init(document.getElementById('myChart')) myChart.setOption({ //官網(wǎng)實例代碼,如下圖 }) } } } </script>
2.4實例
2.4.1柱狀圖(折線圖變換)
<template> <div id="myChart" :style="{width: '100%', height: '1000px'}" /> </template> <script> export default { mounted() { this.drawLine() }, methods: { drawLine() { // 基于準備好的dom,初始化echarts實例 const myChart = this.$echarts.init(document.getElementById('myChart')) myChart.setOption({ title: { text: 'Rainfall vs Evaporation', subtext: 'Fake Data' }, tooltip: { trigger: 'axis' }, legend: { data: ['Rainfall', 'Evaporation'] }, toolbox: { show: true, feature: { dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['line', 'bar'] }, restore: { show: true }, saveAsImage: { show: true } } }, calculable: true, xAxis: [ { type: 'category', // prettier-ignore data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] } ], yAxis: [ { type: 'value' } ], series: [ { name: 'Rainfall', type: 'bar', data: [ 2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3 ], markPoint: { data: [ { type: 'max', name: 'Max' }, { type: 'min', name: 'Min' } ] }, markLine: { data: [{ type: 'average', name: 'Avg' }] } }, { name: 'Evaporation', type: 'bar', data: [ 2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3 ], markPoint: { data: [ { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 }, { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 } ] }, markLine: { data: [{ type: 'average', name: 'Avg' }] } } ] }) } } } </script>
2.4.2極坐標柱狀圖標簽
<template> <div id="myChart" :style="{width: '100%', height: '1000px'}" /> </template> <script> export default { mounted() { this.drawLine() }, methods: { drawLine() { // 基于準備好的dom,初始化echarts實例 const myChart = this.$echarts.init(document.getElementById('myChart')) myChart.setOption({ title: [ { text: 'Radial Polar Bar Label Position (middle)' } ], polar: { radius: [30, '80%'] }, radiusAxis: { max: 4 }, angleAxis: { type: 'category', data: ['a', 'b', 'c', 'd'], startAngle: 75 }, tooltip: {}, series: { type: 'bar', data: [2, 1.2, 2.4, 3.6], coordinateSystem: 'polar', label: { show: true, position: 'middle', formatter: ': {c}' } }, backgroundColor: '#fff', animation: false }) } } } </script>
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
vue el-select綁定對象時,回顯內(nèi)容不正確,始終是最后一項的解決
這篇文章主要介紹了vue el-select綁定對象時,回顯內(nèi)容不正確,始終是最后一項的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07vue點擊Dashboard不同內(nèi)容 跳轉到同一表格的實例
這篇文章主要介紹了vue點擊Dashboard不同內(nèi)容 跳轉到同一表格的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法
這篇文章主要給大家介紹了關于vue-router二級導航切換路由及高亮顯示的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-07-07Vite3 Svelte3構建Web應用報錯process is not def
這篇文章主要介紹了Vite3 Svelte3構建Web應用報錯:'process is not defined'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06