Skip to content

Commit

Permalink
feat(Highcharts plugin): add tooltip.sort configuration (#170)
Browse files Browse the repository at this point in the history
* feat(Highcharts plugin): add `tooltip.sort` configuration

* fix: kuzmadom review fixes
  • Loading branch information
korvin89 authored May 31, 2023
1 parent c3579f6 commit 0825a91
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/plugins/highcharts/renderer/helpers/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ function getTooltip(tooltip, options, comments, holidays) {
],
count: 1,
shared: true,
unsafe: Boolean(options.unsafe),
sort: options?.tooltip?.sort,
};

if (typeof options.manageTooltipConfig === 'function') {
Expand Down Expand Up @@ -592,6 +594,7 @@ function getTooltip(tooltip, options, comments, holidays) {
withPercent: false,
tooltipHeader: null,
unsafe: Boolean(options.unsafe),
sort: options?.tooltip?.sort,
};

if (isDatetimeXAxis) {
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/highcharts/renderer/helpers/tooltip/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import orderBy from 'lodash/orderBy';
import type {TooltipData, TooltipLine} from './types';

export const escapeHTML = (html = '') => {
const elem = document.createElement('span');
elem.innerText = html;

return elem.innerHTML;
};

export const getSortedLines = (lines: TooltipLine[], sort: TooltipData['sort'] = {}) => {
// set eneabled to true after https://github.com/gravity-ui/chartkit/issues/171
const {enabled = false, order = 'desc', iteratee = 'originalValue'} = sort;

if (!enabled) {
return [...lines];
}

return orderBy(lines, iteratee, order);
};
9 changes: 5 additions & 4 deletions src/plugins/highcharts/renderer/helpers/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {i18n} from '../../../../../i18n';
import type {Highcharts} from '../../../types';
import type {TooltipData, TooltipLine, RowRenderingConfig} from './types';
import {renderShapeIcon} from './render-shape-icon';
import {escapeHTML} from './helpers';
import {escapeHTML, getSortedLines} from './helpers';

import './tooltip.scss';

Expand Down Expand Up @@ -211,9 +211,10 @@ export const formatTooltip = (
isMobile?: boolean,
) => {
const {splitTooltip, activeRowAlwaysFirstInTooltip} = data;
const selectedLineIndex = data.lines.findIndex(({selectedSeries}) => selectedSeries);
const selectedLine = data.lines[selectedLineIndex];
const lines = data.lines.slice(0, (tooltip.lastVisibleRowIndex || data.lines.length) + 1);
const sortedLines = getSortedLines(lines, data.sort);
const selectedLineIndex = sortedLines.findIndex(({selectedSeries}) => selectedSeries);
const selectedLine = sortedLines[selectedLineIndex];
const withShapes = lines.every((line) => line.seriesShape);
const unsafe = data.unsafe;
const tooltipHeaderRaw = data.tooltipHeader?.trim();
Expand Down Expand Up @@ -327,7 +328,7 @@ export const formatTooltip = (
</thead>`
}
<tbody class="${TOOLTIP_LIST_CLASS_NAME}">
${lines
${sortedLines
.map((line, index) => renderRow(line, getRowRenderConfig(index)))
.join('')}
</tbody>
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/highcharts/renderer/helpers/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ export type TooltipData = {
/** Sum of the values of the hidden ("not fit" in the tooltip) rows */
hiddenRowsSum?: number | string;
unsafe?: boolean;
/** Used to manage tooltip lines sorting */
sort?: {
/** Enable tooltip lines sorting. `false` by default */
enabled?: boolean;
/** The sort orders. `'desc'` by default */
order?: 'asc' | 'desc';
/** The iteratees to sort by key(s) from `TooltipLine`. `'originalValue'` by default */
iteratee?:
| keyof TooltipLine
| keyof TooltipLine[]
| ((
line: TooltipLine,
) => TooltipLine[keyof TooltipLine] | TooltipLine[keyof TooltipLine][]);
};
};

export type TooltipLine = {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/highcharts/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export type HighchartsWidgetData = {
/** Pin tooltip with pressed meta key */
metaKey?: boolean;
};
/** Used to manage tooltip lines sorting */
sort?: TooltipData['sort'];
};
/**
* Used to modify tooltip data
Expand Down

0 comments on commit 0825a91

Please sign in to comment.