在vue項(xiàng)目中引用Antv G2,以餅圖為例講解
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~
npm install @antv/g2 --save
template內(nèi)容:
<template> <div id="pieChart"></div> </template>
js部分:
//引入G2組件 import G2 from "@antv/g2"; export default { name:"", //數(shù)據(jù)部分 data(){ return{ sourceData: [],//聲明一個(gè)數(shù)組 chart: {}, //全局定義chart對(duì)象 id: Math.random().toString(36).substr(2), //動(dòng)態(tài)生成ID,便于多次引用 } }, //初始加載 mounted() { this.initComponent(); }, methods: { //初始化獲取數(shù)據(jù) initStrateGoal() { debugger; let _this = this; _this.$http .$get("后臺(tái)接口地址") .then(function(response) { if (_this.$util.isBlank(response.data)) { return; } _this.sourceData = response.data; //調(diào)用繪圖方法 _this.getDrawing(_this.sourceData); }) .catch(function(error) { _this.$message.error(_this, error); }); }, //繪制圖形 getDrawing(sourceData) { let _this = this; // Step 1: 創(chuàng)建 Chart 對(duì)象 _this.chart = new G2.Chart({ container: _this.id, forceFit: true, height: 255, padding: [30, 0, 35, 0], animate: false // margin: [0, 500] }); let sumCount = 0; for (let i in sourceData) { sumCount = sumCount + Number(sourceData[i].count); } // Step 2: 載入數(shù)據(jù)源 _this.chart.source(sourceData, { percent: { formatter: function formatter(val) { val = val + "%"; return val; } } }); _this.chart.coord("theta", { radius: 0.75, innerRadius: 0.6 }); //消息提示 _this.chart.tooltip({ showTitle: false, itemTpl: '<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>' }); // 輔助文本 _this.chart.guide().html({ position: ["50%", "50%"], html: '<div style="color:#8c8c8c;font-size: 10px;text-align: center;width: 6em;">目標(biāo)總數(shù)<br><span style="color:#8c8c8c;font-size:14px">' + sumCount + "</span></div>", alignX: "middle", alignY: "middle" }); // Step 3:創(chuàng)建圖形語(yǔ)法,繪制餅圖 var interval = _this.chart .intervalStack() .position("percent") .color("item") .label("percent", { formatter: function formatter(val, item) { return item.point.item + ": " + val; } }) .tooltip("item*percent", function(item, percent) { //數(shù)據(jù)顯示為百分比形式 percent = percent + "%"; return { name: item, value: percent }; }) .style({ lineWidth: 1, stroke: "#fff" }); // Step 4:最后一步渲染至畫(huà)布 _this.chart.render(); //初始加載圖片后默認(rèn)顯示第一個(gè)條目突起,點(diǎn)擊后進(jìn)行變更 interval.setSelected(sourceData[0]); }, //因?yàn)楦讣?jí)頁(yè)面用的事Tab調(diào)用,會(huì)有顯示不全的現(xiàn)象發(fā)生,所以銷(xiāo)毀所有對(duì)象后重新加載 reloadDrawing() { //銷(xiāo)毀畫(huà)布對(duì)象 this.chart.destroy(); //重新調(diào)用數(shù)據(jù)進(jìn)行加載 this.initStrateGoal(); } } }
*父級(jí)頁(yè)面時(shí)tab調(diào)用,并且是一個(gè)頁(yè)面多次引用,有兩個(gè)坑
1.tab點(diǎn)擊時(shí),有的頁(yè)面顯示不全,樣式也有問(wèn)題,需要銷(xiāo)毀重構(gòu)(只是我的個(gè)人方案,有其他方案的話(huà)可以推薦)
2.一個(gè)頁(yè)面有多個(gè)圖表,id是不能重復(fù)的,必須動(dòng)態(tài)生成
補(bǔ)充知識(shí):vue+antv與數(shù)據(jù)庫(kù)交互實(shí)現(xiàn)數(shù)據(jù)可視化圖表
一、安裝antv
npm install @antv/g2
二、在官網(wǎng)選擇自己需要的圖表
https://g2.antv.vision/zh/examples/gallery
這里以這個(gè)圖為例
右側(cè)就是實(shí)現(xiàn)這個(gè)圖的代碼,在這里加上.color(“type”)即可根據(jù)字段名顯示不同的顏色
這里數(shù)據(jù)的字段和值可以按需更改(更改字段名稱(chēng)的話(huà)要把下面相關(guān)的字段名全部替換)
三、整合vue antv
在vue中引入antv
import { Chart } from "@antv/g2";
指定一個(gè)容器來(lái)放圖表
<template> <div id="roomTypeCheckIn"></div> </template>
替換默認(rèn)的data數(shù)據(jù)
data() { return { mydata: [ { roomTypeName: "單人間", checkInValue: 654, checkInPercent: 0.02 }, { roomTypeName: "雙人間", checkInValue: 654, checkInPercent: 0.02 }, { roomTypeName: "鐘點(diǎn)房", checkInValue: 4400, checkInPercent: 0.2 }, { roomTypeName: "海景房", checkInValue: 5300, checkInPercent: 0.24 }, { roomTypeName: "主題房", checkInValue: 6200, checkInPercent: 0.28 }, { roomTypeName: "家庭房", checkInValue: 3300, checkInPercent: 0.14 }, { roomTypeName: "總統(tǒng)房", checkInValue: 1500, checkInPercent: 0.06 } ] }; },
把繪圖代碼復(fù)制進(jìn)來(lái)
此處需要把默認(rèn)的container:‘container' 修改為自己指定的容器id,渲染數(shù)據(jù)時(shí),將data修改為this.xxx(自己定義的數(shù)據(jù)名稱(chēng)),不同的圖修改的地方可能會(huì)不同
methods: { initComponent() { const chart = new Chart({ container: "roomTypeCheckIn", autoFit: true, height: 500, padding: [50, 20, 50, 20] }); chart.data(this.mydata); chart.scale("checkInValue", { alias: "銷(xiāo)售額" }); chart.axis("roomTypeName", { tickLine: { alignTick: false } }); chart.axis("checkInValue", false); chart.tooltip({ showMarkers: false }); chart .interval() .position("roomTypeName*checkInValue") .color("roomTypeName"); chart.interaction("element-active"); // 添加文本標(biāo)注 this.mydata.forEach(item => { chart .annotation() .text({ position: [item.roomTypeName, item.checkInValue], content: item.checkInValue, style: { textAlign: "center" }, offsetY: -30 }) .text({ position: [item.roomTypeName, item.checkInValue], content: (item.checkInPercent * 100).toFixed(0) + "%", style: { textAlign: "center" }, offsetY: -12 }); }); chart.render(); } }
在mounted函數(shù)中調(diào)用繪圖方法
mounted() { this.initComponent(); },
啟動(dòng)項(xiàng)目即可看到最終效果
三、與數(shù)據(jù)庫(kù)交互
添加一個(gè)獲取數(shù)據(jù)的方法(按自己的接口進(jìn)行相應(yīng)的替換)
getData() { roomTypeApi.getRoomTypeStatistics().then(res => { this.mydata = res.data.data }) },
在created函數(shù)中調(diào)用獲取數(shù)據(jù)的函數(shù)
created() { this.getData() },
在watch函數(shù)中監(jiān)聽(tīng)數(shù)據(jù),數(shù)據(jù)發(fā)生變化時(shí)重新渲染圖表
watch: { mydata(b,a) { this.chart.changeData(b) this.chart.render() } },
最后得到的圖表數(shù)據(jù)就是數(shù)據(jù)庫(kù)中的數(shù)據(jù)了
以上這篇在vue項(xiàng)目中引用Antv G2,以餅圖為例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于element-ui的隱藏組件el-scrollbar的使用
這篇文章主要介紹了關(guān)于element-ui的隱藏組件el-scrollbar的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05解決vue組件props傳值對(duì)象獲取不到的問(wèn)題
這篇文章主要介紹了vue組件props傳值,對(duì)象獲取不到的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06vue 計(jì)算屬性和偵聽(tīng)器的使用小結(jié)
這篇文章主要介紹了vue 計(jì)算屬性和偵聽(tīng)器的使用小結(jié),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01圖文詳解如何在vue3+vite項(xiàng)目中使用svg
SVG指可伸縮矢量圖形,用來(lái)定義用于網(wǎng)絡(luò)的基于矢量的圖形,下面這篇文章主要給大家介紹了關(guān)于如何在vue3+vite項(xiàng)目中使用svg的相關(guān)資料,需要的朋友可以參考下2021-11-11使用vue-router設(shè)置每個(gè)頁(yè)面的title方法
下面小編就為大家分享一篇使用vue-router設(shè)置每個(gè)頁(yè)面的title方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue?請(qǐng)求后端數(shù)據(jù)的示例代碼
在vue中,我們?nèi)绾瓮ㄟ^(guò)請(qǐng)求接口來(lái)訪(fǎng)問(wèn)后端的數(shù)據(jù)呢?在這里簡(jiǎn)單總結(jié)了一個(gè)小示例,對(duì)vue請(qǐng)求后端數(shù)據(jù)實(shí)例代碼感興趣的朋友一起看看吧2022-09-09