Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Highcharts plugin): fix legend item click #142

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import Highcharts from 'highcharts';

type LegendItemClickType = 'extended' | 'default';

const getSeriesIdentifier = (series: Highcharts.Series): string => {
return (series.userOptions.id as string | undefined) || series.name;
const getSeriesIdentifier = (item: Highcharts.Series | Highcharts.Point): string => {
if (item instanceof Highcharts.Point) {
return item.name;
}
return (item.userOptions.id as string | undefined) || item.name;
};

const needSetVisible = (
seriesName: string,
seriesVisible: boolean,
chartSeries: Highcharts.Series[],
chartSeries: Highcharts.Series[] | Highcharts.Point[],
) => {
if (!seriesVisible) {
return false;
}

const hasAnotherVisibleSeries = chartSeries
// TypeScript have problems when filter/map/reduce with union type arrays.
// https://github.com/microsoft/TypeScript/issues/44373
const hasAnotherVisibleSeries = (chartSeries as (Highcharts.Point | Highcharts.Series)[])
.filter(
(series) =>
(series: Highcharts.Series | Highcharts.Point) =>
series.options.showInLegend !== false && getSeriesIdentifier(series) !== seriesName,
)
.some((series) => series.visible);
Expand All @@ -28,15 +33,16 @@ const needSetVisible = (
};

const updateSeries = (
series: Highcharts.Series,
chartSeries: Highcharts.Series[],
series: Highcharts.Series | Highcharts.Point,
chartSeries: Highcharts.Series[] | Highcharts.Point[],
type: LegendItemClickType,
) => {
const clickedSeriesName = getSeriesIdentifier(series);
switch (type) {
case 'extended': {
chartSeries.forEach((item) => {
chartSeries.forEach((item: Highcharts.Series | Highcharts.Point) => {
if (getSeriesIdentifier(item) === clickedSeriesName) {
// @ts-ignore
item.setVisible(!item.visible, false);
}
});
Expand All @@ -51,10 +57,12 @@ const updateSeries = (
chartSeries,
);

chartSeries.forEach((item) => {
chartSeries.forEach((item: Highcharts.Series | Highcharts.Point) => {
if (getSeriesIdentifier(item) === clickedSeriesName) {
// @ts-ignore
item.setVisible(true, false);
} else {
// @ts-ignore
item.setVisible(visible, false);
}
});
Expand Down Expand Up @@ -89,14 +97,17 @@ const updateComments = (chart: Highcharts.Chart) => {
};

// https://api.highcharts.com/class-reference/Highcharts#.SeriesLegendItemClickCallbackFunction
export const handleLegendItemClick = (event: Highcharts.SeriesLegendItemClickEventObject) => {
export const handleLegendItemClick = (
event: Highcharts.SeriesLegendItemClickEventObject | Highcharts.PointLegendItemClickEventObject,
) => {
event.preventDefault();

const series = event.target;
const chart: Highcharts.Chart = series.chart ? series.chart : (series as any).series.chart;
const chartSeries: Highcharts.Series[] = series.chart
? chart.series
: (series as any).series.data;
const series: Highcharts.Series | Highcharts.Point = event.target;

const chart: Highcharts.Chart =
series instanceof Highcharts.Point ? series.series.chart : series.chart;
const chartSeries: Highcharts.Series[] | Highcharts.Point[] =
series instanceof Highcharts.Point ? series.series.data : series.chart.series;

if (isNavigatorSeries(series)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Highcharts} from '../../../../types';

export const isNavigatorSeries = (series?: Highcharts.Series) => {
export const isNavigatorSeries = (series?: Highcharts.Series | Highcharts.Point) => {
return series?.options.className === 'highcharts-navigator-series';
};