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

feat(plugins): Tooltips on BigNumber with Time Comparison chart #27092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -30,6 +30,7 @@ export * from './components/ColumnOption';
export * from './components/ColumnTypeLabel/ColumnTypeLabel';
export * from './components/MetricOption';
export * from './components/ControlSubSectionHeader';
export * from './components/Tooltip';

export * from './shared-controls';
export * from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* under the License.
*/
import React, { createRef, useMemo } from 'react';
import { css, styled, useTheme } from '@superset-ui/core';
import { css, styled, t, useTheme } from '@superset-ui/core';
import { Tooltip } from '@superset-ui/chart-controls';
import {
PopKPIComparisonSymbolStyleProps,
PopKPIComparisonValueStyleProps,
Expand Down Expand Up @@ -57,6 +58,7 @@ export default function PopKPI(props: PopKPIProps) {
subheaderFontSize,
comparisonColorEnabled,
percentDifferenceNumber,
comparatorText,
} = props;

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

const SYMBOLS_WITH_VALUES = useMemo(
() => [
['#', prevNumber],
['△', valueDifference],
['%', percentDifferenceFormattedString],
{
symbol: '#',
value: prevNumber,
tooltipText: t('Data for %s', comparatorText),
},
{
symbol: '△',
value: valueDifference,
tooltipText: t('Value difference between the time periods'),
},
{
symbol: '%',
value: percentDifferenceFormattedString,
tooltipText: t('Percentage difference between the time periods'),
},
],
[prevNumber, valueDifference, percentDifferenceFormattedString],
);
Expand Down Expand Up @@ -147,18 +161,24 @@ export default function PopKPI(props: PopKPIProps) {
>
{SYMBOLS_WITH_VALUES.map((symbol_with_value, index) => (
<ComparisonValue
key={`comparison-symbol-${symbol_with_value[0]}`}
key={`comparison-symbol-${symbol_with_value.symbol}`}
subheaderFontSize={subheaderFontSize}
>
<SymbolWrapper
backgroundColor={
index > 0 ? backgroundColor : defaultBackgroundColor
}
textColor={index > 0 ? textColor : defaultTextColor}
<Tooltip
id="tooltip"
placement="top"
title={symbol_with_value.tooltipText}
>
{symbol_with_value[0]}
</SymbolWrapper>
{symbol_with_value[1]}
<SymbolWrapper
backgroundColor={
index > 0 ? backgroundColor : defaultBackgroundColor
}
textColor={index > 0 ? textColor : defaultTextColor}
>
{symbol_with_value.symbol}
</SymbolWrapper>
{symbol_with_value.value}
</Tooltip>
</ComparisonValue>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
NumberFormats,
getNumberFormatter,
} from '@superset-ui/core';
import { computeQueryBComparator, formatCustomComparator } from '../utils';

export const parseMetricValue = (metricValue: number | string | null) => {
if (typeof metricValue === 'string') {
Expand Down Expand Up @@ -128,6 +129,18 @@ export default function transformProps(chartProps: ChartProps) {
prevNumber = numberFormatter(prevNumber);
valueDifference = numberFormatter(valueDifference);
const percentDifference: string = formatPercentChange(percentDifferenceNum);
const comparatorText =
formData.timeComparison !== 'c'
? ` ${computeQueryBComparator(
formData.adhocFilters,
formData.timeComparison,
formData.extraFormData,
' - ',
)}`
: `${formatCustomComparator(
formData.adhocCustom,
formData.extraFormData,
)}`;

return {
width,
Expand All @@ -147,5 +160,6 @@ export default function transformProps(chartProps: ChartProps) {
compType,
comparisonColorEnabled,
percentDifferenceNumber: percentDifferenceNum,
comparatorText,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ export type PopKPIProps = PopKPIStylesProps &
percentDifferenceFormattedString: string;
compType: string;
percentDifferenceNumber: number;
comparatorText: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,10 @@
return [startDatePrev, endDatePrev];
};

export const computeQueryBComparator = (
const getTimeRange = (
adhocFilters: AdhocFilter[],
timeComparison: string,
extraFormData: any,
join = ':',
) => {
): string | null => {
const timeFilterIndex =
adhocFilters?.findIndex(
filter => 'operator' in filter && filter.operator === 'TEMPORAL_RANGE',
Expand All @@ -212,9 +210,6 @@
const timeFilter =
timeFilterIndex !== -1 ? adhocFilters[timeFilterIndex] : null;

let testSince = null;
let testUntil = null;

if (
timeFilter &&
'comparator' in timeFilter &&
Expand All @@ -224,6 +219,24 @@
if (extraFormData?.time_range) {
timeRange = extraFormData.time_range;
}
return timeRange;

Check warning on line 222 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L222 was not covered by tests
}

return null;

Check warning on line 225 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L225 was not covered by tests
};

export const computeQueryBComparator = (
adhocFilters: AdhocFilter[],
timeComparison: string,
extraFormData: any,
join = ':',
) => {
const timeRange = getTimeRange(adhocFilters, extraFormData);

Check warning on line 234 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L234 was not covered by tests

let testSince = null;
let testUntil = null;

Check warning on line 237 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L236 - L237 were not covered by tests

if (timeRange) {
[testSince, testUntil] = getSinceUntil(timeRange);
}

Expand All @@ -244,3 +257,21 @@

return null;
};

export const formatCustomComparator = (
adhocFilters: AdhocFilter[],
extraFormData: any,
): string => {
const timeRange = getTimeRange(adhocFilters, extraFormData);

Check warning on line 265 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L265 was not covered by tests

if (timeRange) {
const [start, end] = timeRange.split(' : ').map(dateStr => {
const formattedDate = moment(dateStr).format('YYYY-MM-DDTHH:mm:ss');
return formattedDate.replace(/Z/g, '');

Check warning on line 270 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L268 - L270 were not covered by tests
});

return `${start} - ${end}`;

Check warning on line 273 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L273 was not covered by tests
}

return '';

Check warning on line 276 in superset-frontend/plugins/plugin-chart-period-over-period-kpi/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L276 was not covered by tests
};
Loading