蚂蚁可视化图表AntV day5

image.png

进入AntV官网后,我们选择G2。

安装AntV

cnpm install @antv/g2 --save
在home.vue里import G2 from '@antv/g2';
然后在export default里添加相应的样式即可。

export default {
  mounted() {
    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();
  },

  components: {
    MyCard
  }
};

最后就可以使用该图表了。

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

效果图:


image.png

当我们要使用多个样式的时候,推荐把初始化图表的函数封装起来,如下图:


image.png
export default {
  mounted() {
    this.initc2();
    this.initc1();
  },
  methods: {
    initc2() {
      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: "c2", // 指定图表容器 ID
        width: 600, // 指定图表宽度
        height: 400 // 指定图表高度
      });
      // Step 2: 载入数据源
      chart.source(data);
      // Step 3:创建图形语法,绘制柱状图,由 genre 和 sold 两个属性决定图形位置,genre 映射至 x 轴,sold 映射至 y 轴
      chart
        .interval()
        .position("genre*sold")
        .color("genre");
      // Step 4: 渲染图表
      chart.render();
    },
    initc1() {
      var data = [
        {
          year: "2001",
          population: 41.8
        },
        {
          year: "2002",
          population: 38
        },
        {
          year: "2003",
          population: 33.7
        },
        {
          year: "2004",
          population: 30.7
        },
        {
          year: "2005",
          population: 25.8
        },
        {
          year: "2006",
          population: 31.7
        },
        {
          year: "2007",
          population: 33
        },
        {
          year: "2008",
          population: 46
        },
        {
          year: "2009",
          population: 38.3
        },
        {
          year: "2010",
          population: 28
        },
        {
          year: "2011",
          population: 42.5
        },
        {
          year: "2012",
          population: 30.3
        }
      ];

      var chart = new G2.Chart({
        container: "c1",
        height: 600,
        width: 900
      });
      chart.source(data);
      chart.coord("polar");
      chart.legend({
        position: "right",
        offsetY: -window.innerHeight / 2 + 180,
        offsetX: -140
      });
      chart.axis(false);
      chart
        .interval()
        .position("year*population")
        .color("year", G2.Global.colors_pie_16)
        .style({
          lineWidth: 1,
          stroke: "#fff"
        });
      chart.render();
    }
  },
  components: {
    MyCard
  }
};

然后再调用以下即可:


image.png

这里再次用到了iView提供的Raw布局。

 <Row type="flex" justify="center" align="middle" class="code-row-bg">
      <Col span="8">
        <div id="c2"></div>
      </Col>
      <Col span="16">
        <div id="c1"></div>
      </Col>
    </Row>

效果图:


image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 简说Vue (组件库) https://github.com/ElemeFE/element" 饿了么出品的VUE...
    Estrus丶阅读 1,908评论 0 1
  • 本篇主要介绍三块知识点: node.js vue.js webpack前端工程化 本篇不是写给零基础的同学看的,读...
    3hours阅读 1,485评论 0 0
  • 背景 一直以来都想着如何把一个项目的前后端代码完全分离,单独开发,后来看到springboot的静态资源文件目录,...
    慢慢淡忘阅读 453评论 0 1
  • 夜入三更,一种安静的气息弥漫屋内。白天的余热在空气中滞留,偶尔一阵夜风透过小窗,带来几丝凉意。在这样的夏夜,我一如...
    竹鸿初阅读 584评论 6 8
  • 一、一个好的社群运营官的标准:能独立运营好一个收费社群。 二、社群运营岗位有三个方面的要求: 1.能够管理好线上用...
    剑侠火羽阅读 516评论 0 0

友情链接更多精彩内容