An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
feat: add geoList
islxyqwe308557d4cc66
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsxindex 3b1840cf..83f4d10b 100644--- a/packages/graphic-walker/src/App.tsx+++ b/packages/graphic-walker/src/App.tsx@@ -1,7 +1,7 @@import React, { useEffect, useRef, useMemo, useState, useCallback } from 'react';import { observer } from 'mobx-react-lite';import { useTranslation } from 'react-i18next';-import { IGeographicData, IComputationFunction, IDarkMode, IMutField, IRow, ISegmentKey, IThemeKey, Specification } from './interfaces';+import { IGeographicData, IComputationFunction, IDarkMode, IMutField, IRow, ISegmentKey, IThemeKey, Specification, IGeoDataItem } from './interfaces';import type { IReactVegaHandler } from './vis/react-vega';import VisualSettings from './visualSettings';import PosFields from './fields/posFields';@@ -67,6 +67,7 @@ export interface IGWProps {};computationTimeout?: number;onError?: (err: Error) => void;+ geoList?: IGeoDataItem[];}const App = observer<IGWProps>(function App(props) {@@ -213,7 +214,7 @@ const App = observer<IGWProps>(function App(props) {<Errorpanel /><LogPanel /><BinPanel/>- {commonStore.showGeoJSONConfigPanel && <GeoConfigPanel />}+ {commonStore.showGeoJSONConfigPanel && <GeoConfigPanel geoList={props.geoList} />}<div className="md:grid md:grid-cols-12 xl:grid-cols-6"><div className="md:col-span-3 xl:col-span-1"><DatasetFields />diff --git a/packages/graphic-walker/src/components/leafletRenderer/geoConfigPanel.tsx b/packages/graphic-walker/src/components/leafletRenderer/geoConfigPanel.tsxindex faf26b34..0eac141e 100644--- a/packages/graphic-walker/src/components/leafletRenderer/geoConfigPanel.tsx+++ b/packages/graphic-walker/src/components/leafletRenderer/geoConfigPanel.tsx@@ -6,9 +6,12 @@ import { useGlobalStore } from '../../store';import Modal from '../modal';import PrimaryButton from '../button/primary';import DefaultButton from '../button/default';-import type { IGeoUrl, Topology } from '../../interfaces';+import type { IGeoDataItem, IGeoUrl, Topology } from '../../interfaces';+import DropdownSelect from '../dropdownSelect';-const GeoConfigPanel: React.FC = (props) => {+const emptyList = [];++const GeoConfigPanel = ({ geoList = emptyList }: { geoList?: IGeoDataItem[] }) => {const { commonStore, vizStore } = useGlobalStore();const { showGeoJSONConfigPanel } = commonStore;const { visualConfig } = vizStore;@@ -25,6 +28,32 @@ const GeoConfigPanel: React.FC = (props) => {const [loadedUrl, setLoadedUrl] = useState<IGeoUrl | undefined>(geoUrl);const [loading, setLoading] = useState(false);+ const hasCustomData = url || (dataMode === 'GeoJSON' && geoJSON) || (dataMode === 'TopoJSON' && topoJSON);++ const [selectItem, setSelectItemR] = useState(() => {+ const i = geoList.findIndex((x) => x.url === geoUrl?.url && x.type === geoUrl?.type);+ if (i === -1 && hasCustomData) {+ return -2;+ }+ return i;+ });++ const options = useMemo(+ () =>+ [{ label: 'Select a Geographic Data', value: '-1' }]+ .concat(+ geoList.map((x, i) => ({+ label: x.name,+ value: `${i}`,+ }))+ )+ .concat({ label: 'Manual Set', value: '-2' }),+ [geoList]+ );+ const setSelectItem = useMemo(() => (a: string) => setSelectItemR(parseInt(a)), []);++ const isCustom = (geoList ?? []).length === 0 || selectItem === -2;+const defaultTopoJSONKey = useMemo(() => {try {const value = JSON.parse(topoJSON) as Topology;@@ -43,6 +72,18 @@ const GeoConfigPanel: React.FC = (props) => {}, [geojson]);const handleSubmit = () => {+ if (!isCustom) {+ const item = geoList[selectItem];+ if (!item) {+ } else {+ vizStore.setGeographicUrl({+ type: item.type,+ url: item.url,+ });+ }+ commonStore.setShowGeoJSONConfigPanel(false);+ return;+ }try {const json = JSON.parse(dataMode === 'GeoJSON' ? geoJSON : topoJSON);if (dataMode === 'TopoJSON') {@@ -72,7 +113,7 @@ const GeoConfigPanel: React.FC = (props) => {};return (- <Modal show={showGeoJSONConfigPanel} onClose={() => commonStore.setShowGeoJSONConfigPanel(false)}>+ <Modal containerStyle={{ overflow: 'visible' }} show={showGeoJSONConfigPanel} onClose={() => commonStore.setShowGeoJSONConfigPanel(false)}><div><h2 className="text-lg mb-4">{t('geography')}</h2><div>@@ -87,101 +128,109 @@ const GeoConfigPanel: React.FC = (props) => {/></div></div>- <div className="my-2">- <label className="block text-xs font-medium leading-6 text-gray-900">{t(`geography_settings.${dataMode.toLowerCase()}`)}</label>- <div className="mt-1 flex flex-col space-y-2">- <div role="radiogroup">+ {geoList.length > 0 && (+ <div className="my-2">+ <label className="block text-xs font-medium leading-6 text-gray-900">GeoData</label>+ <DropdownSelect options={options} selectedKey={`${selectItem}`} onSelect={setSelectItem} />+ </div>+ )}+ {isCustom && (+ <div className="my-2">+ <label className="block text-xs font-medium leading-6 text-gray-900">{t(`geography_settings.${dataMode.toLowerCase()}`)}</label>+ <div className="mt-1 flex flex-col space-y-2">+ <div role="radiogroup">+ <div className="flex items-center space-x-2">+ <input+ type="radio"+ name="dataMode"+ id="geojson"+ className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 dark:bg-gray-800 dark:border-gray-600"+ checked={dataMode === 'GeoJSON'}+ onChange={() => {+ setDataMode('GeoJSON');+ }}+ />+ <label htmlFor="geojson" className="text-xs whitespace-nowrap">+ {t('geography_settings.geojson')}+ </label>+ <input+ type="radio"+ name="dataMode"+ id="topojson"+ className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 dark:bg-gray-800 dark:border-gray-600"+ checked={dataMode === 'TopoJSON'}+ onChange={() => {+ setDataMode('TopoJSON');+ }}+ />+ <label htmlFor="topojson" className="text-xs whitespace-nowrap">+ {t('geography_settings.topojson')}+ </label>+ </div>+ </div><div className="flex items-center space-x-2">+ <label className="text-xs whitespace-nowrap capitalize">+ {t('geography_settings.href', { format: dataMode.toLowerCase() })}+ </label><input- type="radio"- name="dataMode"- id="geojson"- className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 dark:bg-gray-800 dark:border-gray-600"- checked={dataMode === 'GeoJSON'}- onChange={() => {- setDataMode('GeoJSON');+ type="text"+ className="block w-full rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"+ value={url}+ placeholder={t('geography_settings.hrefPlaceholder', { format: dataMode.toLowerCase() })}+ onChange={(e) => {+ setUrl(e.target.value);}}/>- <label htmlFor="geojson" className="text-xs whitespace-nowrap">- {t('geography_settings.geojson')}- </label>- <input- type="radio"- name="dataMode"- id="topojson"- className="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 dark:bg-gray-800 dark:border-gray-600"- checked={dataMode === 'TopoJSON'}- onChange={() => {- setDataMode('TopoJSON');+ <DefaultButton+ text={t('geography_settings.load')}+ className="mr-2"+ disabled={loading}+ icon={loading ? <Spinner className="text-black" /> : undefined}+ onClick={() => {+ if (url) {+ setLoading(true);+ fetch(url)+ .then((res) => res.json())+ .then((json) => {+ (dataMode === 'GeoJSON' ? setGeoJSON : setTopoJSON)(JSON.stringify(json, null, 2));+ setLoadedUrl({ type: dataMode, url });+ setLoading(false);+ })+ .catch(() => {+ setLoading(false);+ });+ }}}/>- <label htmlFor="topojson" className="text-xs whitespace-nowrap">- {t('geography_settings.topojson')}- </label></div>- </div>- <div className="flex items-center space-x-2">- <label className="text-xs whitespace-nowrap capitalize">- {t('geography_settings.href', { format: dataMode.toLowerCase() })}- </label>- <input- type="text"- className="block w-full rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"- value={url}- placeholder={t('geography_settings.hrefPlaceholder', { format: dataMode.toLowerCase() })}+ <textarea+ className="block w-full h-40 rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 resize-none"+ value={dataMode === 'GeoJSON' ? geoJSON : topoJSON}+ placeholder={t('geography_settings.jsonInputPlaceholder', { format: dataMode.toLowerCase() })}onChange={(e) => {- setUrl(e.target.value);- }}- />- <DefaultButton- text={t('geography_settings.load')}- className="mr-2"- disabled={loading}- icon={loading ? <Spinner className='text-black' /> : undefined}- onClick={() => {- if (url) {- setLoading(true);- fetch(url)- .then((res) => res.json())- .then((json) => {- (dataMode === 'GeoJSON' ? setGeoJSON : setTopoJSON)(JSON.stringify(json, null, 2));- setLoadedUrl({ type: dataMode, url });- setLoading(false);- })- .catch(() => {- setLoading(false);- });+ (dataMode === 'GeoJSON' ? setGeoJSON : setTopoJSON)(e.target.value);+ if (loadedUrl?.type === dataMode) {+ setLoadedUrl(undefined);}}}/>+ {dataMode === 'TopoJSON' && (+ <div className="flex items-center space-x-2">+ <label className="text-xs whitespace-nowrap capitalize">{t('geography_settings.objectKey')}</label>+ <input+ type="text"+ className="block w-full rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"+ value={topoJSONKey}+ placeholder={defaultTopoJSONKey}+ onChange={(e) => {+ setTopoJSONKey(e.target.value);+ }}+ />+ </div>+ )}</div>- <textarea- className="block w-full h-40 rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 resize-none"- value={dataMode === 'GeoJSON' ? geoJSON : topoJSON}- placeholder={t('geography_settings.jsonInputPlaceholder', { format: dataMode.toLowerCase() })}- onChange={(e) => {- (dataMode === 'GeoJSON' ? setGeoJSON : setTopoJSON)(e.target.value);- if (loadedUrl?.type === dataMode) {- setLoadedUrl(undefined);- }- }}- />- {dataMode === 'TopoJSON' && (- <div className="flex items-center space-x-2">- <label className="text-xs whitespace-nowrap capitalize">{t('geography_settings.objectKey')}</label>- <input- type="text"- className="block w-full rounded-md border-0 py-1 px-2 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-1 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"- value={topoJSONKey}- placeholder={defaultTopoJSONKey}- onChange={(e) => {- setTopoJSONKey(e.target.value);- }}- />- </div>- )}</div>- </div>+ )}</div><div className="mt-4"><PrimaryButton text={tGlobal('actions.confirm')} className="mr-2" onClick={handleSubmit} />diff --git a/packages/graphic-walker/src/components/modal.tsx b/packages/graphic-walker/src/components/modal.tsxindex a8b3bd74..c1bbf0fc 100644--- a/packages/graphic-walker/src/components/modal.tsx+++ b/packages/graphic-walker/src/components/modal.tsx@@ -40,6 +40,7 @@ interface ModalProps {onClose?: () => void;show?: boolean;title?: string;+ containerStyle?: React.CSSProperties;}const Modal: React.FC<ModalProps> = (props) => {const { onClose, title, show } = props;@@ -60,7 +61,7 @@ const Modal: React.FC<ModalProps> = (props) => {}}}>- <Container role="dialog" className="bg-white dark:bg-zinc-900 shadow-lg rounded-md border border-gray-100 dark:border-gray-800" onMouseDown={(e) => e.stopPropagation()}>+ <Container style={props.containerStyle} role="dialog" className="bg-white dark:bg-zinc-900 shadow-lg rounded-md border border-gray-100 dark:border-gray-800" onMouseDown={(e) => e.stopPropagation()}><div className="absolute top-0 right-0 hidden pt-4 pr-4 sm:block"><buttontype="button"diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.tsindex 6c235ba9..a9bf4153 100644--- a/packages/graphic-walker/src/interfaces.ts+++ b/packages/graphic-walker/src/interfaces.ts@@ -498,3 +498,9 @@ export type IGeographicData = (objectKey?: string;});++export type IGeoDataItem = {+ type: 'GeoJSON' | 'TopoJSON';+ name: string;+ url: string;+}\ No newline at end of filediff --git a/packages/graphic-walker/src/main.tsx b/packages/graphic-walker/src/main.tsxindex cff68018..16bfb91f 100644--- a/packages/graphic-walker/src/main.tsx+++ b/packages/graphic-walker/src/main.tsx@@ -5,4 +5,13 @@ if (!import.meta.env.DEV) {inject();}-embedGraphicWalker(document.getElementById('root') as HTMLElement, {})\ No newline at end of file+embedGraphicWalker(document.getElementById('root') as HTMLElement, {+ geoList: [+ { name: 'World Countries', type: 'TopoJSON', url: 'https://cdn.jsdelivr.net/npm/world-atlas@2/countries-10m.json' },+ {+ name: 'World Cities',+ type: 'GeoJSON',+ url: 'https://raw.githubusercontent.com/drei01/geojson-world-cities/f2a988af4bc15463df55586afbbffbd3068b7218/cities.geojson',+ },+ ],+});diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.tsindex 1decade3..9d61cdb8 100644--- a/packages/graphic-walker/src/store/visualSpecStore.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.ts@@ -860,6 +860,12 @@ export class VizSpecStore {this.importStoInfo(content);}+ public setGeographicUrl(geoUrl: IGeoUrl) {+ this.useMutable(({ config }) => {+ config.geoUrl = geoUrl;+ });+ }+public setGeographicData(data: IGeographicData, geoKey: string, geoUrl?: IGeoUrl) {const geoJSON =data.type === 'GeoJSON' ? data.data : (feature(data.data, data.objectKey || Object.keys(data.data.objects)[0]) as unknown as FeatureCollection);