Antv | G2-Plot图表在vue项目中的使用

什么是G2-Plot

G2Plot 是一套简单、易用、并具备一定扩展能力和组合能力的统计图表库,基于图形语法理论搭建而成,"G2Plot"中的 G2 即意指图形语法 (the Gramma of Graphics),同时也致敬了 ggplot2。(详情可查看g2官网。)
G2图表 、 Echarts图表都是我们在vue项目中视图化很好表现的形式,因此利用好图表对视图和模型数据进行连接,能够很好的进行数据分析和数据表达。
G2的特性:
📦 开箱即用、默认好用的高质量统计图表
🎨 提炼自企业级产品的视觉语言和设计规范
📊 响应式图表:致力于解决图表在任何数据和显示尺寸下的基本可读性问题
🔳 图层化设计方法:在 G2Plot 体系下,图表不仅仅只是各不相关的实例,图层概念的引入提供了多图表组合叠联动,共同讲述一个数据故事的可能性

G2在常规html中的引用和使用:
1.引入g2**

**
第一种方法:


 
<!-- 引入在线资源 -->
<script type="text/javascript" src="https://unpkg.com/@antv/g2plot@latest/dist/g2plot.min.js"></script>
<script>
  const {
      Line } = G2Plot;
</script>

第二种方法:

<!-- 下载到本地 引入本地脚本 -->
<script src="./g2plot.min.js"></script>
<script>
  const {
      Line } = G2Plot;
</script>

2. 设置父容器

<div id="root"></div>

3. 引入数据

const data = [
  { year: '1991', value: 3 },
  { year: '1992', value: 4 },
  { year: '1993', value: 3.5 },
];

4渲染并创建图表

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

完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta<
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,您可以按照以下步骤在Vue3.0项目搭建g2plot: 1. 安装g2plot和@antv/data-set依赖: ``` npm install @antv/g2plot @antv/data-set --save ``` 2. 在需要使用g2plot的组件引入g2plot和@antv/data-set: ``` import { defineComponent } from 'vue'; import { Chart, Line, Point } from '@antv/g2plot'; import DataSet from '@antv/data-set'; export default defineComponent({ ... }); ``` 3. 在mounted钩子函数创建图表并渲染: ``` import { defineComponent } from 'vue'; import { Chart, Line, Point } from '@antv/g2plot'; import DataSet from '@antv/data-set'; export default defineComponent({ mounted() { // 创建数据集 const data = [ { 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 }, ]; const ds = new DataSet(); const dv = ds.createView().source(data); dv.transform({ type: 'fold', fields: ['value'], key: 'type', value: 'value', }); // 创建图表 const chart = new Chart({ container: 'chart-container', // 指定容器 autoFit: true, height: 500, padding: [40, 40, 40, 40], }); chart.data(dv.rows); chart.scale({ year: { range: [0, 1], }, value: { min: 0, nice: true, }, }); chart.tooltip({ shared: true, showMarkers: false, }); chart.axis('value', { label: { formatter: (val) => { return `${val}亿`; }, }, }); chart.line().position('year*value').color('type'); chart.point().position('year*value').color('type').shape('circle'); chart.render(); }, }); ``` 4. 在模板添加容器: ``` <template> <div id="chart-container"></div> </template> ``` 这样就搭建好了g2plotVue3.0项目的环境,您可以根据需要修改图表的类型、样式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值