Skip to content

Commit 6ba9dc9

Browse files
committed
ChartSettingsPanel: Add UI for legend position
1 parent 43bcdb6 commit 6ba9dc9

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

packages/components/src/internal/components/chart/ChartSettingsPanel.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useEnterEscape } from '../../../public/useEnterEscape';
2121
import { ChartLabelInput } from './ChartLabelInput';
2222
import { ChartColorInputs } from './ChartColorInputs';
2323
import { Alert } from '../base/Alert';
24+
import { RadioGroupInput } from '../forms/input/RadioGroupInput';
2425

2526
function changedIntValue(strVal: string, currentVal: number): [value: number, changed: boolean] {
2627
strVal = strVal.trim();
@@ -279,6 +280,7 @@ export const ChartSettingsPanel: FC<Props> = memo(props => {
279280
setChartConfig,
280281
setChartModel,
281282
} = props;
283+
const legendPos = chartConfig.legendPos;
282284
const showTrendline = hasTrendline(chartType);
283285
const fields = chartType.fields.filter(f => f.name !== 'trendline');
284286

@@ -326,6 +328,20 @@ export const ChartSettingsPanel: FC<Props> = memo(props => {
326328
[setChartConfig]
327329
);
328330

331+
const legendOptions = useMemo(() => {
332+
return [
333+
{ label: 'Right', selected: !legendPos || legendPos === 'right', value: 'right' },
334+
{ label: 'Bottom', selected: legendPos === 'bottom', value: 'bottom' },
335+
];
336+
}, [legendPos]);
337+
338+
const onLegendPosChange = useCallback(
339+
value => setChartConfig(current => ({ ...current, legendPos: value })),
340+
[setChartConfig]
341+
);
342+
343+
const showLegendPos = chartType.name !== 'pie_chart';
344+
329345
return (
330346
<div className="chart-settings">
331347
{error && <Alert>{error}</Alert>}
@@ -392,6 +408,22 @@ export const ChartSettingsPanel: FC<Props> = memo(props => {
392408
value={chartConfig?.labels?.subtitle}
393409
/>
394410
<SizeInputs height={chartConfig.height} setChartConfig={setChartConfig} width={chartConfig.width} />
411+
412+
{showLegendPos && (
413+
<div className="chart-settings__legend-pos">
414+
<label>Legend Position</label>
415+
416+
<div className="chart-settings__legend-pos-values">
417+
<RadioGroupInput
418+
formsy={false}
419+
name="legendPos"
420+
onValueChange={onLegendPosChange}
421+
options={legendOptions}
422+
/>
423+
</div>
424+
</div>
425+
)}
426+
395427
<ChartColorInputs chartConfig={chartConfig} model={model} setChartConfig={setChartConfig} />
396428
</div>
397429
);

packages/components/src/internal/components/chart/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ChartConfig {
1818
gridLinesVisible: string;
1919
height?: number;
2020
labels: ChartLabels;
21+
legendPos?: 'bottom' | 'right';
2122
measures: Record<string, Record<string, any>>; // TODO: we can probably do better than any
2223
measuresOptions?: Record<string, Record<string, MeasureOption>>; // map from measures to the options for the distinct values of that measure
2324
pointType: string;

packages/components/src/theme/charts.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@
224224
margin: 0 0 8px;
225225
}
226226

227+
.chart-settings__legend-pos-values {
228+
display: flex;
229+
gap: 8px;
230+
}
231+
232+
.chart-settings__legend-pos-values .radio-input-wrapper input {
233+
margin-right: 4px;
234+
}
235+
236+
.chart-settings__legend-pos-values .radioinput-label {
237+
cursor: pointer;
238+
}
239+
227240
.chart-builder-modal__chart-preview {
228241
flex: 3;
229242
overflow: auto;

0 commit comments

Comments
 (0)