Skip to content

Commit 749e137

Browse files
committed
fix(plugin-chart-echarts): show zero value in tooltip
1 parent 034ee1c commit 749e137

File tree

2 files changed

+137
-91
lines changed

2 files changed

+137
-91
lines changed

superset-frontend/plugins/plugin-chart-echarts/src/utils/forecast.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import { isNumber } from 'lodash';
1920
import { DataRecord, DTTM_ALIAS, NumberFormatter } from '@superset-ui/core';
2021
import { OptionName } from 'echarts/types/src/util/types';
2122
import { TooltipMarker } from 'echarts/types/src/util/format';
@@ -60,7 +61,7 @@ export const extractForecastValuesFromTooltipParams = (
6061
const { marker, seriesId, value } = param;
6162
const context = extractForecastSeriesContext(seriesId);
6263
const numericValue = isHorizontal ? value[0] : value[1];
63-
if (numericValue) {
64+
if (isNumber(numericValue)) {
6465
if (!(context.name in values))
6566
values[context.name] = {
6667
marker: marker || '',
@@ -94,7 +95,7 @@ export const formatForecastTooltipSeries = ({
9495
}): string => {
9596
let row = `${marker}${sanitizeHtml(seriesName)}: `;
9697
let isObservation = false;
97-
if (observation) {
98+
if (isNumber(observation)) {
9899
isObservation = true;
99100
row += `${formatter(observation)}`;
100101
}

superset-frontend/plugins/plugin-chart-echarts/test/utils/forecast.test.ts

+134-89
Original file line numberDiff line numberDiff line change
@@ -154,103 +154,148 @@ describe('rebaseForecastDatum', () => {
154154
});
155155
});
156156

157-
describe('extractForecastValuesFromTooltipParams', () => {
158-
it('should extract the proper data from tooltip params', () => {
159-
expect(
160-
extractForecastValuesFromTooltipParams([
161-
{
162-
marker: '<img>',
163-
seriesId: 'abc',
164-
value: [new Date(0), 10],
165-
},
166-
{
167-
marker: '<img>',
168-
seriesId: 'abc__yhat',
169-
value: [new Date(0), 1],
170-
},
171-
{
172-
marker: '<img>',
173-
seriesId: 'abc__yhat_lower',
174-
value: [new Date(0), 5],
175-
},
176-
{
177-
marker: '<img>',
178-
seriesId: 'abc__yhat_upper',
179-
value: [new Date(0), 6],
180-
},
181-
{
182-
marker: '<img>',
183-
seriesId: 'qwerty',
184-
value: [new Date(0), 2],
185-
},
186-
]),
187-
).toEqual({
188-
abc: {
157+
test('extractForecastValuesFromTooltipParams should extract the proper data from tooltip params', () => {
158+
expect(
159+
extractForecastValuesFromTooltipParams([
160+
{
189161
marker: '<img>',
190-
observation: 10,
191-
forecastTrend: 1,
192-
forecastLower: 5,
193-
forecastUpper: 6,
162+
seriesId: 'abc',
163+
value: [new Date(0), 10],
194164
},
195-
qwerty: {
165+
{
196166
marker: '<img>',
197-
observation: 2,
167+
seriesId: 'abc__yhat',
168+
value: [new Date(0), 1],
198169
},
199-
});
200-
});
201-
});
202-
203-
const formatter = getNumberFormatter(NumberFormats.INTEGER);
204-
205-
describe('formatForecastTooltipSeries', () => {
206-
it('should generate a proper series tooltip', () => {
207-
expect(
208-
formatForecastTooltipSeries({
209-
seriesName: 'abc',
170+
{
210171
marker: '<img>',
211-
observation: 10.1,
212-
formatter,
213-
}),
214-
).toEqual('<img>abc: 10');
215-
expect(
216-
formatForecastTooltipSeries({
217-
seriesName: 'qwerty',
172+
seriesId: 'abc__yhat_lower',
173+
value: [new Date(0), 5],
174+
},
175+
{
218176
marker: '<img>',
219-
observation: 10.1,
220-
forecastTrend: 20.1,
221-
forecastLower: 5.1,
222-
forecastUpper: 7.1,
223-
formatter,
224-
}),
225-
).toEqual('<img>qwerty: 10, ŷ = 20 (5, 12)');
226-
expect(
227-
formatForecastTooltipSeries({
228-
seriesName: 'qwerty',
177+
seriesId: 'abc__yhat_upper',
178+
value: [new Date(0), 6],
179+
},
180+
{
229181
marker: '<img>',
230-
forecastTrend: 20,
231-
forecastLower: 5,
232-
forecastUpper: 7,
233-
formatter,
234-
}),
235-
).toEqual('<img>qwerty: ŷ = 20 (5, 12)');
236-
expect(
237-
formatForecastTooltipSeries({
238-
seriesName: 'qwerty',
182+
seriesId: 'qwerty',
183+
value: [new Date(0), 2],
184+
},
185+
]),
186+
).toEqual({
187+
abc: {
188+
marker: '<img>',
189+
observation: 10,
190+
forecastTrend: 1,
191+
forecastLower: 5,
192+
forecastUpper: 6,
193+
},
194+
qwerty: {
195+
marker: '<img>',
196+
observation: 2,
197+
},
198+
});
199+
});
200+
201+
test('extractForecastValuesFromTooltipParams should extract valid values', () => {
202+
expect(
203+
extractForecastValuesFromTooltipParams([
204+
{
239205
marker: '<img>',
240-
observation: 10.1,
241-
forecastLower: 6,
242-
forecastUpper: 7,
243-
formatter,
244-
}),
245-
).toEqual('<img>qwerty: 10 (6, 13)');
246-
expect(
247-
formatForecastTooltipSeries({
248-
seriesName: 'qwerty',
206+
seriesId: 'foo',
207+
value: [0, 10],
208+
},
209+
{
249210
marker: '<img>',
250-
forecastLower: 7,
251-
forecastUpper: 8,
252-
formatter,
253-
}),
254-
).toEqual('<img>qwerty: (7, 15)');
211+
seriesId: 'bar',
212+
value: [100, 0],
213+
},
214+
]),
215+
).toEqual({
216+
foo: {
217+
marker: '<img>',
218+
observation: 10,
219+
},
220+
bar: {
221+
marker: '<img>',
222+
observation: 0,
223+
},
255224
});
256225
});
226+
227+
const formatter = getNumberFormatter(NumberFormats.INTEGER);
228+
229+
test('formatForecastTooltipSeries should apply format to value', () => {
230+
expect(
231+
formatForecastTooltipSeries({
232+
seriesName: 'abc',
233+
marker: '<img>',
234+
observation: 10.1,
235+
formatter,
236+
}),
237+
).toEqual('<img>abc: 10');
238+
});
239+
240+
test('formatForecastTooltipSeries should show falsy value', () => {
241+
expect(
242+
formatForecastTooltipSeries({
243+
seriesName: 'abc',
244+
marker: '<img>',
245+
observation: 0,
246+
formatter,
247+
}),
248+
).toEqual('<img>abc: 0');
249+
});
250+
251+
test('formatForecastTooltipSeries should format full forecast', () => {
252+
expect(
253+
formatForecastTooltipSeries({
254+
seriesName: 'qwerty',
255+
marker: '<img>',
256+
observation: 10.1,
257+
forecastTrend: 20.1,
258+
forecastLower: 5.1,
259+
forecastUpper: 7.1,
260+
formatter,
261+
}),
262+
).toEqual('<img>qwerty: 10, ŷ = 20 (5, 12)');
263+
});
264+
265+
test('formatForecastTooltipSeries should format forecast without observation', () => {
266+
expect(
267+
formatForecastTooltipSeries({
268+
seriesName: 'qwerty',
269+
marker: '<img>',
270+
forecastTrend: 20,
271+
forecastLower: 5,
272+
forecastUpper: 7,
273+
formatter,
274+
}),
275+
).toEqual('<img>qwerty: ŷ = 20 (5, 12)');
276+
});
277+
278+
test('formatForecastTooltipSeries should format forecast without point estimate', () => {
279+
expect(
280+
formatForecastTooltipSeries({
281+
seriesName: 'qwerty',
282+
marker: '<img>',
283+
observation: 10.1,
284+
forecastLower: 6,
285+
forecastUpper: 7,
286+
formatter,
287+
}),
288+
).toEqual('<img>qwerty: 10 (6, 13)');
289+
});
290+
291+
test('formatForecastTooltipSeries should format forecast with only confidence band', () => {
292+
expect(
293+
formatForecastTooltipSeries({
294+
seriesName: 'qwerty',
295+
marker: '<img>',
296+
forecastLower: 7,
297+
forecastUpper: 8,
298+
formatter,
299+
}),
300+
).toEqual('<img>qwerty: (7, 15)');
301+
});

0 commit comments

Comments
 (0)