Skip to content

Commit 5b6b7f3

Browse files
committed
fix: dont render legend container on empty items
1 parent baeb4e7 commit 5b6b7f3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pages/03-core/empty-line.page.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import Highcharts from "highcharts";
5+
import { omit } from "lodash";
6+
7+
import CoreChart from "../../lib/components/internal-do-not-use/core-chart";
8+
import { PageSettingsForm, useChartSettings } from "../common/page-settings";
9+
import { Page } from "../common/templates";
10+
11+
export default function () {
12+
const { chartProps } = useChartSettings();
13+
return (
14+
<Page
15+
title="Core chart demo"
16+
subtitle="The page demonstrates the use of the core chart, including additional legend settings."
17+
settings={
18+
<PageSettingsForm
19+
selectedSettings={["showLegend", "legendPosition", "showLegendTitle", "showLegendActions", "useFallback"]}
20+
/>
21+
}
22+
>
23+
<CoreChart
24+
{...omit(chartProps.cartesian, "ref")}
25+
chartHeight={400}
26+
highcharts={Highcharts}
27+
options={{
28+
xAxis: {
29+
min: 1,
30+
max: 50,
31+
},
32+
yAxis: {
33+
min: 0,
34+
max: 1,
35+
},
36+
series: [
37+
{
38+
data: [],
39+
type: "line",
40+
showInLegend: false,
41+
},
42+
],
43+
}}
44+
/>
45+
</Page>
46+
);
47+
}

src/core/chart-core.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Spinner from "@cloudscape-design/components/spinner";
1313
import { getDataAttributes } from "../internal/base-component/get-data-attributes";
1414
import { InternalBaseComponentProps } from "../internal/base-component/use-base-component";
1515
import * as Styles from "../internal/chart-styles";
16+
import { useSelector } from "../internal/utils/async-store";
1617
import { castArray } from "../internal/utils/utils";
1718
import { useChartAPI } from "./chart-api";
1819
import { ChartContainer } from "./chart-container";
@@ -84,6 +85,7 @@ export function InternalCoreChart({
8485
const mergedRootRef = useMergeRefs(rootRef, __internalRootRef);
8586
const rootProps = { ref: mergedRootRef, className: rootClassName, ...getDataAttributes(rest) };
8687
const legendPosition = legendOptions?.position ?? "bottom";
88+
const emptyLegendItems = useSelector(api.legendStore, (s) => s.items.length === 0);
8789
const containerProps = {
8890
fitHeight,
8991
chartHeight,
@@ -302,7 +304,7 @@ export function InternalCoreChart({
302304
}}
303305
navigator={navigator}
304306
legend={
305-
context.legendEnabled ? (
307+
context.legendEnabled && !emptyLegendItems ? (
306308
<ChartLegend
307309
{...legendOptions}
308310
position={legendPosition}

0 commit comments

Comments
 (0)