Skip to content
GRAPHIC WALKER
API 参考
PureRenderer

PureRenderer

PureRenderer 根据可视化规格渲染单个只读图表。它具有最小化的 UI — 没有工具栏、没有图表标签页、没有字段列表。这使其非常适合在仪表板、文章或任何需要静态可视化的场景中嵌入图表。

import { PureRenderer } from '@kanaries/graphic-walker';

使用场景

组件可交互工具栏多图表字段列表
GraphicWalker完整编辑
GraphicRenderer仅查看
PureRenderer仅查看

当你需要以最小的占用空间展示图表时,使用 PureRenderer

属性

本地模式(客户端数据)

属性类型必填说明
rawDataIRow[]数据记录数组。
type'local'显式设置为本地模式(默认)。

远程模式(服务端数据)

属性类型必填说明
computationIComputationFunction用于服务端处理的计算函数。
type'remote'必须设置为 'remote'

可视化状态

属性类型必填说明
visualStateDraggableFieldState字段编码(行、列、颜色、大小等)。
visualConfigIVisualConfigNew可视化配置(聚合、几何类型、坐标系)。
visualLayoutIVisualLayout布局配置(大小、堆叠模式、格式化)。未提供时回退到 visualConfig

外观属性

属性类型默认值说明
appearance'media' | 'light' | 'dark''media'暗色模式设置。
vizThemeConfigIThemeKey | GWGlobalConfigundefined图表主题。
uiThemeIUIThemeConfigundefined自定义 UI 颜色。
overrideSize{ mode: 'auto' | 'fixed' | 'full'; width: number; height: number }undefined覆盖规格中的大小。
scalesIChannelScalesundefined自定义比例尺配置。

其他属性

属性类型说明
namestring图表名称(用于导出)。
classNamestring根元素的 CSS 类名。
localestring数字/日期格式化的语言环境。默认为 'en-US'
disableCollapseboolean禁用可折叠图表容器。

示例

本地模式

import { PureRenderer } from '@kanaries/graphic-walker';
import type { DraggableFieldState, IVisualConfigNew, IVisualLayout } from '@kanaries/graphic-walker';
 
const data = [
  { category: 'A', value: 100 },
  { category: 'B', value: 200 },
  { category: 'C', value: 150 },
];
 
const visualState: DraggableFieldState = {
  dimensions: [],
  measures: [],
  rows: [{ fid: 'value', name: 'Value', analyticType: 'measure', semanticType: 'quantitative' }],
  columns: [{ fid: 'category', name: 'Category', analyticType: 'dimension', semanticType: 'nominal' }],
  color: [],
  opacity: [],
  size: [],
  shape: [],
  theta: [],
  radius: [],
  longitude: [],
  latitude: [],
  geoId: [],
  details: [],
  filters: [],
  text: [],
};
 
const visualConfig: IVisualConfigNew = {
  defaultAggregated: true,
  geoms: ['bar'],
  coordSystem: 'generic',
  limit: -1,
};
 
const visualLayout: IVisualLayout = {
  showTableSummary: false,
  stack: 'stack',
  showActions: false,
  interactiveScale: false,
  zeroScale: true,
  size: { mode: 'auto', width: 600, height: 400 },
  format: {},
  resolve: {},
};
 
function ChartEmbed() {
  return (
    <PureRenderer
      rawData={data}
      visualState={visualState}
      visualConfig={visualConfig}
      visualLayout={visualLayout}
    />
  );
}

远程模式

import { PureRenderer } from '@kanaries/graphic-walker';
import type { IComputationFunction } from '@kanaries/graphic-walker';
 
const computation: IComputationFunction = async (payload) => {
  const res = await fetch('/api/query', {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: { 'Content-Type': 'application/json' },
  });
  return res.json();
};
 
function RemoteChart() {
  return (
    <PureRenderer
      type="remote"
      computation={computation}
      visualState={visualState}
      visualConfig={visualConfig}
      visualLayout={visualLayout}
    />
  );
}

固定大小

<PureRenderer
  rawData={data}
  visualState={visualState}
  visualConfig={visualConfig}
  overrideSize={{ mode: 'fixed', width: 400, height: 300 }}
/>

从 GraphicWalker 提取状态

你可以通过 storeRefGraphicWalker 实例获取 visualStatevisualConfigvisualLayout,然后将它们传给 PureRenderer

import { useRef } from 'react';
import { GraphicWalker, PureRenderer } from '@kanaries/graphic-walker';
import type { VizSpecStore } from '@kanaries/graphic-walker';
 
function App() {
  const storeRef = useRef<VizSpecStore>(null);
 
  const handleExport = () => {
    if (!storeRef.current) return;
    const chart = storeRef.current.exportCurrentChart();
    // chart contains encodings, config, layout
    // Pass these to PureRenderer elsewhere
  };
 
  return <GraphicWalker data={data} fields={fields} storeRef={storeRef} />;
}