Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add svg renderer

islxyqwe08cdb163b638
diff --git a/packages/graphic-walker/src/components/visualConfig/index.tsx b/packages/graphic-walker/src/components/visualConfig/index.tsx
index bda4f5cf..626f4b71 100644
--- a/packages/graphic-walker/src/components/visualConfig/index.tsx
+++ b/packages/graphic-walker/src/components/visualConfig/index.tsx
@@ -36,6 +36,7 @@ const VisualConfigPanel: React.FC = (props) => {
size: visualConfig.resolve.size,
});
const [zeroScale, setZeroScale] = useState<boolean>(visualConfig.zeroScale);
+ const [svg, setSvg] = useState<boolean>(visualConfig.useSvg ?? false);
const [scaleIncludeUnmatchedChoropleth, setScaleIncludeUnmatchedChoropleth] = useState<boolean>(visualConfig.scaleIncludeUnmatchedChoropleth ?? false);
const [background, setBackground] = useState<string | undefined>(visualConfig.background);
@@ -84,9 +85,11 @@ const VisualConfigPanel: React.FC = (props) => {
</div>
</div>
))}
- <hr className='my-4' />
+ <hr className="my-4" />
<div className="my-2">
- <label className="block text-xs font-medium leading-6">{t('config.background')} {t(`config.color`)}</label>
+ <label className="block text-xs font-medium leading-6">
+ {t('config.background')} {t(`config.color`)}
+ </label>
<div className="mt-1">
<input
type="text"
@@ -139,6 +142,13 @@ const VisualConfigPanel: React.FC = (props) => {
setZeroScale(en);
}}
/>
+ <Toggle
+ label={t(`config.svg`)}
+ enabled={svg}
+ onChange={(en) => {
+ setSvg(en);
+ }}
+ />
{isChoropleth && (
<Toggle
label="include unmatched choropleth in scale"
@@ -161,6 +171,7 @@ const VisualConfigPanel: React.FC = (props) => {
vizStore.setVisualConfig('scaleIncludeUnmatchedChoropleth', scaleIncludeUnmatchedChoropleth);
vizStore.setVisualConfig('background', background);
vizStore.setVisualConfig('resolve', resolve);
+ vizStore.setVisualConfig('useSvg', svg);
commonStore.setShowVisualConfigPanel(false);
});
}}
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index beeb5299..d6984194 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -239,6 +239,7 @@ export interface IVisualConfig {
/** @default false */
scaleIncludeUnmatchedChoropleth?: boolean;
background?: string;
+ useSvg?: boolean;
format: {
numberFormat?: string;
timeFormat?: string;
diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.json
index e3302245..77b4c0d4 100644
--- a/packages/graphic-walker/src/locales/en-US.json
+++ b/packages/graphic-walker/src/locales/en-US.json
@@ -12,7 +12,8 @@
"zeroScale": "Zero Scale",
"misc": "Misc",
"formatGuidesDocs": "Format guides docs",
- "readHere": "read here"
+ "readHere": "read here",
+ "svg": "Use SVG"
},
"constant": {
"row_count": "Row count",
diff --git a/packages/graphic-walker/src/locales/ja-JP.json b/packages/graphic-walker/src/locales/ja-JP.json
index f9acfef4..892547d1 100644
--- a/packages/graphic-walker/src/locales/ja-JP.json
+++ b/packages/graphic-walker/src/locales/ja-JP.json
@@ -12,7 +12,8 @@
"zeroScale": "0を含む",
"misc": "その他",
"formatGuidesDocs": "フォーマットガイド",
- "readHere": "ここを読む"
+ "readHere": "ここを読む",
+ "svg": "SVGを使う"
},
"constant": {
"row_count": "行数",
diff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.json
index da946a39..f47f3e05 100644
--- a/packages/graphic-walker/src/locales/zh-CN.json
+++ b/packages/graphic-walker/src/locales/zh-CN.json
@@ -12,7 +12,8 @@
"zeroScale": "零刻度",
"misc": "其他",
"formatGuidesDocs": "格式指南",
- "readHere": "阅读此处"
+ "readHere": "阅读此处",
+ "svg": "使用SVG"
},
"constant": {
"row_count": "行数",
diff --git a/packages/graphic-walker/src/renderer/specRenderer.tsx b/packages/graphic-walker/src/renderer/specRenderer.tsx
index c0b02619..c51f8975 100644
--- a/packages/graphic-walker/src/renderer/specRenderer.tsx
+++ b/packages/graphic-walker/src/renderer/specRenderer.tsx
@@ -34,7 +34,7 @@ const SpecRenderer = forwardRef<IReactVegaHandler, SpecRendererProps>(function (
ref
) {
// const { draggableFieldState, visualConfig } = vizStore;
- const { geoms, coordSystem = 'generic', interactiveScale, defaultAggregated, stack, showActions, size, format: _format, background, zeroScale, resolve } = visualConfig;
+ const { geoms, coordSystem = 'generic', interactiveScale, defaultAggregated, stack, showActions, size, format: _format, background, zeroScale, resolve, useSvg } = visualConfig;
const rows = draggableFieldState.rows;
const columns = draggableFieldState.columns;
@@ -186,6 +186,7 @@ const SpecRenderer = forwardRef<IReactVegaHandler, SpecRendererProps>(function (
ref={ref}
onGeomClick={onGeomClick}
locale={locale}
+ useSvg={useSvg}
/>
)}
</Resizable>
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 0e0fef28..1609fc80 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -385,7 +385,7 @@ export class VizSpecStore {
public setVisualConfig<K extends keyof IVisualConfig>(configKey: K, value: IVisualConfig[K]) {
this.useMutable(({ config }) => {
switch (true) {
- case ['defaultAggregated', 'defaultStack', 'showActions', 'interactiveScale', 'scaleIncludeUnmatchedChoropleth'].includes(configKey): {
+ case ['defaultAggregated', 'defaultStack', 'showActions', 'interactiveScale', 'scaleIncludeUnmatchedChoropleth', 'useSvg'].includes(configKey): {
return ((config as unknown as { [k: string]: boolean })[configKey] = Boolean(value));
}
case configKey === 'geoms' && Array.isArray(value):
diff --git a/packages/graphic-walker/src/vis/react-vega.tsx b/packages/graphic-walker/src/vis/react-vega.tsx
index b98164ca..7d4a0ded 100644
--- a/packages/graphic-walker/src/vis/react-vega.tsx
+++ b/packages/graphic-walker/src/vis/react-vega.tsx
@@ -49,6 +49,7 @@ interface ReactVegaProps {
vegaConfig: VegaGlobalConfig;
/** @default "en-US" */
locale?: string;
+ useSvg?: boolean;
}
const click$ = new Subject<ScenegraphEvent>();
@@ -102,6 +103,7 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
vegaConfig,
// format
locale = 'en-US',
+ useSvg
} = props;
const [viewPlaceholders, setViewPlaceholders] = useState<React.MutableRefObject<HTMLDivElement>[]>([]);
// const mediaTheme = useCurrentMediaTheme(dark);
@@ -237,7 +239,7 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
}
if (viewPlaceholders.length > 0 && viewPlaceholders[0].current) {
- const task = embed(viewPlaceholders[0].current, spec, { mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
+ const task = embed(viewPlaceholders[0].current, spec, { renderer: useSvg ? 'svg' : 'canvas', mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
const container = res.view.container();
const canvas = container?.querySelector('canvas') ?? null;
vegaRefs.current = [{
@@ -316,7 +318,7 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
}
if (node) {
const id = index;
- const task = embed(node, ans, { mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
+ const task = embed(node, ans, { renderer: useSvg ? 'svg' : 'canvas', mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(locale), config: vegaConfig }).then(res => {
const container = res.view.container();
const canvas = container?.querySelector('canvas') ?? null;
vegaRefs.current[id] = {
@@ -421,7 +423,8 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe
height,
vegaConfig,
details,
- text
+ text,
+ useSvg
]);
const containerRef = useRef<HTMLDivElement>(null);