vue2中使用AntV?以g2plot為實例
使用AntV以g2plot為例
效果

代碼
nmp 導(dǎo)入:
npm install @antv/g2plot
代碼:
<template> ?? ?<div id="app"> ?? ??? ?<div id="container"></div> ?? ?</div> </template>
<script>
?? ?import {
?? ??? ?Bar
?? ?} from '@antv/g2plot';
?? ?export default {
?? ??? ?name: 'App',
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?sj: [{
?? ??? ??? ??? ??? ??? ?year: '1951 年',
?? ??? ??? ??? ??? ??? ?value: 38
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?year: '1952 年',
?? ??? ??? ??? ??? ??? ?value: 52
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?year: '1956 年',
?? ??? ??? ??? ??? ??? ?value: 61
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?year: '1957 年',
?? ??? ??? ??? ??? ??? ?value: 145
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?year: '1958 年',
?? ??? ??? ??? ??? ??? ?value: 48
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?]
?? ??? ??? ?}
?? ??? ?},
?? ??? ?mounted() {
?? ??? ??? ?const bar = new Bar('container', {
?? ??? ??? ??? ?data: this.sj,
?? ??? ??? ??? ?xField: 'value',
?? ??? ??? ??? ?yField: 'year',
?? ??? ??? ??? ?seriesField: 'year',
?? ??? ??? ??? ?legend: {
?? ??? ??? ??? ??? ?position: 'top-left',
?? ??? ??? ??? ?},
?? ??? ??? ?});
?? ??? ??? ?bar.render();
?? ??? ?}
?? ?}
</script>
<style> </style>
AntV g2的簡單運用
G2 是一套基于圖形語法理論的可視化底層引擎,以數(shù)據(jù)驅(qū)動,提供圖形語法與交互語法,具有高度的易用性和擴展性。
使用 G2,你可以無需關(guān)注圖表各種繁瑣的實現(xiàn)細節(jié),一條語句即可使用 Canvas 或 SVG 構(gòu)建出各種各樣的可交互的統(tǒng)計圖表。
安裝
npm install @antv/g2 --save
成功安裝完成之后,即可使用 import 或 require 進行引用。
import { Chart } from '@antv/g2';在繪圖前我們需要為 G2 準備一個 DOM 容器:
<div id="container"></div>
數(shù)據(jù)類型和實現(xiàn)步驟
const data = [
? { year: '1951 年', sales: 38 },
? { year: '1952 年', sales: 52 },
? { year: '1956 年', sales: 61 },
? { year: '1957 年', sales: 145 },
? { year: '1958 年', sales: 48 },
? { year: '1959 年', sales: 38 },
? { year: '1960 年', sales: 38 },
? { year: '1962 年', sales: 38 },
];
const chart = new Chart({
? container: 'container',
? autoFit: true,
? height: 500,
});
chart.data(data);
chart.scale('sales', {
? nice: true,
});
chart.tooltip({
? showMarkers: false
});
chart.interaction('active-region');
chart.interval().position('year*sales');
chart.render();語法介紹
1.new Chart() 創(chuàng)建 Chart 圖表對象,指定圖表所在的容器 id、圖表的寬高、邊距等信息;
2.chart.data() 載入圖表數(shù)據(jù)源;
3.使用圖形語法進行圖表的繪制;
4.chart.render() 渲染圖表。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Vue中使用CSS3實現(xiàn)內(nèi)容無縫滾動的示例代碼
這篇文章主要介紹了在Vue中使用CSS3實現(xiàn)內(nèi)容無縫滾動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Vue 實現(xiàn)列表動態(tài)添加和刪除的兩種方法小結(jié)
今天小編就為大家分享一篇Vue 實現(xiàn)列表動態(tài)添加和刪除的兩種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
一文搞懂Vue3中的異步組件defineAsyncComponentAPI的用法
這篇文章主要介紹了一文搞懂Vue3中的異步組件,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
使用Pinia Persistedstate插件實現(xiàn)狀態(tài)持久化的操作方法
Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開發(fā)者的喜愛,然而,Pinia 默認使用內(nèi)存存儲狀態(tài),為了解決這個問題,我們可以借助 Pinia Persistedstate 插件來實現(xiàn)狀態(tài)的持久化存儲,本文將詳細介紹該插件的使用方法,需要的朋友可以參考下2024-11-11
Vue.extend和VueComponent的關(guān)系源碼解析
這篇文章主要為大家詳解了Vue.extend和VueComponent的關(guān)系源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Vue使用pdf.js和docx-preview實現(xiàn)docx和pdf的在線預(yù)覽
這篇文章主要為大家詳細介紹了在Vue中使用pdf.js和docx-preview實現(xiàn)docx和pdf的在線預(yù)覽的相關(guān)知識,文中的示例代碼講解詳細,需要的可以參考下2025-03-03
Vue中slot-scope的深入理解(適合初學(xué)者)
這篇文章主要給大家介紹了關(guān)于Vue中slot-scope的深入理解,這個教程非常適合初學(xué)者,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

