Skip to content

Commit

Permalink
feat(Highcharts plugin)!: remove sort logic from tooltip to higher le…
Browse files Browse the repository at this point in the history
…vel (#200)
  • Loading branch information
korvin89 committed Jul 17, 2023
1 parent 8c11708 commit 703191c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
5 changes: 2 additions & 3 deletions src/plugins/highcharts/renderer/helpers/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
numberFormat,
getFormatOptionsFromLine,
checkTooltipPinningAvailability,
getSortedData,
} from './utils';
import {handleLegendItemClick} from './handleLegendItemClick';
import {getChartKitFormattedValue} from './utils/getChartKitFormattedValue';
Expand Down Expand Up @@ -528,7 +529,6 @@ 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 @@ -573,7 +573,7 @@ function getTooltip(tooltip, options, comments, holidays) {
let shared;

if (this.points) {
points = this.points;
points = getSortedData(this.points, options?.tooltip?.sort);
shared = true;
} else {
points.push(Object.assign({}, this.point));
Expand All @@ -594,7 +594,6 @@ function getTooltip(tooltip, options, comments, holidays) {
withPercent: false,
tooltipHeader: null,
unsafe: Boolean(options.unsafe),
sort: options?.tooltip?.sort,
};

if (isDatetimeXAxis) {
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/highcharts/renderer/helpers/config/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import orderBy from 'lodash/orderBy';
import type {HighchartsSortData} from '../../../../types/widget';

export {addShowInNavigatorToSeries} from './addShowInNavigatorToSeries';
export {buildNavigatorFallback} from './buildNavigatorFallback';
export {calculatePrecision} from './calculatePrecision';
Expand All @@ -11,3 +14,16 @@ export {isSafari} from './isSafari';
export {mergeArrayWithObject} from './mergeArrayWithObject';
export {numberFormat} from './numberFormat';
export {setNavigatorDefaultPeriod} from './setNavigatorDefaultPeriod';

export const getSortedData = <T extends Record<string, any>>(
data: T[],
sort: HighchartsSortData = {},
) => {
const {enabled = true, order = 'desc', iteratee = 'y'} = sort;

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

return orderBy(data, iteratee, order);
};
13 changes: 0 additions & 13 deletions src/plugins/highcharts/renderer/helpers/tooltip/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
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'] = {}) => {
const {enabled = true, order = 'desc', iteratee = 'originalValue'} = sort;

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

return orderBy(lines, iteratee, order);
};
9 changes: 4 additions & 5 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, getSortedLines} from './helpers';
import {escapeHTML} from './helpers';

import './tooltip.scss';

Expand Down Expand Up @@ -212,9 +212,8 @@ export const formatTooltip = (
) => {
const {splitTooltip, activeRowAlwaysFirstInTooltip} = data;
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 selectedLineIndex = lines.findIndex(({selectedSeries}) => selectedSeries);
const selectedLine = lines[selectedLineIndex];
const withShapes = lines.every((line) => line.seriesShape);
const unsafe = data.unsafe;
const tooltipHeaderRaw = data.tooltipHeader?.trim();
Expand Down Expand Up @@ -328,7 +327,7 @@ export const formatTooltip = (
</thead>`
}
<tbody class="${TOOLTIP_LIST_CLASS_NAME}">
${sortedLines
${lines
.map((line, index) => renderRow(line, getRowRenderConfig(index)))
.join('')}
</tbody>
Expand Down
14 changes: 0 additions & 14 deletions src/plugins/highcharts/renderer/helpers/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ 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
16 changes: 15 additions & 1 deletion src/plugins/highcharts/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ export type HighchartsManageTooltipConfig = (
chart: Highcharts.Chart,
) => HighchartsManageTooltipConfigOptions;

export type HighchartsSortData = {
/** 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 HighchartsWidgetData = {
data: (
| CkHighchartsSeriesOptionsType[]
Expand Down Expand Up @@ -105,7 +119,7 @@ export type HighchartsWidgetData = {
metaKey?: boolean;
};
/** Used to manage tooltip lines sorting */
sort?: TooltipData['sort'];
sort?: HighchartsSortData;
};
/**
* Used to modify tooltip data
Expand Down

0 comments on commit 703191c

Please sign in to comment.