Vue에서 antv 예제 코드 사용하기

6570 단어 Vueantv
하나, vue 원형에서 사용
1. 먼저 antv/g2 설치

yarn add @antv/g2 --save
2. 메인에서js에서 vue 원형 실례에 걸기

const G2 = require('@antv/g2')
Vue.prototype.$G2 = G2
3. vue 파일에서 mounted 생명주기에서 직접 사용 가능

<template>
 <div>
  <div id="c1"></div>
 </div>
</template>

<script>
export default {
 mounted() {
  this.initComponent();
 },
 data() {
  return {
   msg: "",
   chart: null,
   data: [
    { genre: "Sports", sold: 275 },
    { genre: "Strategy", sold: 115 },
    { genre: "Action", sold: 120 },
    { genre: "Shooter", sold: 350 },
    { genre: "Other", sold: 150 }
   ]
  };
 },
 methods: {
  initComponent() {
   const chart = new this.$G2.Chart({
    container: "c1",
    width: 600,
    height: 300
   });
   chart.source(this.data);
   chart
    .interval()
    .position("genre*sold")
    .color("genre");
   this.chart = chart;
   this.chart.render();
  }
 }
};
</script>

둘째, 필요에 따라 인용
1. 또는 atv/g2 설치

yarn add @antv/g2 --save
2. 직접 구성 요소에서 필요에 따라 도입

<template>
 <div>
  <div id="l1"></div>
 </div>
</template>

<script>
import { Chart } from "@antv/g2";
export default {
 data() {
  return {
   year: [
    { 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() {
  this.initLineChart()
 },
 methods: {
  initLineChart() {
   const chart = new Chart({
    container: "l1",
    autoFit: true,
    height: 500
   });
   chart.data(this.year);
   chart.scale({
    year: {
     range: [0, 1]
    },
    value: {
     min: 0,
     nice: true
    }
   });
   chart.tooltip({
    showCrosshairs: true, //   Tooltip  
    shared: true
   });
   chart
    .line()
    .position("year*value")
    .label("value");
   chart.point().position("year*value");
   chart.render();
  }
 }
};
</script>
<style scoped>
</style>
예제 코드

<template>
 <div>
  <div><h1 style="color: white">{{title}}</h1></div>
  <span>
   <div id="c1"></div>
   <div id="mountNode"></div>
  </span>
 </div>
</template>

<script>
  import G2 from '@antv/g2';
  export default {
    name: "spectaculars",
    data(){
      return{
        title:' ',
        basicColumnChartProp:{
          data:[{ genre: 'Sports', sold: 275 },
            { genre: 'Strategy', sold: 115 },
            { genre: 'Action', sold: 120 },
            { genre: 'Shooter', sold: 350 },
            { genre: 'Other', sold: 150 }],
          container:'c1',
          width:600,
          height:300
        },
        basicBarChartProp:{
          container:'mountNode',
          size:{'width':500,'height':300},
          data:[
            {
              country: ' ',
              population: 18203
            }, {
              country: ' ',
              population: 23489
            }, {
              country: ' ',
              population: 29034
            }, {
              country: ' ',
              population: 104970
            }, {
              country: ' ',
              population: 131744
            }
          ]
        }
      }
    },
    methods:{
      test:function () {
        const data = this.basicColumnChartProp.data;
        // Step 1:   Chart  
        const chart = new G2.Chart({
          container: this.basicColumnChartProp.container, //   ID
          width : this.basicColumnChartProp.width, //  
          height : this.basicColumnChartProp.height //  
        });
        // Step 2:  
        chart.source(data);
        // Step 3: , ,  genre   sold  ,genre   x  ,sold   y  
        chart.interval().position('genre*sold').color('genre')
        // Step 4:  
        chart.render();
      },
      basicBarChart:function () {
        let data = this.basicBarChartProp.data;
        let chart = new G2.Chart({
          container: this.basicBarChartProp.container,
          width:this.basicBarChartProp.size.width,
          height:this.basicBarChartProp.size.height
        });
        chart.source(data);
        chart.axis('country', {
          label: {
            offset: 12
          }
        });
        chart.coord().transpose();
        chart.interval().position('country*population');
        chart.render();
      }
    },
    mounted() {
      this.test();
      this.basicBarChart();
    },
    beforeCreate () {
      document.querySelector('body').setAttribute('style', 'background:#000000')
    },
    beforeDestroy () {
      document.querySelector('body').removeAttribute('style')
    }
  }
</script>

<style scoped>

</style>
Vue에서 antv를 사용하는 예시 코드에 대한 이 글을 소개합니다. 더 많은 관련 Vue에서 antv를 사용하는 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기