antd使用g2plot统计图表(7)


G2Plot 是一套简单、易用、并具备一定扩展能力和组合能力的统计图表库,基于图形语法理论搭建而成。

特性
📦 开箱即用、默认好用的高质量统计图表
🎨 提炼自企业级产品的视觉语言和设计规范
📊 响应式图表:致力于解决图表在任何数据和显示尺寸下的基本可读性问题
🔳 图层化设计方法:在 G2Plot 体系下,图表不仅仅只是各不相关的实例,图层概念的引入提供了多图表组合叠联动,共同讲述一个数据故事的可能性

https://antv.vision/zh
https://g2plot.antv.vision/zh/docs/manual/introduction

一、基本步骤

安装g2plot

npm install @antv/g2plot --save

之后便可使用 import 或 require 进行引用:

import { Line } from '@antv/g2plot';

二、使用

step1: 创建图表容器

<a-row :gutter="2">
      <a-col :span="12">
        <div id="container" style="width: 100%"></div>
        <div id="g2-Area" style="width: 100%"></div>
      </a-col>
      <a-col :span="12">
        <div id="g2-Column" style="width: 100%"></div>
        <div id="g2-Pie" style="width: 100%"></div>
      </a-col>
    </a-row>

step2: 引入数据。G2Plot 的数据源格式是 JSON 数组,数组的每个元素是一个标准 JSON 对象,部分图表除外。
step3: 创建并渲染图表

const line = new Line('container', {
  data,
  xField: 'year',
  yField: 'value',
});

line.render();

三、完整代码

<template>
  <div>
    <a-card :bordered="false">
      <div style="display: flex; flex-wrap: wrap">
        <head-info title="参与项目" content="8" :bordered="true"/>
        <head-info title="本周完成case数" content="32" :bordered="true"/>
        <head-info title="团队内排名" content="3/61"/>
      </div>
    </a-card>
    <a-row :gutter="2">
      <a-col :span="12">
        <div id="container" style="width: 100%"></div>
        <div id="g2-Area" style="width: 100%"></div>
      </a-col>
      <a-col :span="12">
        <div id="g2-Column" style="width: 100%"></div>
        <div id="g2-Pie" style="width: 100%"></div>
      </a-col>
    </a-row>
  </div>
</template>

<script>
//引入g2plot
import { Line, Area, Column, Pie} from '@antv/g2plot';
import HeadInfo from "@/components/tool/HeadInfo";

const data = [
  { type: '郑先森', value: 27 },
  { type: '曹先森', value: 25 },
  { type: '王先森', value: 18 },
  { type: '李先森', value: 15 },
  { type: '张先森', value: 10 },
  { type: '其他', value: 5 },
];

export default {
  name: 'Demo',
  components: {HeadInfo},
  data() {
    return {
      data,
    }
  },
  mounted() {
    fetch('https://gw.alipayobjects.com/os/bmw-prod/e00d52f4-2fa6-47ee-a0d7-105dd95bde20.json')
        .then((res) => res.json())
        .then((data) => {
          const linePlot = new Line('container', {
            data,
            xField: 'year',
            yField: 'gdp',
            seriesField: 'name',
            yAxis: {
              label: {
                formatter: (v) => `${(v / 10e8).toFixed(1)} B`,
              },
            },
            legend: {
              position: 'top',
            },
            smooth: true,
            // @TODO 后续会换一种动画方式
            animation: {
              appear: {
                animation: 'path-in',
                duration: 5000,
              },
            },
          });
          linePlot.render();
        });
    fetch('https://gw.alipayobjects.com/os/bmw-prod/1d565782-dde4-4bb6-8946-ea6a38ccf184.json')
        .then((res) => res.json())
        .then((data) => {
          const area = new Area('g2-Area', {
            data,
            xField: 'Date',
            yField: 'scales',
            xAxis: {
              tickCount: 5,
            },
            animation: false,
            slider: {
              start: 0.1,
              end: 0.9,
              trendCfg: {
                isArea: true,
              },
            },
          });
          area.render();
        });
    fetch('https://gw.alipayobjects.com/os/antfincdn/8elHX%26irfq/stack-column-data.json')
        .then((data) => data.json())
        .then((data) => {
          const stackedColumnPlot = new Column('g2-Column', {
            data,
            isStack: true,
            xField: 'year',
            yField: 'value',
            seriesField: 'type',
            label: {
              // 可手动配置 label 数据标签位置
              position: 'middle', // 'top', 'bottom', 'middle'
            },
            interactions: [{ type: 'active-region', enable: false }],
            columnBackground: {
              style: {
                fill: 'rgba(0,0,0,0.1)',
              },
            },
          })
          stackedColumnPlot.render();
        });

    const piePlot = new Pie('g2-Pie', {
      appendPadding: 10,
      data,
      angleField: 'value',
      colorField: 'type',
      radius: 1,
      innerRadius: 0.6,
      label: {
        type: 'inner',
        offset: '-50%',
        content: '{value}',
        style: {
          textAlign: 'center',
          fontSize: 14,
        },
      },
      interactions: [{ type: 'element-selected' }, { type: 'element-active' }],
      statistic: {
        title: false,
        content: {
          style: {
            whiteSpace: 'pre-wrap',
            overflow: 'hidden',
            textOverflow: 'ellipsis',
          },
          content: 'AntV\nG2Plot',
        },
      },
    });
    piePlot.render();
  }
}
</script>

<style scoped>
</style>

三、效果演示

在这里插入图片描述
用的都是示例,需要时再调后端接口吧。。。。

### 关于 Vue.js 图形化界面的解决方案 Vue.js 是一种流行的前端框架,其生态系统中有许多用于实现图形化界面的库和解决方案。这些工具可以帮助开发者更高效地构建复杂的用户界面。 #### 1. **Element Plus** Element Plus 是 Element UI 的官方 Vue 3 版本[^5]。它是一个基于 Vue 3 的桌面端组件库,提供了丰富的预设组件,支持国际化、深色模式以及多种主题定制选项。虽然它的主要目标是提供通用的 UI 组件,但它也适用于简单的图形化需求场景。 ```javascript import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/dist/index.css'; const app = createApp(App); app.use(ElementPlus); app.mount('#app'); ``` #### 2. **Ant Design of Vue** Ant Design of Vue 是 Ant Design 的 Vue 实现版本,专注于企业级产品的设计体系[^4]。除了常见的表单、表格等功能外,还内置了一些图表展示能力,适合需要数据可视化的企业应用。 ```javascript import { createApp } from 'vue'; import Antd from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; const app = createApp(App); app.use(Antd); app.mount('#app'); ``` #### 3. **Viser-Vue** Viser-Vue 是蚂蚁金服开源的数据可视化解决方案 G2Plot 和 Viser 的 Vue 封装版[^4]。它可以无缝集成到 Vue 项目中,帮助开发者快速创建高质量的统计图表和动态效果。 ```javascript <template> <div id="mountNode"></div> </template> <script> export default { mounted () { this.$viser.g2.plot('mountNode', {...}); } } </script> ``` #### 4. **DlhSoft Gannt Chart** 如果需要在 Vue 中实现甘特图功能,可以考虑 DlhSoft 提供的甘特图库[^1]。这是一个高度可配置的交互式甘特图组件,支持拖拽操作、时间轴缩放以及其他高级特性。 ```javascript import { GanttChartView } from '@dlhsoft/ganttchartviewforwebcomponents/vue'; new GanttChartView({ elementId: 'ganttChartDiv', tasks: [...], }); ``` #### 5. **Storybook for Vue** 对于复杂的应用程序,Storybook 是一个强大的工具,可用于隔离开发和测试 Vue 组件[^4]。尽管 Storybook 主要用于组件管理和调试,但也可以配合其他图形化工具一起使用,从而提高开发效率。 ```bash npx sb init --type vue ``` --- ###
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值