-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ava/advisor): separate visual encodingfrom spec generator mo…
…dule
- Loading branch information
Showing
21 changed files
with
724 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 25 additions & 59 deletions
84
packages/ava/src/advisor/advise-pipeline/plugin/presets/spec-generator/charts/column.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,50 @@ | ||
import { Data } from '../../../../../../common/types'; | ||
import { Advice, BasicDataPropertyForAdvice } from '../../../../../types'; | ||
import { compare, hasSubset } from '../../../../../utils'; | ||
import { splitColumnXYSeries } from '../../visual-encoder/split-fields'; | ||
import { columnEncodeRequirement } from '../../../../../../ckb/encode'; | ||
import { mapFieldsToVisualEncode } from '../../visual-encoder/encode-mapping'; | ||
|
||
export function columnChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] { | ||
const nominalFields = dataProps.filter((field) => hasSubset(field.levelOfMeasurements, ['Nominal'])); | ||
const sortedNominalFields = nominalFields.sort(compare); | ||
const field4X = sortedNominalFields[0]; | ||
const field4Color = sortedNominalFields[1]; | ||
const field4Y = dataProps.find((field) => hasSubset(field.levelOfMeasurements, ['Interval'])); | ||
import type { Advice } from '../../../../../types'; | ||
import type { GenerateChartSpecParams } from '../types'; | ||
|
||
export function columnChart({ data, dataProps, encode: customEncode }: GenerateChartSpecParams): Advice['spec'] { | ||
const encode = | ||
customEncode ?? mapFieldsToVisualEncode({ fields: dataProps, encodeRequirements: columnEncodeRequirement }); | ||
const [field4X, field4Y, field4Color] = [encode.x?.[0], encode.y?.[0], encode.color?.[0]]; | ||
if (!field4X || !field4Y) return null; | ||
|
||
const spec: Advice['spec'] = { | ||
type: 'interval', | ||
data, | ||
encode: { | ||
x: field4X.name, | ||
y: field4Y.name, | ||
x: field4X, | ||
y: field4Y, | ||
}, | ||
}; | ||
|
||
if (field4Color) { | ||
spec.encode.color = field4Color.name; | ||
spec.encode.color = field4Color; | ||
spec.transform = [{ type: 'stackY' }]; | ||
} | ||
|
||
return spec; | ||
} | ||
|
||
export function groupedColumnChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] { | ||
const [field4X, field4Y, field4Series] = splitColumnXYSeries(dataProps); | ||
if (!field4X || !field4Y || !field4Series) return null; | ||
|
||
const spec: Advice['spec'] = { | ||
type: 'interval', | ||
data, | ||
encode: { | ||
x: field4X.name, | ||
y: field4Y.name, | ||
color: field4Series.name, | ||
}, | ||
transform: [{ type: 'dodgeX' }], | ||
}; | ||
|
||
export function groupedColumnChart({ data, dataProps, encode }: GenerateChartSpecParams): Advice['spec'] { | ||
const spec = columnChart({ data, dataProps, encode }); | ||
if (spec?.encode?.color) { | ||
spec.transform = [{ type: 'dodgeX' }]; | ||
} | ||
return spec; | ||
} | ||
|
||
export function stackedColumnChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] { | ||
const [field4X, field4Y, Field4Series] = splitColumnXYSeries(dataProps); | ||
if (!field4X || !field4Y || !Field4Series) return null; | ||
|
||
const spec: Advice['spec'] = { | ||
type: 'interval', | ||
data, | ||
encode: { | ||
x: field4X.name, | ||
y: field4Y.name, | ||
color: Field4Series.name, | ||
}, | ||
transform: [{ type: 'stackY' }], | ||
}; | ||
|
||
return spec; | ||
export function stackedColumnChart({ data, dataProps, encode }: GenerateChartSpecParams): Advice['spec'] { | ||
return columnChart({ data, dataProps, encode }); | ||
} | ||
|
||
export function percentStackedColumnChart(data: Data, dataProps: BasicDataPropertyForAdvice[]): Advice['spec'] { | ||
const [field4X, field4Y, Field4Series] = splitColumnXYSeries(dataProps); | ||
if (!field4X || !field4Y || !Field4Series) return null; | ||
|
||
const spec: Advice['spec'] = { | ||
type: 'interval', | ||
data, | ||
encode: { | ||
x: field4X.name, | ||
y: field4Y.name, | ||
color: Field4Series.name, | ||
}, | ||
transform: [{ type: 'stackY' }, { type: 'normalizeY' }], | ||
}; | ||
|
||
export function percentStackedColumnChart({ data, dataProps, encode }: GenerateChartSpecParams): Advice['spec'] { | ||
const spec = columnChart({ data, dataProps, encode }); | ||
if (spec?.transform) { | ||
spec.transform.push({ type: 'normalizeY' }); | ||
} else { | ||
spec.transform = [{ type: 'normalizeY' }]; | ||
} | ||
return spec; | ||
} |
25 changes: 13 additions & 12 deletions
25
packages/ava/src/advisor/advise-pipeline/plugin/presets/spec-generator/charts/heatmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 9 additions & 6 deletions
15
packages/ava/src/advisor/advise-pipeline/plugin/presets/spec-generator/charts/histogram.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.