interval3D

主要用于绘制 3D 条形图。

开始使用

首先需要使用 @antv/g-webgl 作为渲染器并注册以下两个插件:

然后设置 z 通道、scale 和 z 坐标轴,最后在场景中添加光源。

import { Runtime, corelib, extend } from '@antv/g2';
import { threedlib } from '@antv/g2-extension-3d';
import { CameraType } from '@antv/g';
import { Renderer as WebGLRenderer } from '@antv/g-webgl';
import { Plugin as ThreeDPlugin, DirectionalLight } from '@antv/g-plugin-3d';
import { Plugin as ControlPlugin } from '@antv/g-plugin-control';

// Create a WebGL renderer.
const renderer = new WebGLRenderer();
renderer.registerPlugin(new ControlPlugin());
renderer.registerPlugin(new ThreeDPlugin());

const Chart = extend(Runtime, {
  ...corelib(),
  ...threedlib(),
});

// 初始化图表实例
const chart = new Chart({
  container: 'container',
  renderer,
  width: 500,
  height: 500,
  depth: 400,
});

const data = [];
for (let x = 0; x < 5; ++x) {
  for (let z = 0; z < 5; ++z) {
    data.push({
      x: `x-${x}`,
      z: `z-${z}`,
      y: 10 - x - z,
      color: Math.random() < 0.33 ? 0 : Math.random() < 0.67 ? 1 : 2,
    });
  }
}

chart.options({
  type: 'interval3D',
  data: {
    type: 'inline',
    value: data,
  },
  encode: { x: 'x', y: 'y', z: 'z', color: 'color', shape: 'cube' },
  coordinate: { type: 'cartesian3D' },
  scale: { x: { nice: true }, y: { nice: true }, z: { nice: true } },
  legend: false,
  axis: {
    x: { gridLineWidth: 2 },
    y: { gridLineWidth: 2, titleBillboardRotation: -Math.PI / 2 },
    z: { gridLineWidth: 2 },
  },
  style: { opacity: 0.7 },
});

chart.render().then(() => {
  const { canvas } = chart.getContext();
  const camera = canvas.getCamera();
  camera.setPerspective(0.1, 5000, 50, 1280 / 960);
  camera.setType(CameraType.ORBITING);
  camera.rotate(-20, -20, 0);

  // Add a directional light into scene.
  const light = new DirectionalLight({
    style: {
      intensity: 2.5,
      fill: 'white',
      direction: [-1, 0, 1],
    },
  });
  canvas.appendChild(light);
});

更多的案例,可以查看图表示例页面。

选项

目前 interval3D 有以下内置 shape 图形:

图形 描述 示例
cube 绘制立方体
cylinder 绘制圆柱
cone 绘制圆锥

cube

属性 描述 类型 默认值
fill 图形的填充色 string | Function<string> -
opacity 图形的整体透明度 number | Function<number> -
cursor 鼠标样式。同 css 的鼠标样式,默认 ‘default’。 string | Function<string> ‘default’
全站搜索
文档与示例输入关键词开始搜索

输入关键词,查找文档、API 与示例

选择打开Esc关闭