Skip to content

Commit 4f0535a

Browse files
ryugarajRaj Jaiswal
andauthored
fix: filter empty series data (#89)
Co-authored-by: Raj Jaiswal <[email protected]>
1 parent 0a72feb commit 4f0535a

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/core/chart-api/chart-extra-context.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type Highcharts from "highcharts";
55

66
import { NonCancelableEventHandler } from "../../internal/events";
77
import { getChartSeries } from "../../internal/utils/chart-series";
8+
import { getSeriesData } from "../../internal/utils/series-data";
89
import { ChartLabels } from "../i18n-utils";
910
import { CoreChartProps, Rect } from "../interfaces";
1011
import { getGroupRect, isSeriesStacked } from "../utils";
@@ -99,13 +100,13 @@ function computeDerivedState(chart: Highcharts.Chart): ChartExtraContext.Derived
99100
for (const s of getChartSeries(chart.series)) {
100101
const seriesX = new Set<number>();
101102
if (s.visible) {
102-
for (const d of s.data) {
103+
for (const d of getSeriesData(s.data)) {
103104
// Points with y=null represent the absence of value, there is no need to include them and those
104105
// should have no impact on computed rects or navigation.
105106

106107
// Although "d" can't be undefined according to Highcharts API, it does become undefined for chart containing more datapoints
107108
// than the cropThreshold for that series (specific cases of re-rendering the chart with updated options listening to setExteme updates)
108-
if (d && d.visible && d.y !== null) {
109+
if (d.visible && d.y !== null) {
109110
seriesX.add(d.x);
110111
allXSet.add(d.x);
111112
addPoint(d);

src/core/chart-api/chart-extra-highlight.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import type Highcharts from "highcharts";
55

66
import * as Styles from "../../internal/chart-styles";
7+
import { getSeriesData } from "../../internal/utils/series-data";
78
import { getPointId, getSeriesId } from "../utils";
89
import { ChartExtraContext } from "./chart-extra-context";
910

@@ -140,7 +141,7 @@ export class ChartExtraHighlight {
140141
if (prevState === "inactive") {
141142
inactiveSeriesIds.add(getSeriesId(s));
142143
}
143-
for (const p of s.data) {
144+
for (const p of getSeriesData(s.data)) {
144145
const prevState = this.pointState.get(getSeriesId(s))?.get(this.getPointKey(p));
145146
if (prevState) {
146147
this.setPointState(p, prevState);
@@ -183,7 +184,7 @@ export class ChartExtraHighlight {
183184
(overridden as SeriesSetStateWithLock)[SET_STATE_LOCK] = true;
184185
s.setState = overridden;
185186
}
186-
for (const d of s.data) {
187+
for (const d of getSeriesData(s.data)) {
187188
if ((d.setState as PointSetStateWithLock)[SET_STATE_LOCK] === undefined) {
188189
const original = d.setState;
189190
// The overridden setState method does nothing unless setState[SET_STATE_LOCK] === false.

src/core/chart-api/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type Highcharts from "highcharts";
77
import { fireNonCancelableEvent } from "../../internal/events";
88
import { ReadonlyAsyncStore } from "../../internal/utils/async-store";
99
import { getChartSeries } from "../../internal/utils/chart-series";
10+
import { getSeriesData } from "../../internal/utils/series-data";
1011
import { Writeable } from "../../internal/utils/utils";
1112
import {
1213
getChartAccessibleDescription,
@@ -307,14 +308,15 @@ export class ChartAPI {
307308
private showMarkersForIsolatedPoints() {
308309
let shouldRedraw = false;
309310
for (const s of this.context.chart().series) {
310-
for (let i = 0; i < s.data.length; i++) {
311-
const isEligibleSeries = !isXThreshold(s) && s.type !== "scatter" && !s.data[i].options.marker?.enabled;
311+
const seriesData = getSeriesData(s.data);
312+
for (let i = 0; i < seriesData.length; i++) {
313+
const isEligibleSeries = !isXThreshold(s) && s.type !== "scatter" && !seriesData[i].options.marker?.enabled;
312314
if (
313315
isEligibleSeries &&
314-
(s.data[i - 1]?.y === undefined || s.data[i - 1]?.y === null) &&
315-
(s.data[i + 1]?.y === undefined || s.data[i + 1]?.y === null)
316+
(seriesData[i - 1]?.y === undefined || seriesData[i - 1]?.y === null) &&
317+
(seriesData[i + 1]?.y === undefined || seriesData[i + 1]?.y === null)
316318
) {
317-
s.data[i].update({ marker: { enabled: true } }, false);
319+
seriesData[i].update({ marker: { enabled: true } }, false);
318320
shouldRedraw = true;
319321
}
320322
}

src/internal/utils/series-data.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import type { Point } from "highcharts";
5+
6+
// Although series data can't be undefined according to Highcharts API, it does become undefined for chart containing more datapoints
7+
// than the cropThreshold for that series (specific cases of re-rendering the chart with updated options listening to setExteme updates)
8+
export const getSeriesData = (data: Array<Point>): Array<Point> => {
9+
return data.filter((d) => !!d);
10+
};

0 commit comments

Comments
 (0)