Skip to content

Commit dea4c14

Browse files
committed
drop support for "currency" and "currencyFormatCallback"
There is another callback option "tooltipFnc" that allows transformation of the value string. The currency options are hardcoded as prefix and the transformation is applied in non-intuitive order. Finally, adding a currency symbol can be achieved with a single callback: (value) => '$ ' + value
1 parent ec05e45 commit dea4c14

File tree

4 files changed

+7
-43
lines changed

4 files changed

+7
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* The plugin provides an ESM, CSJ and UMD version
1212
* The "class" option accepts a single class name or an array of class names
1313
* Sources were converted to TypeScript
14-
* replace deprecated properties "page{X,Y}Offset" with scroll{X,Y}
14+
* Replace deprecated properties "page{X,Y}Offset" with scroll{X,Y}
15+
* Dropped support for "currency" and "currencyFormatCallback" options (use "tooltipFnc" instead)
1516

1617
## [1.0.0] - 2nd January 2023
1718

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ It's published on npm as [chartist-plugin-tooltips-updated](https://www.npmjs.co
66

77
## Available options and their defaults
88

9-
* **currency**: `string`
10-
11-
Currency or unit suffix, e.h. '$', '€' or '%' to be appended to the value.
12-
13-
* **currencyFormatCallback**: `(value: string, options: Options) => string`
14-
15-
Transformation function to be applied in combination with "currency".
16-
179
* **tooltipOffset**: `{ x: number, y: number }`
1810

1911
Tooltip offset in px. Default: `{ x: 0, y: -20 }`

src/scripts/chartist-plugin-tooltip.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ import { BarChart, BaseChart, PieChart } from 'chartist';
55
* Tooltip plugin options.
66
*/
77
export interface Options {
8-
/**
9-
* Currency or unit suffix, e.h. '$', '€' or '%' to be appended to the value.
10-
*/
11-
currency?: string;
12-
/**
13-
* Transformation function to be applied in combination with "currency".
14-
*
15-
* @param value - The original value.
16-
* @param options - Plugin options.
17-
* @returns Tooltip value for output.
18-
*/
19-
currencyFormatCallback?: (value: string, options: Options) => string;
208
/**
219
* Tooltip offset in px.
2210
* Default: x 0, y -20
@@ -72,8 +60,6 @@ export function ChartistPluginTooltip<T extends BaseChart>(
7260
options?: Partial<Options>
7361
): void {
7462
const defaultOptions = {
75-
currency: undefined,
76-
currencyFormatCallback: undefined,
7763
tooltipOffset: {
7864
x: 0,
7965
y: -20
@@ -82,6 +68,8 @@ export function ChartistPluginTooltip<T extends BaseChart>(
8268
appendToBody: true,
8369
class: undefined,
8470
pointClass: 'ct-point',
71+
tooltipFnc: undefined,
72+
transformTooltipTextFnc: undefined,
8573
metaIsHTML: false
8674
};
8775

@@ -194,15 +182,6 @@ export function ChartistPluginTooltip<T extends BaseChart>(
194182
}
195183

196184
if (value) {
197-
if ($options.currency) {
198-
if ($options.currencyFormatCallback !== undefined) {
199-
value = $options.currencyFormatCallback(value, $options);
200-
} else {
201-
value =
202-
$options.currency +
203-
value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, '$1,');
204-
}
205-
}
206185
value = '<span class="chartist-tooltip-value">' + value + '</span>';
207186
tooltipText += value;
208187
}

test/spec/tooltip.spec.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { BarChart, LineChart, PieChart } from 'chartist';
2-
import {
3-
ChartistPluginTooltip,
4-
Options
5-
} from '../../src/scripts/chartist-plugin-tooltip';
2+
import { ChartistPluginTooltip } from '../../src/scripts/chartist-plugin-tooltip';
63

74
describe('Tooltip Plugin', () => {
85
beforeAll(() => {
@@ -408,11 +405,6 @@ describe('Tooltip Plugin', () => {
408405
[
409406
ChartistPluginTooltip,
410407
{
411-
currency: '$',
412-
currencyFormatCallback: (
413-
value: string,
414-
options: Options
415-
): string => options.currency + ' ' + value + ' (incl. VAT)',
416408
tooltipOffset: {
417409
x: 13,
418410
y: 37
@@ -422,7 +414,7 @@ describe('Tooltip Plugin', () => {
422414
class: 'my-tooltip',
423415
pointClass: 'ct-point',
424416
transformTooltipTextFnc: (value: string): string =>
425-
value + '.00'
417+
'$ ' + value + '.00'
426418
}
427419
]
428420
]
@@ -464,7 +456,7 @@ describe('Tooltip Plugin', () => {
464456

465457
it('should generate tooltip content', () => {
466458
expect(tooltip?.innerHTML).toEqual(
467-
'<span class="chartist-tooltip-value">$ 5.00 (incl. VAT)</span>'
459+
'<span class="chartist-tooltip-value">$ 5.00</span>'
468460
);
469461
});
470462

0 commit comments

Comments
 (0)