We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
G2 除了能把 options 渲染成一个真实的 Canvas 或者 SVG 元素之外,还可以把图表渲染进一个 g 中的 DisplayObject。
// 底层的使用方式 import { Canvas, Group } from '@antv/g'; import { Renderer } from '@antv/g-canvas'; import { renderToMountedElement } from 'runtime'; // 创建一个 group 并且挂载 const canvas = new Canvas({ container: 'app', renderer: new Renderer() }); const g = new Group(); canvas.append(g); // 把图表渲染进这个挂载好的 group const context = { group: g }; renderToMountedElement(options);
// 上层的使用方式 import { Canvas, Group } from '@antv/g'; import { Renderer } from '@antv/g-canvas'; import { Chart } from '@antv/g2'; // 创建一个 group 并且挂载 const canvas = new Canvas({ container: 'app', renderer: new Renderer() }); const g = new Group(); canvas.append(g); // 把图表渲染进这个挂载好的 group const chart = new Chart({ container: g }); // 声明可视化 .... chart.render();
renderToMountedElement
render
import { Group } from '@anvt/g'; const context = { group: new Group() }; // 第一次渲染 renderToMountedElement(options, context); // 第二次渲染 renderToMountedElement(newOptions, context);
这里新增 renderToMountedElement 而不是把该能力放入 render 里面的原因是:runtime 里面的方法是比较底层的,所以希望每个方法更干净一点,不要有这种分支判断。就像 React 里面的 render 和 renderToString 一样。上层 API 合成一个就好(对用户友好)。
chart.render
为了减少 CR 成本,请遵循以下的基本规范:
// Primitive // 根据上下文命名即可,不需要突出类型 const count = 1; const name = 'jim'; // Array const fruits = ['apple', 'orange', 'pear']; // 复数 const X = [1, 2, 3]; // 首字母大写(`X`) const matrix = [[1, 2, 3], [4, 5, 6,]]; // 特定语意 // Object // 单数形式 const scale = { x: new ScaleLinear(), y: new ScaleOrdinal() } // Set 或者 WeakSet,同 Array const marks = new Set(); // Map 或者 WeakMap // 根据 id 获得名字 const idName = new Map(); // 两个单词:第一个是 key,第二个是 value // 特殊语意 const circle = new Circle(); const store = new Map(); store.set(circle, { fill: 'red' });
// Bad let a; if (b === 1) { a = 1; } else if (b === 2) { a = 2; } else { a = 3 } // Good function getA() { if (b === 1) { return 1; } else if (b === 2) { return 2; } else { return 3; } } const a = getA();
// Bad import { map } from '@antv/util'; const A = [1, 2, 3]; const B = map(a, d => d * 2); // Good const A = [1, 2, 3]; const B = A.map(d => d * 2);
The text was updated successfully, but these errors were encountered:
现在 Lottie 播放器设计的 API 也是类似的,把 Lottie 渲染到一个已挂载的 Group(或者任意元素)上:
import { loadAnimation } from '@antv/g-lottie-player'; // 解析 Lottie 文件 const ballAnimation = loadAnimation(bouncy_ball_json, { loop: true }); canvas.addEventListener(CanvasEvent.READY, () => { // 提供一个已挂载的元素 const wrapper = new Group(); canvas.appendChild(wrapper); // 将 Lottie 渲染到该元素上 ballAnimation.renderToMountedDisplayObject(wrapper); ballAnimation.pause(); // 进行其它变换,例如平移 wrapper.translate(100, 100); });
Sorry, something went wrong.
嗯嗯,直接叫 renderToGroup 确实不太准确,但我感觉 renderToMountedDisplayObject 会不会太长了?不过这个名字很准确!
renderToGroup
renderToMountedDisplayObject
xingwanying
No branches or pull requests
渲染进 @antv/g Group 元素
G2 除了能把 options 渲染成一个真实的 Canvas 或者 SVG 元素之外,还可以把图表渲染进一个 g 中的 DisplayObject。
开始使用
设计
renderToMountedElement
方法,该方法和render
有以下两点不同:这里新增 renderToMountedElement 而不是把该能力放入 render 里面的原因是:runtime 里面的方法是比较底层的,所以希望每个方法更干净一点,不要有这种分支判断。就像 React 里面的 render 和 renderToString 一样。上层 API 合成一个就好(对用户友好)。
chart.render
调用 runtime 的renderToMountedElement
方法;否者原有逻辑。任务
renderToMountedElement
方法基本规范
为了减少 CR 成本,请遵循以下的基本规范:
The text was updated successfully, but these errors were encountered: