/*
* @version: 4.1.3
* @author: Preline Labs Ltd.
* @license: Licensed under MIT and Preline UI Fair Use License (https://preline.co/docs/license.html)
* Copyright 2024 Preline Labs Ltd.
*/
import {
IBuildTooltipHelperOptions,
IBuildTooltipHelperSingleOptions,
IChartDonutProps,
IChartProps,
IChartPropsSeries,
} from './interfaces';
import { EventWithProps } from '../types';
import ApexCharts from 'apexcharts';
function buildTooltip(props: IChartProps, options: IBuildTooltipHelperOptions) {
const {
title,
mode,
valuePrefix = '$',
isValueDivided = true,
valuePostfix = '',
hasTextLabel = false,
invertGroup = false,
labelDivider = '',
wrapperClasses = 'ms-0.5 mb-2 bg-white border border-gray-200 text-gray-800 rounded-lg shadow-md dark:bg-neutral-800 dark:border-neutral-700',
wrapperExtClasses = '',
seriesClasses = 'text-xs',
seriesExtClasses = '',
titleClasses = 'font-semibold !text-sm !bg-white !border-gray-200 text-gray-800 rounded-t-lg dark:!bg-neutral-800 dark:!border-neutral-700 dark:text-neutral-200',
titleExtClasses = '',
markerClasses = '!w-2.5 !h-2.5 !me-1.5',
markerExtClasses = '!rounded-xs',
valueClasses = '!font-medium text-gray-500 !ms-auto dark:text-neutral-400',
valueExtClasses = '',
labelClasses = 'text-gray-500 dark:text-neutral-400',
labelExtClasses = '',
thousandsShortName = 'k',
} = options;
const { dataPointIndex } = props;
// const { colors } = props.ctx.opts;
const { colors } = props.w.globals;
const series = props.ctx.opts.series as IChartPropsSeries[];
let seriesGroups = '';
series.forEach((_, i) => {
const val =
props.series[i][dataPointIndex] ||
(typeof series[i].data[dataPointIndex] !== 'object'
? series[i].data[dataPointIndex]
: props.series[i][dataPointIndex]);
const label = series[i].name;
const groupData = invertGroup
? {
left: `${hasTextLabel ? label : ''}${labelDivider}`,
right: `${valuePrefix}${
val >= 1000 && isValueDivided
? `${val / 1000}${thousandsShortName}`
: val
}${valuePostfix}`,
}
: {
left: `${valuePrefix}${
val >= 1000 && isValueDivided
? `${val / 1000}${thousandsShortName}`
: val
}${valuePostfix}`,
right: `${hasTextLabel ? label : ''}${labelDivider}`,
};
const labelMarkup = `${groupData.left}`;
seriesGroups += `
`;
});
return ``;
}
function buildHeatmapTooltip(
props: IChartProps,
options: IBuildTooltipHelperSingleOptions,
) {
const {
mode,
valuePrefix = '$',
valuePostfix = '',
divider = '',
wrapperClasses = 'ms-0.5 mb-2 bg-white border border-gray-200 text-gray-800 rounded-lg shadow-md dark:bg-neutral-800 dark:border-neutral-700',
wrapperExtClasses = '',
markerClasses = '!w-2.5 !h-2.5 !me-1.5',
markerStyles = '',
markerExtClasses = '!rounded-xs',
valueClasses = '!font-medium text-gray-500 !ms-auto dark:text-neutral-400',
valueExtClasses = '',
} = options;
const { dataPointIndex, seriesIndex, series } = props;
const { name } = props.ctx.opts.series[seriesIndex] as IChartPropsSeries;
const val = `${valuePrefix}${
series[seriesIndex][dataPointIndex]
}${valuePostfix}`;
return ``;
}
function buildTooltipCompareTwo(
props: IChartProps,
options: IBuildTooltipHelperOptions,
) {
const {
title,
mode,
valuePrefix = '$',
isValueDivided = true,
valuePostfix = '',
hasCategory = true,
hasTextLabel = false,
labelDivider = '',
wrapperClasses = 'ms-0.5 mb-2 bg-white border border-gray-200 text-gray-800 rounded-lg shadow-md dark:bg-neutral-800 dark:border-neutral-700',
wrapperExtClasses = '',
seriesClasses = '!justify-between w-full text-xs',
seriesExtClasses = '',
titleClasses = 'flex justify-between font-semibold !text-sm !bg-white !border-gray-200 text-gray-800 rounded-t-lg dark:!bg-neutral-800 dark:!border-neutral-700 dark:text-neutral-200',
titleExtClasses = '',
markerClasses = '!w-2.5 !h-2.5 !me-1.5',
markerExtClasses = '!rounded-xs',
valueClasses = '!font-medium text-gray-500 !ms-auto dark:text-neutral-400',
valueExtClasses = '',
labelClasses = 'text-gray-500 dark:text-neutral-400 ms-2',
labelExtClasses = '',
thousandsShortName = 'k',
} = options;
const { dataPointIndex } = props;
const { categories } = props.ctx.opts.xaxis;
// const { colors } = props.ctx.opts;
const { colors } = props.w.globals;
const series = props.ctx.opts.series as IChartPropsSeries[];
let seriesGroups = '';
const s0 = series[0].data[dataPointIndex];
const s1 = series[1].data[dataPointIndex];
const category = categories[dataPointIndex].split(' ');
const newCategory = hasCategory
? `${category[0]}${category[1] ? ' ' : ''}${
category[1] ? category[1].slice(0, 3) : ''
}`
: '';
// const isGrowing = s0 > s1;
// const isDifferenceIsNull = s0 / s1 === 1;
// const difference = isDifferenceIsNull ? 0 : (s0 / s1) * 100;
// TODO: test this before deleting the code above
const isPrevZero = s1 === 0;
const difference = isPrevZero ? 0 : ((s0 - s1) / Math.abs(s1)) * 100;
const isDifferenceIsNull = difference === 0;
const isGrowing = difference > 0;
const icon = isGrowing
? ``
: ``;
series.forEach((_, i) => {
const val =
props.series[i][dataPointIndex] ||
(typeof series[i].data[dataPointIndex] !== 'object'
? series[i].data[dataPointIndex]
: props.series[i][dataPointIndex]);
const label = series[i].name;
const altValue = series[i].altValue || null;
const labelMarkup = `${newCategory} ${
label || ''
}`;
const valueMarkup =
altValue ||
`${valuePrefix}${
val >= 1000 && isValueDivided
? `${val / 1000}${thousandsShortName}`
: val
}${valuePostfix}${labelDivider}`;
seriesGroups += ``;
});
return `
${title}
${!isDifferenceIsNull ? icon : ''}
${difference.toFixed(1)}%
${seriesGroups}
`;
}
function buildTooltipCompareTwoAlt(
props: IChartProps,
options: IBuildTooltipHelperOptions,
) {
const {
title,
mode,
valuePrefix = '$',
isValueDivided = true,
valuePostfix = '',
hasCategory = true,
hasTextLabel = false,
labelDivider = '',
wrapperClasses = 'ms-0.5 mb-2 bg-white border border-gray-200 text-gray-800 rounded-lg shadow-md dark:bg-neutral-800 dark:border-neutral-700',
wrapperExtClasses = '',
seriesClasses = '!justify-between w-full text-xs',
seriesExtClasses = '',
titleClasses = 'flex justify-between font-semibold !text-sm !bg-white !border-gray-200 text-gray-800 rounded-t-lg dark:!bg-neutral-800 dark:!border-neutral-700 dark:text-neutral-200',
titleExtClasses = '',
markerClasses = '!w-2.5 !h-2.5 !me-1.5',
markerExtClasses = '!rounded-xs',
valueClasses = '!font-medium text-gray-500 !ms-auto dark:text-neutral-400',
valueExtClasses = '',
labelClasses = 'text-gray-500 dark:text-neutral-400 ms-2',
labelExtClasses = '',
thousandsShortName = 'k',
} = options;
const { dataPointIndex } = props;
const { categories } = props.ctx.opts.xaxis;
// const { colors } = props.ctx.opts;
const { colors } = props.w.globals;
const series = props.ctx.opts.series as IChartPropsSeries[];
let seriesGroups = '';
const s0 = series[0].data[dataPointIndex];
const s1 = series[1].data[dataPointIndex];
const category = categories[dataPointIndex].split(' ');
const newCategory = hasCategory
? `${category[0]}${category[1] ? ' ' : ''}${
category[1] ? category[1].slice(0, 3) : ''
}`
: '';
// const isGrowing = s0 > s1;
// const isDifferenceIsNull = s0 / s1 === 1;
// const difference = isDifferenceIsNull ? 0 : (s0 / s1) * 100;
// TODO: test this before deleting the code above
const isPrevZero = s1 === 0;
const difference = isPrevZero ? 0 : ((s0 - s1) / Math.abs(s1)) * 100;
const isDifferenceIsNull = difference === 0;
const isGrowing = difference > 0;
const icon = isGrowing
? ``
: ``;
series.forEach((_, i) => {
const val =
props.series[i][dataPointIndex] ||
(typeof series[i].data[dataPointIndex] !== 'object'
? series[i].data[dataPointIndex]
: props.series[i][dataPointIndex]);
const label = series[i].name;
const labelMarkup = `${valuePrefix}${
val >= 1000 && isValueDivided ? `${val / 1000}${thousandsShortName}` : val
}${valuePostfix}`;
seriesGroups += ``;
});
return `
${title}
${!isDifferenceIsNull ? icon : ''}
${difference.toFixed(1)}%
${seriesGroups}
`;
}
function buildTooltipForDonut(
{ series, seriesIndex, w }: IChartDonutProps,
textColor: string[],
) {
const { globals } = w;
const { colors } = globals;
return `