Skip to content

Commit 13f1642

Browse files
feat(plugins): Tooltips on BigNumber with Time Comparison chart (apache#27092)
1 parent a912faf commit 13f1642

File tree

5 files changed

+87
-20
lines changed

5 files changed

+87
-20
lines changed

superset-frontend/packages/superset-ui-chart-controls/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export * from './components/ColumnOption';
3030
export * from './components/ColumnTypeLabel/ColumnTypeLabel';
3131
export * from './components/MetricOption';
3232
export * from './components/ControlSubSectionHeader';
33+
export * from './components/Tooltip';
3334

3435
export * from './shared-controls';
3536
export * from './types';

superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/PopKPI.tsx

+33-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* under the License.
1818
*/
1919
import React, { createRef, useMemo } from 'react';
20-
import { css, styled, useTheme } from '@superset-ui/core';
20+
import { css, styled, t, useTheme } from '@superset-ui/core';
21+
import { Tooltip } from '@superset-ui/chart-controls';
2122
import {
2223
PopKPIComparisonSymbolStyleProps,
2324
PopKPIComparisonValueStyleProps,
@@ -57,6 +58,7 @@ export default function PopKPI(props: PopKPIProps) {
5758
subheaderFontSize,
5859
comparisonColorEnabled,
5960
percentDifferenceNumber,
61+
comparatorText,
6062
} = props;
6163

6264
const rootElem = createRef<HTMLDivElement>();
@@ -117,9 +119,21 @@ export default function PopKPI(props: PopKPIProps) {
117119

118120
const SYMBOLS_WITH_VALUES = useMemo(
119121
() => [
120-
['#', prevNumber],
121-
['△', valueDifference],
122-
['%', percentDifferenceFormattedString],
122+
{
123+
symbol: '#',
124+
value: prevNumber,
125+
tooltipText: t('Data for %s', comparatorText),
126+
},
127+
{
128+
symbol: '△',
129+
value: valueDifference,
130+
tooltipText: t('Value difference between the time periods'),
131+
},
132+
{
133+
symbol: '%',
134+
value: percentDifferenceFormattedString,
135+
tooltipText: t('Percentage difference between the time periods'),
136+
},
123137
],
124138
[prevNumber, valueDifference, percentDifferenceFormattedString],
125139
);
@@ -147,18 +161,24 @@ export default function PopKPI(props: PopKPIProps) {
147161
>
148162
{SYMBOLS_WITH_VALUES.map((symbol_with_value, index) => (
149163
<ComparisonValue
150-
key={`comparison-symbol-${symbol_with_value[0]}`}
164+
key={`comparison-symbol-${symbol_with_value.symbol}`}
151165
subheaderFontSize={subheaderFontSize}
152166
>
153-
<SymbolWrapper
154-
backgroundColor={
155-
index > 0 ? backgroundColor : defaultBackgroundColor
156-
}
157-
textColor={index > 0 ? textColor : defaultTextColor}
167+
<Tooltip
168+
id="tooltip"
169+
placement="top"
170+
title={symbol_with_value.tooltipText}
158171
>
159-
{symbol_with_value[0]}
160-
</SymbolWrapper>
161-
{symbol_with_value[1]}
172+
<SymbolWrapper
173+
backgroundColor={
174+
index > 0 ? backgroundColor : defaultBackgroundColor
175+
}
176+
textColor={index > 0 ? textColor : defaultTextColor}
177+
>
178+
{symbol_with_value.symbol}
179+
</SymbolWrapper>
180+
{symbol_with_value.value}
181+
</Tooltip>
162182
</ComparisonValue>
163183
))}
164184
</div>

superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/plugin/transformProps.ts

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
NumberFormats,
2525
getNumberFormatter,
2626
} from '@superset-ui/core';
27+
import { computeQueryBComparator, formatCustomComparator } from '../utils';
2728

2829
export const parseMetricValue = (metricValue: number | string | null) => {
2930
if (typeof metricValue === 'string') {
@@ -128,6 +129,18 @@ export default function transformProps(chartProps: ChartProps) {
128129
prevNumber = numberFormatter(prevNumber);
129130
valueDifference = numberFormatter(valueDifference);
130131
const percentDifference: string = formatPercentChange(percentDifferenceNum);
132+
const comparatorText =
133+
formData.timeComparison !== 'c'
134+
? ` ${computeQueryBComparator(
135+
formData.adhocFilters,
136+
formData.timeComparison,
137+
formData.extraFormData,
138+
' - ',
139+
)}`
140+
: `${formatCustomComparator(
141+
formData.adhocCustom,
142+
formData.extraFormData,
143+
)}`;
131144

132145
return {
133146
width,
@@ -147,5 +160,6 @@ export default function transformProps(chartProps: ChartProps) {
147160
compType,
148161
comparisonColorEnabled,
149162
percentDifferenceNumber: percentDifferenceNum,
163+
comparatorText,
150164
};
151165
}

superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ export type PopKPIProps = PopKPIStylesProps &
6060
percentDifferenceFormattedString: string;
6161
compType: string;
6262
percentDifferenceNumber: number;
63+
comparatorText: string;
6364
};

superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

+38-7
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,10 @@ const calculatePrev = (
198198
return [startDatePrev, endDatePrev];
199199
};
200200

201-
export const computeQueryBComparator = (
201+
const getTimeRange = (
202202
adhocFilters: AdhocFilter[],
203-
timeComparison: string,
204203
extraFormData: any,
205-
join = ':',
206-
) => {
204+
): string | null => {
207205
const timeFilterIndex =
208206
adhocFilters?.findIndex(
209207
filter => 'operator' in filter && filter.operator === 'TEMPORAL_RANGE',
@@ -212,9 +210,6 @@ export const computeQueryBComparator = (
212210
const timeFilter =
213211
timeFilterIndex !== -1 ? adhocFilters[timeFilterIndex] : null;
214212

215-
let testSince = null;
216-
let testUntil = null;
217-
218213
if (
219214
timeFilter &&
220215
'comparator' in timeFilter &&
@@ -224,6 +219,24 @@ export const computeQueryBComparator = (
224219
if (extraFormData?.time_range) {
225220
timeRange = extraFormData.time_range;
226221
}
222+
return timeRange;
223+
}
224+
225+
return null;
226+
};
227+
228+
export const computeQueryBComparator = (
229+
adhocFilters: AdhocFilter[],
230+
timeComparison: string,
231+
extraFormData: any,
232+
join = ':',
233+
) => {
234+
const timeRange = getTimeRange(adhocFilters, extraFormData);
235+
236+
let testSince = null;
237+
let testUntil = null;
238+
239+
if (timeRange) {
227240
[testSince, testUntil] = getSinceUntil(timeRange);
228241
}
229242

@@ -244,3 +257,21 @@ export const computeQueryBComparator = (
244257

245258
return null;
246259
};
260+
261+
export const formatCustomComparator = (
262+
adhocFilters: AdhocFilter[],
263+
extraFormData: any,
264+
): string => {
265+
const timeRange = getTimeRange(adhocFilters, extraFormData);
266+
267+
if (timeRange) {
268+
const [start, end] = timeRange.split(' : ').map(dateStr => {
269+
const formattedDate = moment(dateStr).format('YYYY-MM-DDTHH:mm:ss');
270+
return formattedDate.replace(/Z/g, '');
271+
});
272+
273+
return `${start} - ${end}`;
274+
}
275+
276+
return '';
277+
};

0 commit comments

Comments
 (0)