An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
chore: format bin with d3format (#356)
islxyqwe3c65f5fa1b78
diff --git a/packages/graphic-walker/src/lib/execExp.ts b/packages/graphic-walker/src/lib/execExp.tsindex 36442b2c..d0f5d3e4 100644--- a/packages/graphic-walker/src/lib/execExp.ts+++ b/packages/graphic-walker/src/lib/execExp.ts@@ -68,18 +68,13 @@ function bin(resKey: string, params: IExpParameter[], data: IDataFrame, binSize:if (val < _min) _min = val;}const step = (_max - _min) / binSize;- // prevent (_max - _min) to be 0- const safeWidth = Math.min(Number.MAX_SAFE_INTEGER, Math.max(_max - _min, Number.MIN_VALUE));- const beaStep = Math.max(-Math.round(Math.log10(safeWidth)) + 2, 0);- // toFix() accepts 0-100- const safeBeaStep = Math.min(100, Math.max(0, Math.max(Number.isFinite(beaStep) ? beaStep : 0, 0)));const newValues = fieldValues.map((v: number) => {let bIndex = Math.floor((v - _min) / step);if (bIndex === binSize) bIndex = binSize - 1;if (Number.isNaN(bIndex)) {bIndex = 0;}- return [Number((bIndex * step + _min).toFixed(safeBeaStep)), Number(((bIndex + 1) * step + _min).toFixed(safeBeaStep))];+ return [Number((bIndex * step + _min)), Number(((bIndex + 1) * step + _min))];});return {...data,diff --git a/packages/graphic-walker/src/lib/vega.ts b/packages/graphic-walker/src/lib/vega.tsindex 7135dd93..b22ee8b9 100644--- a/packages/graphic-walker/src/lib/vega.ts+++ b/packages/graphic-walker/src/lib/vega.ts@@ -127,6 +127,7 @@ export function toVegaSpec({geomType,displayOffset,dataSource,+ vegaConfig});const singleView = scales ? resolveScales(scales, v, dataSource, mediaTheme) : v;diff --git a/packages/graphic-walker/src/renderer/specRenderer.tsx b/packages/graphic-walker/src/renderer/specRenderer.tsxindex 4324055a..2a85652c 100644--- a/packages/graphic-walker/src/renderer/specRenderer.tsx+++ b/packages/graphic-walker/src/renderer/specRenderer.tsx@@ -95,6 +95,7 @@ const SpecRenderer = forwardRef<IReactVegaHandler, SpecRendererProps>(function (const config: VegaGlobalConfig = {...themeConfig,background: parseColorToHex(uiTheme[mediaTheme].background),+ customFormatTypes: true,};if (format.normalizedNumberFormat && format.normalizedNumberFormat.length > 0) {// @ts-ignorediff --git a/packages/graphic-walker/src/vis/react-vega.tsx b/packages/graphic-walker/src/vis/react-vega.tsxindex 18093675..b1704972 100644--- a/packages/graphic-walker/src/vis/react-vega.tsx+++ b/packages/graphic-walker/src/vis/react-vega.tsx@@ -2,7 +2,7 @@ import React, { useEffect, useState, useMemo, forwardRef, useRef, useContext } fimport embed from 'vega-embed';import { Subject, Subscription } from 'rxjs';import * as op from 'rxjs/operators';-import type { ScenegraphEvent } from 'vega';+import { expressionFunction, type ScenegraphEvent } from 'vega';import styled from 'styled-components';import { useVegaExportApi } from '../utils/vegaApiExport';import { IViewField, IRow, IStackMode, VegaGlobalConfig, IVegaChartRef, IChannelScales, IDarkMode, IConfigScale } from '../interfaces';@@ -13,6 +13,22 @@ import { toVegaSpec } from '../lib/vega';import { useResizeDetector } from 'react-resize-detector';import { startTask } from '../utils';import { themeContext } from '@/store/theme';+import { format } from 'd3-format';++expressionFunction('formatBin', (datum: [number, number] | number, formatString?: string) => {+ const formatter = formatString ? format(formatString) : (x) => x;+ // only append on the tooltip field+ if (!Array.isArray(datum)) {+ return formatter(datum);+ }+ const [min, max] = datum;+ const step = max - min;+ if (step === 0) {+ return `[${formatter(min)},${formatter(max)}]`;+ }+ const beaStep = Math.max(-Math.round(Math.log10(step)) + 2, 0);+ return `[${formatter(Number(min.toFixed(beaStep)))},${formatter(Number(max.toFixed(beaStep)))}]`;+});const CanvaContainer = styled.div<{ rowSize: number; colSize: number }>`display: grid;diff --git a/packages/graphic-walker/src/vis/spec/encode.ts b/packages/graphic-walker/src/vis/spec/encode.tsindex 716f96ec..a222b41a 100644--- a/packages/graphic-walker/src/vis/spec/encode.ts+++ b/packages/graphic-walker/src/vis/spec/encode.ts@@ -19,6 +19,7 @@ export interface IEncodeProps {details: Readonly<IViewField[]>;text: IViewField;displayOffset?: number;+ vegaConfig?: any;}export function availableChannels(geomType: string): Set<string> {if (geomType === 'text') {@@ -77,9 +78,13 @@ export function channelEncode(props: IEncodeProps) {encoding[c].bin = {binned: true,};+ encoding[c].formatType = 'formatBin';+ encoding[c].format = props.vegaConfig?.numberFormat;if (c === 'x' || c === 'y') {encoding[`${c}2`] = {field: `${fid}[1]`,+ formatType: 'formatBin',+ format: props.vegaConfig?.numberFormat,};}}diff --git a/packages/graphic-walker/src/vis/spec/tooltip.ts b/packages/graphic-walker/src/vis/spec/tooltip.tsindex 5e20e1b8..16640ad3 100644--- a/packages/graphic-walker/src/vis/spec/tooltip.ts+++ b/packages/graphic-walker/src/vis/spec/tooltip.ts@@ -1,25 +1,32 @@import { IViewField } from '../../interfaces';import { getMeaAggKey, getMeaAggName } from '../../utils';+import { produce } from 'immer';export function addTooltipEncode(encoding: { [key: string]: any }, details: Readonly<IViewField[]> = [], defaultAggregated = false) {const encs = Object.keys(encoding).filter((ck) => ck !== 'tooltip' && ck !== 'x2' && ck !== 'y2').map((ck) => {- return {- field: encoding[ck].field.replace('[0]', ''),- type: encoding[ck].type,- title: encoding[ck].title,- ...(encoding[ck].timeUnit- ? {- timeUnit: encoding[ck].timeUnit,- }- : {}),- ...(encoding[ck].scale- ? {- scale: encoding[ck].scale,- }- : {}),- };+ return produce(+ {+ field: encoding[ck].field.replace('[0]', ''),+ type: encoding[ck].type,+ title: encoding[ck].title,+ } as Record<string, any>,+ (draft) => {+ if (encoding[ck].timeUnit) {+ draft.timeUnit = encoding[ck].timeUnit;+ }+ if (encoding[ck].scale) {+ draft.scale = encoding[ck].scale;+ }+ if (encoding[ck].formatType) {+ draft.formatType = encoding[ck].formatType;+ }+ if (encoding[ck].format) {+ draft.format = encoding[ck].format;+ }+ }+ );}).concat(details.map((f) => ({diff --git a/packages/graphic-walker/src/vis/spec/view.ts b/packages/graphic-walker/src/vis/spec/view.tsindex 509205ea..ac3f6d6d 100644--- a/packages/graphic-walker/src/vis/spec/view.ts+++ b/packages/graphic-walker/src/vis/spec/view.ts@@ -45,6 +45,7 @@ export function getSingleView(props: SingleViewProps) {hideLegend = false,displayOffset,dataSource,+ vegaConfig,} = props;const fields: IViewField[] = [x, y, color, opacity, size, shape, row, column, xOffset, yOffset, theta, radius, text];let markType = geomType;@@ -120,6 +121,7 @@ export function getSingleView(props: SingleViewProps) {details,text,displayOffset,+ vegaConfig,});if (defaultAggregated) {channelAggregate(encoding, fields);