vueä¸ä½¿ç¨G2
åè¨
è¿ä¸ç« è主è¦è®²è§£çæ¯å¦ä½ä½¿ç¨G2åG2Plot, éè¿é è¯»ä½ å¯ä»¥å¦ä¹ å°:
- G2æåºæ¬ç使ç¨
- å¨vueä¸ä½¿ç¨G2
- å¨vueä¸ä½¿ç¨G2Plot
ä¸ãG2ç®ä»
G2å ¶å®å°±æ¯ä¸å¥åºäºå¯è§åç¼ç çå¾å½¢è¯æ³ï¼ä½¿å¾å端è½å¤é«æççæåç§ç»è®¡å¾è¡¨...
ä¸åè¿å¤ä»ç»ï¼ç´æ¥è´´ææ¡£ï¼G2ææ¡£
äºãæåºæ¬ç使ç¨
ä¸ç»åä»»ä½çå端æ¡æ¶æ¥ä½¿ç¨G2
1. æµè§å¨å¼å ¥
å建ä¸ä¸ªå为demo1.html
çæ件,
æ³è¦å¨html
ä¸ä½¿ç¨G2
çè¯ï¼éè¦å
å¼å
¥è¿æ¥ã
è¿ä¸é¨åå®ç½ä¹æ详ç»ä»ç»ï¼æ两ç§å¼å ¥æ¹å¼ï¼
<!-- å¼å
¥å¨çº¿èµæº -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist/g2.min.js"></script>
<!-- å¼å
¥æ¬å°èæ¬ -->
<script src="./g2.js"></script>
<!-- éç¨è¿ç§å¼å
¥æ¹å¼çè¯ä¸è¬å
è¦å»å®æ¹æ对åºçg2èæ¬ä¸è½½ä¸æ¥åæ¾å°æ件çå级ç®å½ä¸ -->
2. å建 div
å¾è¡¨å®¹å¨
å¨é¡µé¢ç body
é¨åå建ä¸ä¸ª divï¼å¹¶å¶å®å¿
é¡»çå±æ§ id
ï¼
<div id="c1"></div>
3. ç¼åå¾è¡¨ç»å¶ä»£ç
<script>
const data = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 }
]; // G2 对æ°æ®æºæ ¼å¼çè¦æ±ï¼ä»
ä»
æ¯ JSON æ°ç»ï¼æ°ç»çæ¯ä¸ªå
ç´ æ¯ä¸ä¸ªæ å JSON 对象ã
// Step 1: å建 Chart 对象
const chart = new G2.Chart({
container: 'c1', // æå®å¾è¡¨å®¹å¨ ID
width : 600, // æå®å¾è¡¨å®½åº¦
height : 300 // æå®å¾è¡¨é«åº¦
});
// Step 2: è½½å
¥æ°æ®æº
chart.source(data);
// Step 3ï¼å建å¾å½¢è¯æ³ï¼ç»å¶æ±ç¶å¾ï¼ç± genre å sold 两个å±æ§å³å®å¾å½¢ä½ç½®ï¼genre æ å°è³ x è½´ï¼sold æ å°è³ y è½´
chart.interval().position('genre*sold').color('genre')
// Step 4: 渲æå¾è¡¨
chart.render();
</script>
å ä¸ç®¡ä¸é¢çåæ®µä½ çä¸çå¾æï¼å±å æææç»å®ç°äºï¼ç¶ååé¢å¨å»è¯¦ç»äºè§£æ¯ä¸ªå段ãæ¹æ³çå«ä¹ã
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>G2</title>
</head>
<!-- å¼å
¥G2å¨çº¿èµæº -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist/g2.min.js"></script>
<body>
<div id="c1"></div>
<script>
const data = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 }
]; // G2 对æ°æ®æºæ ¼å¼çè¦æ±ï¼ä»
ä»
æ¯ JSON æ°ç»ï¼æ°ç»çæ¯ä¸ªå
ç´ æ¯ä¸ä¸ªæ å JSON 对象ã
// Step 1: å建 Chart 对象
const chart = new G2.Chart({
container: 'c1', // æå®å¾è¡¨å®¹å¨ ID
width: 600, // æå®å¾è¡¨å®½åº¦
height: 300 // æå®å¾è¡¨é«åº¦
});
// Step 2: è½½å
¥æ°æ®æº
chart.source(data);
// Step 3ï¼å建å¾å½¢è¯æ³ï¼ç»å¶æ±ç¶å¾ï¼ç± genre å sold 两个å±æ§å³å®å¾å½¢ä½ç½®ï¼genre æ å°è³ x è½´ï¼sold æ å°è³ y è½´
chart.interval().position('genre*sold').color('genre')
// Step 4: 渲æå¾è¡¨
chart.render();
</script>
</body>
</html>
äºãç»åvueæ¥ä½¿ç¨G2
å¨vue
ä¸ä½¿ç¨G2
çè¯ï¼å为两ç§åºæ¯ï¼
- ç´æ¥ç¨
<script>
å¼å ¥ - å½ä»¤è¡å·¥å ·(CLIèææ¶,éè¦ä½¿ç¨npmå®è£ )
1. ç´æ¥ç¨<script>
å¼å
¥
è¥ä½ çå端项ç®ä¸æ¯ä½¿ç¨èææ¶vue-cli
å¼åçè¯ï¼ä¹å°±æ¯ç´æ¥ä¸è½½å¹¶ç¨ <script>
æ ç¾å¼å
¥çè¯ï¼å¯ä»¥åèä¸é¢ä¸èæåºæ¬ç使ç¨
ä¾å¦æä¸é¢ä¸ä¸ªç®åçvue
ä¾åï¼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>G2</title>
<style>
#app {
margin: 50px auto;
text-align: center;
}
</style>
</head>
<!--vue-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="app">
<h2>{{ msg }}</h2>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
msg: ''
},
mounted() {
this.msg = 'vueæ¡ä¾'
}
})
</script>
</body>
</html>
å¨ä¸é¢çæ件ä¸ä½¿ç¨G2
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>G2</title>
<style>
#app {
margin: 50px auto;
text-align: center;
}
</style>
</head>
<!--vue-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- å¼å
¥G2å¨çº¿èµæº -->
<script src="https://gw.alipayobjects.com/os/lib/antv/g2/3.4.10/dist/g2.min.js"></script>
<body>
<div id="app">
<h2>{{ msg }}</h2>
<div id="c1"></div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
msg: '',
chart: null, // å建ä¸ä¸ªchartåé
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 }
]
},
mounted() {
this.msg = 'vueæ¡ä¾'
// åå§åchart
const chart = new G2.Chart({
container: 'c1',
width: 600,
height: 300
})
chart.source(this.data)
chart
.interval()
.position('genre*sold')
.color('genre')
this.chart = chart // å°chartåéèµå¼ä¸ºvueä¸çchart, åé¢å¯ä»¥ç´æ¥ä½¿ç¨this.chartè°ç¨å¾è¡¨å¯¹è±¡
this.chart.render()
}
})
</script>
</body>
</html>
æå¼demo2.html
æ件åæ ·å¯ä»¥çå°ææã
2. npm
å®è£
è¥ä½ çå端项ç®æ¯ä½¿ç¨èææ¶vue-cli
å¼åçè¯,é£ä¹æ³è¦ä½¿ç¨G2
çè¯å¯ä»¥éç¨npm
çå®è£
æ¹å¼ï¼å®è£
å°é¡¹ç®ä¸ã
(1) å®è£
æå¼é¡¹ç®æ件ç®å½ï¼å¨ç»ç«¯è¾å ¥ï¼
npm install @antv/g2 --save
å®è£
æåä¹åå¨ä½ çnode_modules
æ件夹ä¸åºè¯¥ä¼å¤åº@antv/g2
æ件夹ã
(2) å¼å ¥
éè¦å¨main.js
æ件ä¸å¼å
¥G2
// main.js
import Vue from 'vue'
import G2 from '@antv/g2'
Vue.use(G2)
...
æ¤æ¶,ä½ å°±å¯ä»¥å¨é¡¹ç®çä»»ä½vue
æ件ä¸ç´æ¥ä½¿ç¨ææçG2
åè½äºã
ä¾å¦ï¼æå¨é¡¹ç®ä¸å建äºä¸ä¸ªg2Demo.vue
æ件
<template>
...
</template>
<script>
export default {
...
mounted() {
const chart = new G2.Chart({
...
})
}
}
</script>
æ¡ä¾å®æ´ä»£ç å°å: g2Demo.vue
ä¸ãå¨vueä¸ä½¿ç¨G2Plot
g2plot æ¯ä¸å¥ç®åãæç¨ãå¹¶å ·å¤ä¸å®æ©å±è½ååç»åè½åçç»è®¡å¾è¡¨åºï¼åºäºå¾å½¢è¯æ³ç论æ建èæï¼"g2plot"ä¸ç g2 å³ææå¾å½¢è¯æ³ (the Gramma of Graphics)ï¼åæ¶ä¹è´æ¬äº ggplot2ãå ·ä½å¯ä»¥æ¥çãG2Plotå®æ¹ææ¡£ã
èå¦æä½ è¦å¨vueä¸ä½¿ç¨npmå®è£ å®å¹¶ä¸ä½¿ç¨æ¯æä¸å®åºå«ç.
(1) å®è£ G2Plot
第ä¸æ¥å®è£ å®é½æ²¡æä»ä¹åºå«:
npm i --save-dev @antv/g2plot
(2) å ¨å±ä½¿ç¨G2Plot
è¿è®°å¾g2éæ们å¨vue项ç®æ¯å¦ä½ä½¿ç¨çå?
æ们å¨main.js
ä¸å¼ç¨å¹¶ä½¿ç¨å®:
// main.js
import Vue from 'vue'
import G2 from '@antv/g2'
Vue.use(G2)
...
ä¹æ以å¯ä»¥è¿æ ·ç¨æ¯å 为g2çæºç ä¹å°±æ¯@antv/g2
åå¤æ´é²äºä¸ä¸ªå¯¹è±¡, è¿ä¸ªå¯¹è±¡æºå¸¦äºææçå¾è¡¨ç»ä»¶.
èå¨G2Plotä¸å®å¹¶æ²¡ææ´é²ä¸ä¸ªè¿ä¸ªå¯¹è±¡, å æ¤å¦æä½ ä¹æ³ç¨ç¸åçæ¹å¼å¼ç¨å®çè¯, æ§å¶å°ä¼ç»ä½ æåºä¸ä¸ªè¦å:
warning in ./src/main.js
"export 'default' (imported as 'G2Plot') was not found in '@antv/g2plot'
å
¶å®è¿æ¶åæ们åªéè¦æ¢ä¸ç§å¼å
¥æ¹å¼, ç¨CommonJS
çå¼å
¥æ¹å¼å°±å¯ä»¥äº:
// main.js
const g2plot = require('@antv/g2plot') // 1. å¼å
¥g2plot
Vue.prototype.$g2plot = g2plot // 2. å°g2plotæè½½å°vueä¸
new Vue({
render: h => h(App),
}).$mount('#app')
å¨é¡µé¢ä¸ä½¿ç¨:
ä¾å¦å¨ä¸ä¸ªå«ådemo.vue
çæ件ä¸:
<template>
<div id="canvas"></div>
</template>
<script>
export default {
data () {
return {
mockData: [
{ year: '1991', value: 3 },
{ year: '1992', value: 4 },
{ year: '1993', value: 3.5 },
{ year: '1994', value: 5 },
{ year: '1995', value: 4.9 },
{ year: '1996', value: 6 },
{ year: '1997', value: 7 },
{ year: '1998', value: 9 },
{ year: '1999', value: 13 }
]
}
},
mounted() {
const linePlot = new this.$g2Plot.Line('canvas', {
data: this.mockData,
xField: 'year',
yField: 'value'
})
linePlot.render()
}
}
</script>
(3) æé使ç¨
ä¸é¢ðé£ç§æ¹å¼æ¯å°æ´ä¸ªg2ploté½æè½½å°äºå ¨å±çvue对象ä¸, ææ¶åæ们å¯è½ä¸éè¦ææçç»ä»¶, è¿æ¶åä½ å¯ä»¥æéå¼ç¨å®, èä¸å¿ å ¨é¨å¼ç¨:
<template>
<div id="canvas"></div>
</template>
<script>
+ import { Line } from '@antv/g2plot'
export default {
data () {
return {
mockData: [
{ year: '1991', value: 3 },
{ year: '1992', value: 4 },
{ year: '1993', value: 3.5 },
{ year: '1994', value: 5 },
{ year: '1995', value: 4.9 },
{ year: '1996', value: 6 },
{ year: '1997', value: 7 },
{ year: '1998', value: 9 },
{ year: '1999', value: 13 }
]
}
},
mounted() {
- const linePlot = new this.$g2Plot.Line('canvas', {
+ const linePlot = new Line('canvas', {
data: this.mockData,
xField: 'year',
yField: 'value'
})
linePlot.render()
}
}
</script>
上一篇: 全球Top10传感器厂商盘点
下一篇: 高通收购CSR,点燃车联网芯片大战