Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sphere shape used in 3d lib #5375 #5412

Merged
merged 10 commits into from
Aug 18, 2023
Merged

feat: sphere shape used in 3d lib #5375 #5412

merged 10 commits into from
Aug 18, 2023

Conversation

xiaoiver
Copy link
Contributor

@xiaoiver xiaoiver commented Aug 16, 2023

#5375

  • 官网在 “3D 可视化” 下增加了三个案例
  • 文档进阶主题下增加 /manual/extra-topics/3d-charts

透视投影:
Aug-17-2023 15-23-26

正交投影:
Aug-17-2023 15-25-46

使用方式

参考 chartRender3dScatterPlot 示例:

import { Renderer } from '@antv/g-webgl';
import { Plugin as ControlPlugin } from '@antv/g-plugin-control';
import { Plugin as ThreeDPlugin, DirectionalLight } from '@antv/g-plugin-3d';

const renderer = new Renderer();
renderer.registerPlugin(new ControlPlugin()); // 提供 3D 相机交互
renderer.registerPlugin(new ThreeDPlugin()); // 提供 3D Geometry、Material、光照等

const canvas = new Canvas({
  container: document.createElement('div'),
  width,
  height,
  renderer,
});

const camera = canvas.getCamera();
camera.setType(CameraType.ORBITING);

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

乍一看略显繁琐,主要是光照配置和 G2 没太大关系,我们是否有必要把光照也加到 Spec 里?如果需要的话可能长这样:

lights: [
  { type: 'directional', intensity: 3, direction: [-1, 0, 1] },
  { type: 'point', intensity: 3, position: [-1, 0, 1] },
]

自定义 Chart 扩展 threedlib

import { Runtime, corelib, threedlib, extend } from '@antv/g2';
const Chart = extend(Runtime, { ...corelib(), ...threedlib() });

Spec 用法

depth 默认为 640 和 height 相同。

export function bodyPointScatterPlot3D(): G2Spec {
  return {
    type: 'point3D',
    padding: 'auto',
    data: {
      type: 'fetch',
      value: 'data/cars2.csv',
    },
    encode: {
      x: 'Horsepower',
      y: 'Miles_per_Gallon',
      z: 'Weight_in_lbs',
      size: 'Origin',
      color: 'Cylinders',
      shape: 'cube', // 目前 point3D 支持 sphere | cube
    },
    scale: {
      x: { nice: true },
      y: { nice: true },
      z: { nice: true }, // 增加 Z
    },
    coordinate: { type: 'cartesian3D' }, // 使用 Cartesian3D
    axis: {
      x: { gridLineWidth: 3 },
      y: { gridLineWidth: 3 },
      z: { gridLineWidth: 3 },
    },
    // legend: false, // 可以暂时禁用 legend
  };
}

新增 threedlib 包含:

export function threedlib() {
  return {
    'coordinate.cartesian3D': Cartesian3D,
    'component.axisZ': AxisZ,
    'mark.point3D': Point3D,
  } as const;
}

其中 point3D 暂时支持 Sphere 和 Cube 两种 shape:

截屏2023-08-16 下午1 41 35 截屏2023-08-16 下午2 02 24

后续改进

Chart API

chart.axisZ();

Legend

Legend 不应该受相机影响,在 3D 场景中类似 HUD,有趣的是 Three.js 也建议使用 HTML 处理这类需求:
mrdoob/three.js#225

  • Highcharts 也使用了 HTML:
截屏2023-08-17 上午10 14 40

3D辅助线

hover / click 节点展示辅助线,例如 plot.ly

截屏2023-08-16 下午6 14 29

透视投影

可以参考 chartRender3dScatterPlotPerspective 这个例子,设置相机透视投影:

const camera = canvas.getCamera();
camera.setPerspective(0.1, 5000, 45, 500 / 500);

平面背景色

支持配置 x-z / x-y / y-z 平面的背景色,下面是 highcharts 的效果:

截屏2023-08-17 上午10 31 57

Bugs

@coveralls

This comment was marked as off-topic.

@xiaoiver xiaoiver requested review from pearmini and hustcc August 16, 2023 12:24
src/mark/point3D.ts Outdated Show resolved Hide resolved
src/mark/point3D.ts Outdated Show resolved Hide resolved
src/mark/point3D.ts Show resolved Hide resolved
@xiaoiver xiaoiver requested a review from pearmini August 17, 2023 07:29
site/docs/api/chart.zh.md Outdated Show resolved Hide resolved
src/lib/std.ts Outdated Show resolved Hide resolved
@xiaoiver xiaoiver merged commit 85abdc3 into v5 Aug 18, 2023
@xiaoiver xiaoiver deleted the feat-3d branch August 18, 2023 05:38
@pearmini pearmini mentioned this pull request Sep 19, 2023
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants