An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
fix: temporal locales and format
ObservedObserver1d917133cabf
diff --git a/packages/graphic-walker/src/dataSource/table.tsx b/packages/graphic-walker/src/dataSource/table.tsxindex 229b289a..51a81a01 100644--- a/packages/graphic-walker/src/dataSource/table.tsx+++ b/packages/graphic-walker/src/dataSource/table.tsx@@ -36,16 +36,8 @@ const Container = styled.div`}}`;-const TYPE_LIST = [- {- value: "dimension",- label: "维度",- },- {- value: "measure",- label: "度量",- },-];+const ANALYTIC_TYPE_LIST = ["dimension", "measure"];+const SEMANTIC_TYPE_LIST = ["nominal", "ordinal", "quantitative", "temporal"];// function getCellType(field: IMutField): 'number' | 'text' {// return field.dataType === 'number' || field.dataType === 'integer' ? 'number' : 'text';// }@@ -79,9 +71,16 @@ const Table: React.FC<TableProps> = (props) => {const { t } = useTranslation();const analyticTypeList = useMemo<{ value: string; label: string }[]>(() => {- return TYPE_LIST.map((at) => ({- value: at.value,- label: t(`constant.analytic_type.${at.value}`),+ return ANALYTIC_TYPE_LIST.map((at) => ({+ value: at,+ label: t(`constant.analytic_type.${at}`),+ }));+ }, []);++ const semanticTypeList = useMemo<{ value: string; label: string }[]>(() => {+ return SEMANTIC_TYPE_LIST.map((st) => ({+ value: st,+ label: t(`constant.semantic_type.${st}`),}));}, []);@@ -121,13 +120,27 @@ const Table: React.FC<TableProps> = (props) => {))}</select></div>- <div- className={- "inline-block px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs text-white " +- getSemanticColors(field)- }- >- {field.semanticType}+ <div>+ <select+ className={+ "inline-block px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs text-white " ++ getSemanticColors(field)+ }+ // className="border-b border-gray-200 bg-gray-50 pl-0 mt-2 font-light"+ value={field.semanticType}+ onChange={(e) => {+ commonStore.updateTempFieldSemanticType(+ field.fid,+ e.target.value as IMutField["semanticType"]+ );+ }}+ >+ {semanticTypeList.map((type) => (+ <option key={type.value} value={type.value}>+ {type.label}+ </option>+ ))}+ </select></div></div></th>diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.jsonindex f9fc7fc9..3160dfb4 100644--- a/packages/graphic-walker/src/locales/en-US.json+++ b/packages/graphic-walker/src/locales/en-US.json@@ -5,6 +5,12 @@"dimension": "dimension","measure": "measure"},+ "semantic_type": {+ "nominal": "nominal",+ "ordinal": "ordinal",+ "temporal": "temporal",+ "quantitative": "quantitative"+ },"mark_type": {"__enum__": "Mark Type","auto": "Auto",diff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.jsonindex cffb7ea6..7c656038 100644--- a/packages/graphic-walker/src/locales/zh-CN.json+++ b/packages/graphic-walker/src/locales/zh-CN.json@@ -5,6 +5,12 @@"dimension": "维度","measure": "度量"},+ "semantic_type": {+ "nominal": "类别类型",+ "ordinal": "有序类型",+ "temporal": "时间类型",+ "quantitative": "数值类型"+ },"mark_type": {"__enum__": "标记类型","auto": "自动",diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.tsindex db74cf22..f31c5506 100644--- a/packages/graphic-walker/src/store/commonStore.ts+++ b/packages/graphic-walker/src/store/commonStore.ts@@ -73,6 +73,13 @@ export class CommonStore {}}+ public updateTempFieldSemanticType (fieldKey: string, semanticType: IMutField['semanticType']) {+ const field = this.tmpDSRawFields.find(f => f.fid === fieldKey);+ if (field) {+ field.semanticType = semanticType;+ }+ }+public updateTempName (name: string) {this.tmpDSName = name;}diff --git a/packages/graphic-walker/src/utils/dataPrep.ts b/packages/graphic-walker/src/utils/dataPrep.tsindex 8b7792a3..15f5953d 100644--- a/packages/graphic-walker/src/utils/dataPrep.ts+++ b/packages/graphic-walker/src/utils/dataPrep.ts@@ -36,7 +36,6 @@ export function guardDataKeys (data: IRow[], metas: IMutField[]): {from: metas[i].fid,to: f.fid}));- console.log(keyEncodeList, metas, safeMetas)const safeData = updateRowKeys(data, keyEncodeList);return {safeData,diff --git a/packages/graphic-walker/src/vis/react-vega.tsx b/packages/graphic-walker/src/vis/react-vega.tsxindex c40c3319..273aa206 100644--- a/packages/graphic-walker/src/vis/react-vega.tsx+++ b/packages/graphic-walker/src/vis/react-vega.tsx@@ -9,6 +9,8 @@ import { autoMark } from '../utils/autoMark';import { COUNT_FIELD_ID } from '../constants';import { IViewField, IRow, IStackMode } from '../interfaces';+import { useTranslation } from 'react-i18next';+import { getVegaTimeFormatRules } from './temporalFormat';const CanvaContainer = styled.div<{rowSize: number; colSize: number;}>`display: grid;@@ -119,12 +121,9 @@ function channelEncode(props: EncodeProps) {title: props[c].name,type: props[c].semanticType}- if (props[c].semanticType === 'temporal') {- encoding[c].axis = { format: '%Y-%m' }- }}})- // FIXME: 临时处理逻辑,只处理xy排序+ // FIXME: temporal fix, only for x and y relative orderif (encoding.x && encoding.y) {if ((props.x.sort && props.x.sort) || (props.y && props.y.sort)) {if (props.x.sort !== 'none' && (props.y.sort === 'none' || !Boolean(props.y.sort))) {@@ -383,6 +382,7 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe// const container = useRef<HTMLDivElement>(null);// const containers = useRef<(HTMLDivElement | null)[]>([]);const [viewPlaceholders, setViewPlaceholders] = useState<React.MutableRefObject<HTMLDivElement>[]>([]);+ const { i18n } = useTranslation();useEffect(() => {const clickSub = geomClick$.subscribe(([values, e]) => {if (onGeomClick) {@@ -502,7 +502,7 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVespec.params.push(...singleView.params!);}if (viewPlaceholders.length > 0 && viewPlaceholders[0].current) {- embed(viewPlaceholders[0].current, spec, { mode: 'vega-lite', actions: showActions }).then(res => {+ embed(viewPlaceholders[0].current, spec, { mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(i18n.language) }).then(res => {vegaRefs.current = [res.view];try {res.view.addEventListener('click', (e) => {@@ -583,10 +583,8 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVeans.params = commonSpec.params;}if (node) {- embed(node, ans, { mode: 'vega-lite', actions: showActions }).then(res => {+ embed(node, ans, { mode: 'vega-lite', actions: showActions, timeFormatLocale: getVegaTimeFormatRules(i18n.language) }).then(res => {vegaRefs.current.push(res.view);- // 这种 case 下,我们来考虑联动的 params- // vega 使用 Data 来维护 params 的状态,只需要打通这些状态就可以实现联动const paramStores = (res.vgSpec.data?.map(d => d.name) ?? []).filter(name => [BRUSH_SIGNAL_NAME, POINT_SIGNAL_NAME].map(p => `${p}_store`).includes(name)).map(name => name.replace(/_store$/, ''));@@ -611,12 +609,10 @@ const ReactVega = forwardRef<IReactVegaHandler, ReactVegaProps>(function ReactVe});}});- // 订阅subscribe(entry => {if (entry.source === sourceId || !entry.data) {return;}- // 防止被动更新触发广播noBroadcasting = true;res.view.setState({data: {diff --git a/packages/graphic-walker/src/vis/temporalFormat.ts b/packages/graphic-walker/src/vis/temporalFormat.tsnew file mode 100644index 00000000..14a90314--- /dev/null+++ b/packages/graphic-walker/src/vis/temporalFormat.ts@@ -0,0 +1,66 @@+export function getVegaTimeFormatRules(lang: string) {+ switch (lang) {+ case 'zh-CN':+ return {+ dateTime: '%x %A %X',+ date: '%Y年%-m月%-d日',+ time: '%H:%M:%S',+ periods: ['上午', '下午'],+ days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],+ shortDays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],+ months: [+ '一月',+ '二月',+ '三月',+ '四月',+ '五月',+ '六月',+ '七月',+ '八月',+ '九月',+ '十月',+ '十一月',+ '十二月',+ ],+ shortMonths: [+ '一月',+ '二月',+ '三月',+ '四月',+ '五月',+ '六月',+ '七月',+ '八月',+ '九月',+ '十月',+ '十一月',+ '十二月',+ ],+ };+ case 'en-US':+ default:+ return {+ dateTime: '%x, %X',+ date: '%-m/%-d/%Y',+ time: '%-I:%M:%S %p',+ periods: ['AM', 'PM'],+ days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],+ shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],+ months: [+ 'January',+ 'February',+ 'March',+ 'April',+ 'May',+ 'June',+ 'July',+ 'August',+ 'September',+ 'October',+ 'November',+ 'December',+ ],+ shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],+ };+ }+}\ No newline at end of file