G2 柱状图
示例加载中…
import { Rect as RectGeometry } from '@antv/g';
import { renderToMountedElement, stdlib } from '@antv/g2';
import { ExtensionCategory, Graph, Rect, register } from '@antv/g6';
class BarChart extends Rect {
onCreate() {
const [width, height] = this.getSize();
const group = this.upsert(
'chart-container',
RectGeometry,
{
transform: `translate(${-width / 2}, ${-height / 2})`,
width,
height,
fill: '#fff',
stroke: '#697b8c',
radius: 10,
shadowColor: '#697b8c',
shadowBlur: 10,
shadowOffsetX: 5,
shadowOffsetY: 5,
},
this.shapeMap.key,
);
const { name, value } = this.attributes;
renderToMountedElement(
// @antv/g2 Specification
// https://g2.antv.antgroup.com/examples/general/interval/#column
{
width,
height,
data: { value },
title: name,
type: 'interval',
axis: {
x: { title: false },
y: { title: false },
},
scale: {
y: { domain: [0, 100] },
},
encode: {
x: 'subject',
y: 'score',
color: 'subject',
},
legend: { color: false },
},
{
group,
library: stdlib(),
},
);
}
}
register(ExtensionCategory.NODE, 'bar-chart', BarChart);
const graph = new Graph({
container: 'container',
data: {
nodes: [
{
id: 'Jack',
data: {
value: [
{ subject: 'Math', score: 95 },
{ subject: 'Chinese', score: 70 },
{ subject: 'English', score: 75 },
{ subject: 'Geography', score: 80 },
{ subject: 'Physics', score: 90 },
{ subject: 'Chemistry', score: 85 },
{ subject: 'Biology', score: 70 },
],
},
},
{
id: 'Aaron',
data: {
value: [
{ subject: 'Math', score: 70 },
{ subject: 'Chinese', score: 90 },
{ subject: 'English', score: 90 },
{ subject: 'Geography', score: 60 },
{ subject: 'Physics', score: 70 },
{ subject: 'Chemistry', score: 65 },
{ subject: 'Biology', score: 80 },
],
},
},
{
id: 'Rebecca',
data: {
value: [
{ subject: 'Math', score: 60 },
{ subject: 'Chinese', score: 95 },
{ subject: 'English', score: 100 },
{ subject: 'Geography', score: 80 },
{ subject: 'Physics', score: 60 },
{ subject: 'Chemistry', score: 90 },
{ subject: 'Biology', score: 85 },
],
},
},
],
},
node: {
type: 'bar-chart',
style: {
size: 250,
fillOpacity: 0,
name: (d) => d.id,
value: (d) => d.data.value,
},
},
layout: {
type: 'grid',
},
behaviors: ['drag-element'],
});
graph.render();
import { Graph } from '@antv/g6';
import GUI from 'lil-gui';
export function addPanel(renderPanel: (gui: GUI) => void) {
const gui = new GUI({ container: document.body });
gui.title('Control');
Object.assign(gui.domElement.style, { position: 'absolute', top: '0', right: '0', zIndex: '10' });
renderPanel(gui);
addEventListener('pagehide', () => gui.destroy(), { once: true });
}
export async function createGraph(
options: ConstructorParameters<typeof Graph>[0],
size: { width?: number; height?: number } = {},
renderPanel?: (gui: GUI, graph: Graph) => void,
) {
const container = document.createElement('div');
Object.assign(container.style, {
width: '100%',
maxWidth: `${size.width || 600}px`,
height: `${size.height || 400}px`,
});
document.getElementById('container')!.append(container);
const graph = new Graph({ ...size, ...options, width: container.clientWidth, container, autoResize: true });
addEventListener('pagehide', () => graph.destroy(), { once: true });
await graph.render();
if (renderPanel) addPanel((gui) => renderPanel(gui, graph));
return container;
}