支付宝小程序中使用F2图表

支付宝小程序中使用F2图表

介绍

最近在支付宝小程序开发中接到显示图表的需求,因为支付宝小程序方未提供相关插件,并且目前支付宝小程序不支持document,所以根据推荐使用f2-canvas图表组件。
由于 f2-canvas 组件主要是对小程序的画布上下文和 html5 canvas 的上下文进行了适配以支持 F2 在小程序端的渲染,所以 F2 能绘制什么图表,小程序端就能绘制什么图表。语法也可以按照F2来。
F2 API:https://antv.alipay.com/zh-cn/f2/3.x/api/index.html

效果图

附上最近做的效果图:
柱状图折线图

f2-canvas在小程序中的使用步骤:(以折线图为例)

  1. 小程序中安装依赖 :npm install @antv/my-f2
  2. .acss文件
canvas {
  width: 100%;
  height: 100%;
}
  1. .axml文件
<canvas id="area" onTouchStart="touchStart" onTouchMove="touchMove" 
onTouchEnd="touchEnd" width="{{width}}" height="{{height}}" />
  1. .js文件
 import F2 from '@antv/my-f2';
 const app = getApp();
 const api = require('/api/api/index.js');
 Page({
  data: {
  },
  onLoad(options) {
  },
  onReady() {
    let me = this;
    // 折线图
    my.createSelectorQuery()
          .select('#area')
          .boundingClientRect()
          .exec((res) => {
            // 获取分辨率
            const pixelRatio = my.getSystemInfoSync().pixelRatio;
            // 获取画布实际宽高
            const canvasWidth = res[0].width;
            const canvasHeight = res[0].height;
            // 高清解决方案
            me.setData({
              width: canvasWidth * pixelRatio,
              height: canvasHeight * pixelRatio
            });
            const myCtx = my.createCanvasContext('area');
            myCtx.scale(pixelRatio, pixelRatio); //按照设置的分辨率进行放大
            const canvas = new F2.Renderer(myCtx);
            me.canvas = canvas;
            me.drawLineChart(canvas, res[0].width, res[0].height);
          });
  },
  // 折线
  drawLineChart(canvas, width, height) {
    let me = this;
    let url = api.generalSituationInfo;
    let params = {};
    let header = 'application/json';
    app.request(url, params, 'GET', header, true).then(res => {
      if (res.code == 0) {
        const data = res.data.dataList;  //折线图数据源
        const chart = new F2.Chart({
          el: canvas,
          width,
          height,
          padding: [10, 0, 60, 0]
        });
        chart.source(data, {
          hours: {
            tickCount: 5,  //x默认显示5个时间
          }
        });
        chart.axis('hours', {
          line: null,  //x轴线不显示
          label: function label(text, index, total) { //设置X轴字体颜色大小等
            const textCfg = {
              fill: '#ADCAEF',
              fontSize: 12
            };
            if (index === 0) {
              textCfg.textAlign = 'left';
            } else if (index === total - 1) {
              textCfg.textAlign = 'right';
            }
            return textCfg;
          }
        });
        chart.axis('number', {
          grid: null  //去掉网格线
        });
        chart.legend({  //设置图例样式位置等
          position: 'bottom',
          align: 'center',
          itemWidth: null,
          nameStyle: {
            fill: '#ADCAEF'
          },
        });
        chart.tooltip({  //设置提示信息的样式等
          crosshairsStyle: {
            stroke: 'white',
            lineWidth: 1
          },
          showCrosshairs: true,
          custom: true, // 自定义 tooltip 内容框
          onShow: function onShow(ev) {
            me.setData({
              currentHour: ev.items[0].title,
              hourCount: ev.items[0].value,
              weekDisplay: 'day'
            })
          }
        });
        chart.line()
          .position('hours*number')
          .color('type', function (type) {
            if (type === '今日访问数') {  //设置折线的颜色
              return 'white';
            }
            if (type === '昨日访问数') {
              return '#7EB0F8';
            }
          })
          .shape('smooth') 

        chart.render();
        return chart;
      }
    })
  },
  touchStart(e) {
    if (this.canvas) {
      this.canvas.emitEvent('touchstart', [e]);
    }
  },
  touchMove(e) {
    if (this.canvas) {
      this.canvas.emitEvent('touchmove', [e]);
    }
  },
  touchEnd(e) {
    if (this.canvas) {
      this.canvas.emitEvent('touchend', [e]);
    }
  }
});
  1. 提供测试的json数据:(替换数据源data)
[{"hours":"01:00","number":0,"type":"今日访问数"},{"hours":"02:00","number":0,"type":"今日访问数"},{"hours":"03:00","number":0,"type":"今日访问数"},{"hours":"04:00","number":0,"type":"今日访问数"},{"hours":"05:00","number":0,"type":"今日访问数"},{"hours":"06:00","number":0,"type":"今日访问数"},{"hours":"07:00","number":4,"type":"今日访问数"},{"hours":"08:00","number":17,"type":"今日访问数"},{"hours":"09:00","number":7,"type":"今日访问数"},{"hours":"10:00","number":7,"type":"今日访问数"},{"hours":"11:00","number":6,"type":"今日访问数"},{"hours":"12:00","number":1,"type":"今日访问数"},{"hours":"13:00","number":0,"type":"今日访问数"},{"hours":"14:00","number":0,"type":"今日访问数"},{"hours":"15:00","number":0,"type":"今日访问数"},{"hours":"16:00","number":0,"type":"今日访问数"},{"hours":"17:00","number":0,"type":"今日访问数"},{"hours":"18:00","number":0,"type":"今日访问数"},{"hours":"19:00","number":0,"type":"今日访问数"},{"hours":"20:00","number":0,"type":"今日访问数"},{"hours":"21:00","number":0,"type":"今日访问数"},{"hours":"22:00","number":0,"type":"今日访问数"},{"hours":"23:00","number":0,"type":"今日访问数"},{"hours":"00:00","number":0,"type":"今日访问数"},{"hours":"01:00","number":0,"type":"昨日访问数"},{"hours":"02:00","number":0,"type":"昨日访问数"},{"hours":"03:00","number":0,"type":"昨日访问数"},{"hours":"04:00","number":0,"type":"昨日访问数"},{"hours":"05:00","number":0,"type":"昨日访问数"},{"hours":"06:00","number":0,"type":"昨日访问数"},{"hours":"07:00","number":4,"type":"昨日访问数"},{"hours":"08:00","number":17,"type":"昨日访问数"},{"hours":"09:00","number":5,"type":"昨日访问数"},{"hours":"10:00","number":6,"type":"昨日访问数"},{"hours":"11:00","number":6,"type":"昨日访问数"},{"hours":"12:00","number":19,"type":"昨日访问数"},{"hours":"13:00","number":18,"type":"昨日访问数"},{"hours":"14:00","number":4,"type":"昨日访问数"},{"hours":"15:00","number":15,"type":"昨日访问数"},{"hours":"16:00","number":8,"type":"昨日访问数"},{"hours":"17:00","number":18,"type":"昨日访问数"},{"hours":"18:00","number":33,"type":"昨日访问数"},{"hours":"19:00","number":12,"type":"昨日访问数"},{"hours":"20:00","number":5,"type":"昨日访问数"},{"hours":"21:00","number":2,"type":"昨日访问数"},{"hours":"22:00","number":0,"type":"昨日访问数"},{"hours":"23:00","number":0,"type":"昨日访问数"},{"hours":"00:00","number":0,"type":"昨日访问数"}]
*git地址:https://github.com/antvis/my-f2
组件形式:https://github.com/ant-mini-program/mini-chart
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuwenwenwenwenyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值