All files / src/plugins/highcharts/renderer/helpers add-holidays.ts

0% Statements 0/29
0% Branches 0/17
0% Functions 0/3
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                                                                                                     
import type {ChartKitHolidays} from '../../../../types';
import type {ExtendedHChart, Highcharts} from '../../types';
 
const HALF_DAY = 43200000;
 
const calculateConsistentClosestPointRange = (
    type: string,
    closestPointRange: number,
    series: Highcharts.Series[],
) => {
    const isDatetimeAxis = type === 'datetime';
 
    return (
        isDatetimeAxis &&
        closestPointRange === 86400000 &&
        series.every(({xData}) => {
            let consistent = true;
            for (let i = 2; i < xData.length - 1; i++) {
                if ((xData[i] - xData[i - 1]) % closestPointRange !== 0) {
                    consistent = false;
                    break;
                }
            }
            return consistent;
        })
    );
};
 
export function addHolidays(chart: ExtendedHChart, holidays: ChartKitHolidays) {
    const {
        userOptions: {_config: {region: configRegion = 'TOT'} = {}},
        xAxis: [xAxis],
    } = chart;
 
    const {dataMin, dataMax, closestPointRange, series} = xAxis;
    const isConsistentClosestPointRange = calculateConsistentClosestPointRange(
        xAxis.options.type || '',
        closestPointRange,
        series,
    );
 
    let needRedraw = false;
 
    if (isConsistentClosestPointRange) {
        const region = configRegion.toLowerCase();
 
        for (let passed = 0; dataMin + passed <= dataMax; passed += closestPointRange) {
            const timestamp = dataMin + passed;
 
            const pointDate = Number(chart.time.dateFormat('%Y%m%d', timestamp));
 
            const holidayByRegion = holidays.holiday[region];
            const weekendByRegion = holidays.weekend[region];
 
            if (
                (holidayByRegion && holidayByRegion[pointDate]) ||
                (weekendByRegion && weekendByRegion[pointDate])
            ) {
                const bandStart = timestamp - HALF_DAY;
                const bandStop = timestamp + HALF_DAY;
 
                xAxis.addPlotBand({
                    color: 'var(--highcharts-holiday-band)',
                    from: bandStart,
                    to: bandStop,
                });
 
                needRedraw = true;
            }
        }
    }
 
    return needRedraw;
}