antvis/G2

仪表盘的实现 #4383

pepper-nice posted onGitHub

API 设计

仪表盘

import { G2Spec } from '../../../src';

export function gaugeDefault(): G2Spec {
  /**
   * 两种数据格式
   * 1. { target: 4000, total: 10000 }
   * 2. { percent: 0.75 }
   */
  const data = { target: 4000, total: 10000 };
  return {
    type: 'gauge',
    data,
    encode: {
      target: 'target',
      total: 'total',
      // percent:'percent',
    },
    layout: {
      // 环半径
      radius: 1,
      // 环起始角度
      startAngle: -Math.PI,
      // 环中止角度
      endAngle: Math.PI,
      // 仪表盘辅助圆弧的样式。
      range: {
        // 根据ticks映射颜色
        ticks: [0, 1 / 3, 2 / 3, 1],
        color: ['#F4664A', '#FAAD14', '#30BF78'],
      },
      /** 默认 type = '', type = meter: 环为离散型 */
      type: 'meter',
      /** type 为 meter 时配置 */
      meter: {
        // 总步数
        steps: 30,
        // 间距宽度
        gap: 0.5,
      },
      text: {
        // 优先级:customHtml > formatter > content
        customHtml: () => ``,
        formatter: () => ``,
        content: () => ``,
        dx: 0,
        dy: 0,
      },
    },
    scale: {
      color: {
        range: [0, 0.5, 1],
        color: ['#F4664A', '#FAAD14', '#30BF78'],
      },
    },
    label: {},
    style: {
      // 小圆圈样式
      pinFill: '#ccc',
      // 指针样式
      pointerFill: '#ccc',
      // 文本样式
      textFill: '#000',
    },
  };
}

实现

<img width="509" alt="image" src="https://user-images.githubusercontent.com/109653633/204807652-6db0652b-80b9-48f6-a04d-18ed5a7c274e.png">

 [
  // 仪表盘圆环部分
  {
    type: 'shape',
    style: {
      render: arcShape,
    },
  },
  // 指示针
  {
    type: 'shape',
    style: {
      render: indicatorShape,
    },
  },
  // 文本
  {
    type: 'shape',
    style: {
      render: statisticShape,
    },
  },
]

text 为什么放在 layout 中? layout 在仪表盘中的含义是?

posted by visiky over 2 years ago

想保持高阶 Mark 的 API 统一性。所有有关布局的设置都放在 layout 下面

posted by pepper-nice over 2 years ago

@pepper-nice 移入到 discussion 中吧~

posted by hustcc over 2 years ago

Fund this Issue

$0.00
Funded

Pull requests