Skip to content

Commit c089318

Browse files
authored
Merge pull request #662 from adobe/s2VegaConfig
S2 Vega Config
2 parents fd5e6e5 + 2a72011 commit c089318

File tree

11 files changed

+204
-20
lines changed

11 files changed

+204
-20
lines changed

packages/react-spectrum-charts/src/RscChart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const RscChart = forwardRef<ChartHandle, RscChartProps>((props, forwarded
9292
useSpecProps(spec);
9393

9494
const { signals, targetStyle, tooltipOptions, onNewView } = useChartInteractions(props, sanitizedChildren);
95-
const chartConfig = useMemo(() => getChartConfig(config, colorScheme), [config, colorScheme]);
95+
const chartConfig = useMemo(() => getChartConfig(config, colorScheme, s2), [config, colorScheme, s2]);
9696

9797
useEffect(() => {
9898
const tooltipElement = document.getElementById('vg-tooltip-element');
@@ -118,6 +118,7 @@ export const RscChart = forwardRef<ChartHandle, RscChartProps>((props, forwarded
118118
style={targetStyle}
119119
/>
120120
<VegaChart
121+
s2={s2}
121122
spec={spec}
122123
config={chartConfig}
123124
data={data}

packages/react-spectrum-charts/src/VegaChart.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface VegaChartProps {
3535
renderer: Renderers;
3636
signals?: Record<string, unknown>;
3737
spec: Spec;
38+
s2: boolean;
3839
tooltip: TooltipOptions;
3940
width: number;
4041
}
@@ -50,6 +51,7 @@ export const VegaChart: FC<VegaChartProps> = ({
5051
renderer = 'svg',
5152
signals,
5253
spec,
54+
s2,
5355
tooltip,
5456
width,
5557
}) => {
@@ -90,6 +92,7 @@ export const VegaChart: FC<VegaChartProps> = ({
9092
}
9193
embed(containerRef.current, specCopy, {
9294
...getVegaEmbedOptions({
95+
s2,
9396
locale,
9497
height,
9598
width,

packages/react-spectrum-charts/src/stories/Chart.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import { createRef } from 'react';
1313

1414
import { FADE_FACTOR } from '@spectrum-charts/constants';
15-
import { s2SpectrumColors } from '@spectrum-charts/themes';
15+
import { spectrum2Colors } from '@spectrum-charts/themes';
1616
import { ChartHandle } from '@spectrum-charts/vega-spec-builder';
1717

1818
import { Chart } from '../Chart';
@@ -205,7 +205,7 @@ describe('Chart', () => {
205205
expect(chart).toBeInTheDocument();
206206
const bars = getAllMarksByGroupName(chart, 'bar0');
207207

208-
const colors = s2SpectrumColors.light;
208+
const colors = spectrum2Colors.light;
209209

210210
expect(bars[0].getAttribute('fill')).toEqual(colors['categorical-100']);
211211
expect(bars[1].getAttribute('fill')).toEqual(colors['categorical-200']);
@@ -220,7 +220,7 @@ describe('Chart', () => {
220220
expect(chart).toBeInTheDocument();
221221
const bars = getAllMarksByGroupName(chart, 'bar0');
222222

223-
const colors = s2SpectrumColors.light;
223+
const colors = spectrum2Colors.light;
224224

225225
expect(bars[0].getAttribute('fill')).toEqual(colors['cinnamon-1200']);
226226
expect(bars[1].getAttribute('fill')).toEqual(colors['cinnamon-1000']);
@@ -235,7 +235,7 @@ describe('Chart', () => {
235235
expect(chart).toBeInTheDocument();
236236
const bars = getAllMarksByGroupName(chart, 'bar0');
237237

238-
const colors = s2SpectrumColors.dark;
238+
const colors = spectrum2Colors.dark;
239239

240240
expect(bars[0].getAttribute('fill')).toEqual(colors['cinnamon-1200']);
241241
expect(bars[1].getAttribute('fill')).toEqual(colors['cinnamon-1000']);

packages/themes/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './colorSchemes';
1515
export * from './divergingColorPalette';
1616
export * from './sequentialColorPalette';
1717
export * from './spectrumColors';
18-
export * from './s2SpectrumColors';
18+
export * from './spectrum2Colors';
1919
export * from './spectrumTheme';
20+
export * from './spectrum2Theme';
2021
export * from './utils';

packages/themes/src/s2SpectrumColors.ts renamed to packages/themes/src/spectrum2Colors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
export const s2SpectrumColors = {
13+
export const spectrum2Colors = {
1414
light: {
1515
'gray-25': 'white',
1616
'gray-50': '#F8F8F8',
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2025 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
import { BaseLegendLayout, Config } from 'vega';
13+
14+
import {
15+
DEFAULT_BACKGROUND_COLOR,
16+
DEFAULT_FONT_COLOR,
17+
DEFAULT_FONT_SIZE,
18+
DEFAULT_LEGEND_COLUMN_PADDING,
19+
DEFAULT_LEGEND_LABEL_LIMIT,
20+
DEFAULT_LEGEND_SYMBOL_SIZE,
21+
DEFAULT_SYMBOL_SIZE,
22+
DEFAULT_SYMBOL_STROKE_WIDTH,
23+
ROUNDED_SQUARE_PATH,
24+
} from '@spectrum-charts/constants';
25+
26+
import { categorical16 } from './categoricalColorPalette';
27+
import { divergentOrangeYellowSeafoam15 } from './divergingColorPalette';
28+
import { sequentialViridis16 } from './sequentialColorPalette';
29+
import { spectrum2Colors } from './spectrum2Colors';
30+
import { ADOBE_CLEAN_FONT } from './spectrumTheme';
31+
import { getS2ColorValue } from './utils';
32+
33+
export function getSpectrum2VegaConfig(colorScheme: 'light' | 'dark'): Config {
34+
const FONT_COLOR = getS2ColorValue(DEFAULT_FONT_COLOR, colorScheme);
35+
const {
36+
'blue-400': blue400,
37+
'gray-300': gray300,
38+
'gray-700': gray700,
39+
'gray-800': gray800,
40+
} = spectrum2Colors[colorScheme];
41+
const horizontalLegendLayout: BaseLegendLayout = {
42+
anchor: 'middle',
43+
direction: 'horizontal',
44+
center: true,
45+
offset: 24,
46+
bounds: 'full',
47+
margin: 48,
48+
};
49+
const verticalLegendLayout: BaseLegendLayout = {
50+
anchor: 'middle',
51+
direction: 'vertical',
52+
center: false,
53+
offset: 24,
54+
bounds: 'full',
55+
margin: 24,
56+
};
57+
58+
const defaultColor = spectrum2Colors[colorScheme]['categorical-100'];
59+
60+
return {
61+
axis: {
62+
bandPosition: 0.5,
63+
domain: false,
64+
domainWidth: 1,
65+
domainColor: gray800,
66+
gridColor: gray300,
67+
labelFont: ADOBE_CLEAN_FONT,
68+
labelFontSize: DEFAULT_FONT_SIZE,
69+
labelFontWeight: 'normal',
70+
labelPadding: 8,
71+
labelOverlap: true,
72+
labelColor: FONT_COLOR,
73+
ticks: false,
74+
tickColor: gray300,
75+
tickRound: true,
76+
tickSize: 8,
77+
tickCap: 'round',
78+
tickWidth: 1,
79+
titleAnchor: 'middle',
80+
titleColor: FONT_COLOR,
81+
titleFont: ADOBE_CLEAN_FONT,
82+
titleFontSize: DEFAULT_FONT_SIZE,
83+
titleFontWeight: 'bold',
84+
titlePadding: 16,
85+
},
86+
range: {
87+
category: categorical16,
88+
diverging: divergentOrangeYellowSeafoam15,
89+
ordinal: categorical16,
90+
ramp: sequentialViridis16,
91+
},
92+
background: DEFAULT_BACKGROUND_COLOR,
93+
legend: {
94+
columnPadding: DEFAULT_LEGEND_COLUMN_PADDING,
95+
labelColor: FONT_COLOR,
96+
labelFont: ADOBE_CLEAN_FONT,
97+
labelFontSize: DEFAULT_FONT_SIZE,
98+
labelFontWeight: 'normal',
99+
labelLimit: DEFAULT_LEGEND_LABEL_LIMIT,
100+
layout: {
101+
bottom: horizontalLegendLayout,
102+
top: horizontalLegendLayout,
103+
left: verticalLegendLayout,
104+
right: verticalLegendLayout,
105+
},
106+
rowPadding: 8,
107+
symbolSize: DEFAULT_LEGEND_SYMBOL_SIZE,
108+
symbolType: ROUNDED_SQUARE_PATH,
109+
symbolStrokeColor: gray700,
110+
titleColor: FONT_COLOR,
111+
titleFont: ADOBE_CLEAN_FONT,
112+
titleFontSize: DEFAULT_FONT_SIZE,
113+
titlePadding: 8,
114+
},
115+
arc: {
116+
fill: defaultColor,
117+
},
118+
area: {
119+
fill: defaultColor,
120+
opacity: 0.8,
121+
},
122+
line: {
123+
strokeWidth: 1.5,
124+
stroke: defaultColor,
125+
},
126+
path: {
127+
stroke: defaultColor,
128+
},
129+
rect: {
130+
strokeWidth: 0,
131+
stroke: blue400,
132+
fill: defaultColor,
133+
},
134+
rule: {
135+
stroke: gray800,
136+
strokeWidth: 1,
137+
},
138+
shape: {
139+
stroke: defaultColor,
140+
},
141+
symbol: {
142+
strokeWidth: DEFAULT_SYMBOL_STROKE_WIDTH,
143+
size: DEFAULT_SYMBOL_SIZE,
144+
fill: defaultColor,
145+
},
146+
text: {
147+
fill: FONT_COLOR,
148+
font: ADOBE_CLEAN_FONT,
149+
fontSize: DEFAULT_FONT_SIZE,
150+
},
151+
title: {
152+
offset: 10,
153+
font: ADOBE_CLEAN_FONT,
154+
fontSize: 18,
155+
color: FONT_COLOR,
156+
},
157+
autosize: {
158+
type: 'fit',
159+
contains: 'padding',
160+
resize: true,
161+
},
162+
};
163+
}

packages/themes/src/spectrumColors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { s2SpectrumColors } from './s2SpectrumColors';
12+
import { spectrum2Colors } from './spectrum2Colors';
1313

14-
const { light: s2LightColors, dark: s2DarkColors } = s2SpectrumColors;
14+
const { light: s2LightColors, dark: s2DarkColors } = spectrum2Colors;
1515

1616
// add 's2-' as a prefix to all s2 light colors
1717
const s2LightColorsWithPrefix = Object.fromEntries(

packages/themes/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { s2SpectrumColors } from './s2SpectrumColors';
12+
import { spectrum2Colors } from './spectrum2Colors';
1313
import { spectrumColors } from './spectrumColors';
1414

1515
/**
@@ -29,5 +29,5 @@ export const getColorValue = (color: string, colorScheme: 'light' | 'dark'): str
2929
* @returns css color string
3030
*/
3131
export const getS2ColorValue = (color: string, colorScheme: 'light' | 'dark'): string => {
32-
return s2SpectrumColors[colorScheme][color] || color;
32+
return spectrum2Colors[colorScheme][color] || color;
3333
};

packages/vega-spec-builder/src/specUtils.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ import {
2121
ROUNDED_SQUARE_PATH,
2222
TABLE,
2323
} from '@spectrum-charts/constants';
24-
import { getSpectrumVegaConfig } from '@spectrum-charts/themes';
24+
import {
25+
getSpectrum2VegaConfig,
26+
getSpectrumVegaConfig,
27+
spectrum2Colors,
28+
spectrumColors,
29+
} from '@spectrum-charts/themes';
2530

2631
import {
2732
addUserMetaInteractiveMark,
@@ -191,10 +196,19 @@ describe('getD3FormatSpecifierFromNumberFormat()', () => {
191196

192197
describe('getChartConfig()', () => {
193198
test('should merge the provided config with the Spectrum Vega config', () => {
194-
expect(getChartConfig(undefined, 'light')).toEqual(getSpectrumVegaConfig('light'));
195-
const mergedConfig = getChartConfig({ axis: { labelFontSize: 12 } }, 'light');
196-
expect(mergedConfig.axis).toHaveProperty('labelFontSize', 12);
197-
expect(mergedConfig.axis).toHaveProperty('labelFont');
199+
const s1colors = spectrumColors.light;
200+
201+
expect(getChartConfig(undefined, 'light', false)).toEqual(getSpectrumVegaConfig('light'));
202+
const mergedConfig = getChartConfig({ axis: { labelFontSize: 12 } }, 'light', false);
203+
expect(mergedConfig.axis).toHaveProperty('domainWidth', 2);
204+
expect(mergedConfig.axis).toHaveProperty('domainColor', s1colors['gray-900']);
205+
});
206+
test('should use the Spectrum 2 Vega config if s2 is true', () => {
207+
const s2colors = spectrum2Colors.light;
208+
expect(getChartConfig(undefined, 'light', true)).toEqual(getSpectrum2VegaConfig('light'));
209+
const mergedConfig = getChartConfig({ axis: { labelFontSize: 12 } }, 'light', true);
210+
expect(mergedConfig.axis).toHaveProperty('domainWidth', 1);
211+
expect(mergedConfig.axis).toHaveProperty('domainColor', s2colors['gray-800']);
198212
});
199213
});
200214

packages/vega-spec-builder/src/specUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
SENTIMENT_POSITIVE_PATH,
2727
TABLE,
2828
} from '@spectrum-charts/constants';
29-
import { getColorValue, getSpectrumVegaConfig } from '@spectrum-charts/themes';
29+
import { getColorValue, getSpectrum2VegaConfig, getSpectrumVegaConfig } from '@spectrum-charts/themes';
3030

3131
import {
3232
ChartSpecOptions,
@@ -298,8 +298,8 @@ export const getD3FormatSpecifierFromNumberFormat = (numberFormat: NumberFormat
298298
* @param colorScheme
299299
* @returns Vega config
300300
*/
301-
export function getChartConfig(config: Config | undefined, colorScheme: ColorScheme): Config {
302-
const defaultConfig = getSpectrumVegaConfig(colorScheme);
301+
export function getChartConfig(config: Config | undefined, colorScheme: ColorScheme, s2: boolean): Config {
302+
const defaultConfig = s2 ? getSpectrum2VegaConfig(colorScheme) : getSpectrumVegaConfig(colorScheme);
303303
if (config) {
304304
return mergeConfig(defaultConfig, config);
305305
}

0 commit comments

Comments
 (0)