Skip to content

Commit a80744f

Browse files
authored
feat(Tooltip): add customTotalCalculation function (#2043)
1 parent febb636 commit a80744f

File tree

4 files changed

+85
-11
lines changed

4 files changed

+85
-11
lines changed

packages/core/src/components/axes/ruler-binned.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,30 @@ export class BinnedRuler extends Ruler {
9999
? [
100100
{
101101
label:
102-
get(options, 'locale.translations.total') ||
103102
get(options, 'tooltip.totalLabel') ||
103+
get(options, 'locale.translations.total') ||
104104
'Total',
105-
value: activeDataGroupNames.reduce(
106-
(accum: number, currentValue: any) =>
107-
accum + parseFloat(get(sampleMatchData, `data.${currentValue}`)),
108-
0
109-
)
105+
value: (() => {
106+
const customTotalCalculation = getProperty(
107+
options,
108+
'tooltip',
109+
'customTotalCalculation'
110+
)
111+
if (customTotalCalculation) {
112+
const dataForCalculation = activeDataGroupNames.map(
113+
(groupName: string) => ({
114+
[groupName]: parseFloat(get(sampleMatchData, `data.${groupName}`))
115+
})
116+
)
117+
return customTotalCalculation(dataForCalculation)
118+
} else {
119+
return activeDataGroupNames.reduce(
120+
(accum: number, currentValue: string) =>
121+
accum + parseFloat(get(sampleMatchData, `data.${currentValue}`)),
122+
0
123+
)
124+
}
125+
})()
110126
}
111127
]
112128
: [])

packages/core/src/components/essentials/tooltip-axis.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,24 @@ export class AxisChartsTooltip extends Tooltip {
112112
if (!dualAxes && getProperty(options, 'tooltip', 'showTotal') === true) {
113113
// use the primary/only range id
114114
const rangeIdentifier = cartesianScales.getRangeIdentifier()
115+
const customTotalCalculation = getProperty(options, 'tooltip', 'customTotalCalculation')
116+
117+
let totalValue: number
118+
if (customTotalCalculation) {
119+
totalValue = customTotalCalculation(data)
120+
} else {
121+
totalValue = data.reduce(
122+
(accumulator: number, datum: any) => accumulator + datum[rangeIdentifier],
123+
0
124+
)
125+
}
126+
115127
items.push({
116128
label:
117-
get(options, 'locale.translations.total') ||
118129
get(options, 'tooltip.totalLabel') ||
130+
get(options, 'locale.translations.total') ||
119131
'Total',
120-
value: data.reduce(
121-
(accumulator: number, datum: any) => accumulator + datum[rangeIdentifier],
122-
0
123-
),
132+
value: totalValue,
124133
bold: true
125134
})
126135
}

packages/core/src/interfaces/components.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ export interface TooltipOptions {
175175
* customizes the `Total` label shown inside tooltips
176176
*/
177177
totalLabel?: string
178+
/**
179+
* custom function for calculating the total value
180+
* receives all the data corresponding to the tooltip
181+
*/
182+
customTotalCalculation?: (data: any[]) => number
178183
/**
179184
* when true, always shows ruler tooltip instead of individual point tooltips
180185
*/

packages/docs/src/lib/area/examplesStacked.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ const areaStackedToolbarChanges: StackedAreaChartOptions = {
105105
height: '400px'
106106
}
107107

108+
const optionsDelta: StackedAreaChartOptions = {
109+
title: 'Area chart with Delta Tooltip',
110+
axes: {
111+
left: {
112+
stacked: true,
113+
scaleType: ScaleTypes.LINEAR,
114+
mapsTo: 'value'
115+
},
116+
bottom: {
117+
scaleType: ScaleTypes.TIME,
118+
mapsTo: 'date'
119+
}
120+
},
121+
tooltip: {
122+
showTotal: true,
123+
totalLabel: 'Delta',
124+
customTotalCalculation: data => {
125+
const values = data.map(d => d.value).filter(v => v != null)
126+
if (values.length === 0) return 0
127+
return Math.max(...values) - Math.min(...values)
128+
}
129+
},
130+
curve: 'curveMonotoneX',
131+
height: '400px'
132+
}
133+
108134
const areaStackedToolbarChangesData: ChartTabularData = [
109135
{
110136
group: 'Dataset 1',
@@ -226,6 +252,19 @@ const data: ChartTabularData = [
226252
{ group: 'Dataset 3', date: '2023-01-17', value: 25213 }
227253
]
228254

255+
const dataDelta: ChartTabularData = [
256+
{ group: 'Dataset 1', date: '2023-01-01', value: 10000 },
257+
{ group: 'Dataset 1', date: '2023-01-05', value: 65000 },
258+
{ group: 'Dataset 1', date: '2023-01-08', value: 10000 },
259+
{ group: 'Dataset 1', date: '2023-01-13', value: 49213 },
260+
{ group: 'Dataset 1', date: '2023-01-17', value: 51213 },
261+
{ group: 'Dataset 2', date: '2023-01-01', value: 20000 },
262+
{ group: 'Dataset 2', date: '2023-01-05', value: 25000 },
263+
{ group: 'Dataset 2', date: '2023-01-08', value: 60000 },
264+
{ group: 'Dataset 2', date: '2023-01-13', value: 30213 },
265+
{ group: 'Dataset 2', date: '2023-01-17', value: 55213 }
266+
]
267+
229268
const dataUneven: ChartTabularData = [
230269
{ group: 'Dataset 1', date: '2023-01-01', value: 10000 },
231270
{ group: 'Dataset 1', date: '2023-01-08', value: 10000 },
@@ -257,6 +296,11 @@ export const examplesStacked: Example[] = [
257296
options: optionsUneven,
258297
tags: ['test']
259298
},
299+
{
300+
data: dataDelta,
301+
options: optionsDelta,
302+
tags: ['test', 'tooltip', 'delta']
303+
},
260304
{
261305
data: areaStackedToolbarChangesData,
262306
options: areaStackedToolbarChanges,

0 commit comments

Comments
 (0)