Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
feat(plugin-chart-echarts): rich tooltip in ts chart supports scroll …
Browse files Browse the repository at this point in the history
…and highlighted (#1304)

* feat(plugin-chart-echarts): richtooltip  supports highliting and scroll

* fix: test

* fix: todo
  • Loading branch information
stephenLYZ authored Aug 16, 2021
1 parent 47eb0b3 commit ff2309a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React, { useCallback } from 'react';
import { EchartsMixedTimeseriesChartTransformedProps } from './types';
import Echart from '../components/Echart';
import { EventHandlers } from '../types';
import { currentSeries } from '../utils/series';

export default function EchartsMixedTimeseries({
height,
Expand Down Expand Up @@ -93,6 +94,12 @@ export default function EchartsMixedTimeseries({
handleChange([seriesName], seriesIndex);
}
},
mousemove: params => {
currentSeries.name = params.seriesName;
},
mouseout: () => {
currentSeries.name = '';
},
};

return (
Expand Down
25 changes: 17 additions & 8 deletions plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ import {
} from './types';
import { ForecastSeriesEnum, ProphetValue } from '../types';
import { parseYAxisBound } from '../utils/controls';
import { dedupSeries, extractTimeseriesSeries, getLegendProps } from '../utils/series';
import {
currentSeries,
dedupSeries,
extractTimeseriesSeries,
getLegendProps,
} from '../utils/series';
import { extractAnnotationLabels } from '../utils/annotation';
import {
extractForecastSeriesContext,
Expand Down Expand Up @@ -249,6 +254,7 @@ export default function transformProps(
],
tooltip: {
...defaultTooltip,
appendToBody: true,
trigger: richTooltip ? 'axis' : 'item',
formatter: (params: any) => {
const value: number = !richTooltip ? params.value : params[0].value[0];
Expand All @@ -261,13 +267,16 @@ export default function transformProps(

Object.keys(prophetValues).forEach(key => {
const value = prophetValues[key];
rows.push(
formatProphetTooltipSeries({
...value,
seriesName: key,
formatter: primarySeries.has(key) ? formatter : formatterSecondary,
}),
);
const content = formatProphetTooltipSeries({
...value,
seriesName: key,
formatter: primarySeries.has(key) ? formatter : formatterSecondary,
});
if (currentSeries.name === key) {
rows.push(`<span style="font-weight: 700">${content}</span>`);
} else {
rows.push(`<span style="opacity: 0.7">${content}</span>`);
}
});
return rows.join('<br />');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React, { useCallback } from 'react';
import { EventHandlers } from '../types';
import Echart from '../components/Echart';
import { TimeseriesChartTransformedProps } from './types';
import { currentSeries } from '../utils/series';

// @ts-ignore
export default function EchartsTimeseries({
Expand Down Expand Up @@ -78,6 +79,12 @@ export default function EchartsTimeseries({
handleChange([name]);
}
},
mousemove: params => {
currentSeries.name = params.seriesName;
},
mouseout: () => {
currentSeries.name = '';
},
};

return (
Expand Down
25 changes: 17 additions & 8 deletions plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ import {
} from './types';
import { ForecastSeriesEnum, ProphetValue } from '../types';
import { parseYAxisBound } from '../utils/controls';
import { dedupSeries, extractTimeseriesSeries, getLegendProps } from '../utils/series';
import {
dedupSeries,
extractTimeseriesSeries,
getLegendProps,
currentSeries,
} from '../utils/series';
import { extractAnnotationLabels } from '../utils/annotation';
import {
extractForecastSeriesContext,
Expand Down Expand Up @@ -226,6 +231,7 @@ export default function transformProps(
},
tooltip: {
...defaultTooltip,
appendToBody: true,
trigger: richTooltip ? 'axis' : 'item',
formatter: (params: any) => {
const value: number = !richTooltip ? params.value : params[0].value[0];
Expand All @@ -238,13 +244,16 @@ export default function transformProps(

Object.keys(prophetValues).forEach(key => {
const value = prophetValues[key];
rows.push(
formatProphetTooltipSeries({
...value,
seriesName: key,
formatter,
}),
);
const content = formatProphetTooltipSeries({
...value,
seriesName: key,
formatter,
});
if (currentSeries.name === key) {
rows.push(`<span style="font-weight: 700">${content}</span>`);
} else {
rows.push(`<span style="opacity: 0.7">${content}</span>`);
}
});
return rows.join('<br />');
},
Expand Down
5 changes: 5 additions & 0 deletions plugins/plugin-chart-echarts/src/utils/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ export function dedupSeries(series: SeriesOption[]): SeriesOption[] {
export function sanitizeHtml(text: string): string {
return format.encodeHTML(text);
}

// TODO: Better use other method to maintain this state
export const currentSeries = {
name: '',
};

1 comment on commit ff2309a

@vercel
Copy link

@vercel vercel bot commented on ff2309a Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.