AVA icon indicating copy to clipboard operation
AVA copied to clipboard

打算支持vue吗

Open MaJiaXuan opened this issue 6 years ago • 2 comments

打算支持vue吗

MaJiaXuan avatar Jan 07 '20 08:01 MaJiaXuan

没有提供 vue react 等 组件,你可以自己封装一下组件,

Jeffy2012 avatar Jan 07 '20 09:01 Jeffy2012

@MaJiaXuan

一个简单的封装:

1. vue

// Home.vue

<template>
  <Chart :option="option" class="chart" />
</template>

<script>
import Chart from "./Chart";
export default {
  components: {
    Chart
  },
  data() {
    return {
      option: {
        data: [
          { year: "1951 年", sales: 38 },
          { year: "1952 年", sales: 52 },
          { year: "1956 年", sales: 61 },
          { year: "1957 年", sales: 145 },
          { year: "1958 年", sales: 48 }
        ],
        toolbar: true,
        title: "不同年份销量数据",
        description: "一个简单的图表",
        development: true
      }
    };
  }
};
</script>

<style lang="less" scoped>
.chart {
  width: 800px;
  height: 600px;
}
</style>

// chart.js

<template>
  <div ref="chart"></div>
</template>

<script>
import { autoChart } from "@antv/chart-advisor";
export default {
  props: {
    option: {
      required: true,
      default() {
        return {
          data: []
        };
      }
    }
  },
  methods: {
    async init() {
      if (!this.$refs.chart) {
        return;
      }
      let { data, ...option } = this.option;
      await autoChart(this.$refs.chart, data, option || {});
    }
  },
  mounted() {
    this.init();
  }
};
</script> 

2.react

import React, { useEffect, useRef } from 'react';
import { autoChart, AutoChartOptions } from '@antv/chart-advisor';

export interface IAChart extends AutoChartOptions {
  data: any[];
  [key: string]: any;
}

const AChart = ({ option: { data = [], ...option }, ...props }) => {
  const instance = useRef(null);
  useEffect(() => {
    if (!instance) {
      return;
    }
    autoChart(instance.current, data, option || {});
  }, [option, data]);
  return <div ref={instance} {...props} />;
};

export default function() {
  const option:IAChart  = {
        data: [
          { year: "1951 年", sales: 38 },
          { year: "1952 年", sales: 52 },
          { year: "1956 年", sales: 61 },
          { year: "1957 年", sales: 145 },
          { year: "1958 年", sales: 48 }
        ],
        toolbar: true,
        title: "不同年份销量数据",
        description: "一个简单的图表",
        development: true
      }

  return (
    <div>  
       <AChart option={option} />
    </div>
  );
}

realeve avatar Jan 07 '20 12:01 realeve