Framework/React

[React] λ¦¬μ•‘νŠΈ apexcharts μ»€μŠ€ν…€ 툴팁 (custom tooltip) λ§Œλ“œλŠ” 방법, νˆ΄νŒμ— μ‹œκ°„ 데이터와 κ°’ ν‘œμ‹œν•˜κΈ°

yuri lee 2023. 3. 20. 22:24
λ°˜μ‘ν˜•

Intro

μ•ˆλ…•ν•˜μ„Έμš”. μ΄λ²ˆμ‹œκ°„μ—λŠ” λ¦¬μ•‘νŠΈμ—μ„œ apexcharts λ₯Ό μ΄μš©ν•˜μ—¬ μ»€μŠ€ν…€ νˆ΄νŒμ„ λ§Œλ“œλŠ” 방법에 λŒ€ν•΄ μ•Œμ•„λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. 

 

How to solve the problem

apexcharts λ₯Ό μ‚¬μš©ν•œλ‹€λŠ” κ°€μ •ν•˜μ—, μ•„λž˜μ˜ series 데이터와 chart option이 μžˆλ‹€κ³  μƒκ°ν•΄λ΄…μ‹œλ‹€.

    const series: any = [
            {
                name: 'EBAY',
                data: [
                    ['2023-03-20T07:00:22.625Z', '123124.82'],
                    ['2023-03-20T06:00:20.103Z', '52353.83'],
                    ['2023-03-20T05:00:16.960Z', '98943.91'],
                    ['2023-03-20T04:00:15.051Z', '796756.65'],
                    ['2023-03-20T03:00:05.754Z', '4831.18'],
                    ['2023-03-20T02:00:08.835Z', '13.75'],
                ],
                stockName: 'EBAY',
            },
            {
                name: 'AAPL',
                data: [
                    ['2023-03-20T07:00:22.625Z', '6756.78'],
                    ['2023-03-20T06:00:20.103Z', '345547.78'],
                    ['2023-03-20T05:00:16.960Z', '1321.95'],
                    ['2023-03-20T04:00:15.051Z', '235890.64'],
                    ['2023-03-20T03:00:05.754Z', '32.20'],
                    ['2023-03-20T02:00:08.835Z', '12314.79'],
                ],
                stockName: 'AAPL',
            },
            {
                name: 'AMZN',
                data: [
                    ['2023-03-20T07:00:22.625Z', '4.89'],
                    ['2023-03-20T06:00:20.103Z', '8.84'],
                    ['2023-03-20T05:00:16.960Z', '11.94'],
                    ['2023-03-20T04:00:15.051Z', '13.66'],
                    ['2023-03-20T03:00:05.754Z', '14.23'],
                    ['2023-03-20T02:00:08.835Z', '12.85'],
                ],
                stockName: 'AMZN',
            },
        ],
        options: any = {
            chart: {
                height: 300,
                type: 'line',
            },
            dataLabels: {
                enabled: false,
            },
            colors: props.data.apxChart.colors,
            fill: {
                colors: props.data.apxChart.colors,
            },
            legend: {
                show: true,
            },
            xaxis: {
                type: 'datetime',
                labels: {
                    show: true,
                    showDuplicates: false,
                    datetimeUTC: false,
                    format: undefined,
                    formatter: undefined,
                    datetimeFormatter: {
                        year: 'yyyy',
                        month: 'yy/MM',
                        day: 'MM-dd',
                        hour: 'HH:mm',
                        minute: 'HH:mm',
                    },
                },
                tooltip: {
                    enabled: false,
                },
            },
            yaxis: {
                axisBorder: {
                    show: false,
                },
                axisTicks: {
                    show: false,
                },
            },
        };

 

Series data

μ—¬κΈ°μ„œ μš°μ„  series 데이터λ₯Ό μžμ„Ένžˆ μ‚΄νŽ΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€. λ°°μ—΄ μ•ˆμ— 총 3개의 object dataκ°€ λ“€μ–΄ μžˆμŠ΅λ‹ˆλ‹€. name, data, stockName의 3개의 ν”„λ‘œνΌν‹°λ₯Ό κ°–κ³  있고, data의 경우 μ‹œκ°„κ³Ό μˆ«μžκ°’μ„ 가진 λ°°μ—΄λ‘œ 이루어져 μžˆμŠ΅λ‹ˆλ‹€. 

[
    {
        name: 'EBAY',
        data: [
            ['2023-03-20T07:00:22.625Z', '123124.82'],
            ['2023-03-20T06:00:20.103Z', '52353.83'],
            ['2023-03-20T05:00:16.960Z', '98943.91'],
            ['2023-03-20T04:00:15.051Z', '796756.65'],
            ['2023-03-20T03:00:05.754Z', '4831.18'],
            ['2023-03-20T02:00:08.835Z', '13.75'],
        ],
        stockName: 'EBAY',
    },
    {
        name: 'AAPL',
        data: [
            ['2023-03-20T07:00:22.625Z', '6756.78'],
            ['2023-03-20T06:00:20.103Z', '345547.78'],
            ['2023-03-20T05:00:16.960Z', '1321.95'],
            ['2023-03-20T04:00:15.051Z', '235890.64'],
            ['2023-03-20T03:00:05.754Z', '32.20'],
            ['2023-03-20T02:00:08.835Z', '12314.79'],
        ],
        stockName: 'AAPL',
    },
    {
        name: 'AMZN',
        data: [
            ['2023-03-20T07:00:22.625Z', '4.89'],
            ['2023-03-20T06:00:20.103Z', '8.84'],
            ['2023-03-20T05:00:16.960Z', '11.94'],
            ['2023-03-20T04:00:15.051Z', '13.66'],
            ['2023-03-20T03:00:05.754Z', '14.23'],
            ['2023-03-20T02:00:08.835Z', '12.85'],
        ],
        stockName: 'AMZN',
    },
],

 

Options

λ‹€μŒμ€ options μž…λ‹ˆλ‹€. series 데이터 (EBAY, AAPL, AMZN) 의 line 차트λ₯Ό κ·Έλ €μ£ΌλŠ” μ˜΅μ…˜μ„ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 

options: any = {
    chart: {
        height: 300,
        type: 'line',
    },
    dataLabels: {
        enabled: false,
    },
    colors: props.data.apxChart.colors,
    fill: {
        colors: props.data.apxChart.colors,
    },
    legend: {
        show: true,
    },
    xaxis: {
        type: 'datetime',
        labels: {
            show: true,
            showDuplicates: false,
            datetimeUTC: false,
            format: undefined,
            formatter: undefined,
            datetimeFormatter: {
                year: 'yyyy',
                month: 'yy/MM',
                day: 'MM-dd',
                hour: 'HH:mm',
                minute: 'HH:mm',
            },
        },
        tooltip: {
            enabled: false,
        },
    },
    yaxis: {
        axisBorder: {
            show: false,
        },
        axisTicks: {
            show: false,
        },
    },
};

 

Custom Tooltip

μœ„ series와 options을 λ°”νƒ•μœΌλ‘œ μ»€μŠ€ν…€ νˆ΄νŒμ„ κ΅¬ν˜„ν•˜λŠ” 방법은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€. μ»€μŠ€ν…€μ— μ‚¬μš©λ  μ‹€μ œ html μ½”λ“œλ₯Ό λ„£μ–΄μ£Όλ©΄ λ©λ‹ˆλ‹€. 

 

μš°μ„  μ €μ˜ 경우, x좕에 ν•΄λ‹Ήν•˜λŠ” μ‹œκ°„κ³Ό 값을 ν‘œν˜„ν•˜κ³  μ‹Άμ—ˆκΈ° λ•Œλ¬Έμ— μ•„λž˜μ™€ 같이 w.config.seriesλ₯Ό μ‚¬μš©ν•˜μ—¬ dateTime 값을 λ§Œλ“€μ–΄μ€¬μŠ΅λ‹ˆλ‹€. dateTimeκ°’ ν‘œμΆœμ‹œμ—λŠ” moment.jsλΌλŠ” λ‚ μ§œ 라이브러리λ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. data νŠΉμ„±μƒ data[0]이 μ‹œκ°„, data[1]이 μˆ«μžκ°’μ„ κ°–κ³  있기 λ•Œλ¬Έμ—, valueλŠ” dataPoint[1]와 같이 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€. 

 

특히 μ»€μŠ€ν…€ 툴팁의 CSSλŠ” 각각 λ‹€λ₯Ό ν…λ°μš”,  <div class="tooltip">으둜 μ„ μ–Έν•œ λ’€, tooltip κ΄€λ ¨ cssλ₯Ό css νŒŒμΌμ— μ •μ˜ν•˜λ©΄ μ μš©λ©λ‹ˆλ‹€. 

tooltip: {
    custom: ({series, seriesIndex, dataPointIndex, w}: any) => {
        const dataPoint = w.config.series[seriesIndex].data[dataPointIndex];
        const date = moment(w.config.series[seriesIndex].data[dataPointIndex][0]);
        const dateTime = date.format('YYYYMMDD HH:mm');
        const value = dataPoint[1];
        let html = '<div class="tooltip">';
        html += `<p>${dateTime}</p>`;
        html += '<ul>';
        series.forEach((item: any, index: number) => {
            html += `<li><i style="background-color: ${w.globals.colors[index]};"></i> <strong>${
                w.globals.seriesNames[index]
            }${value}</li>`;
        });
        html += '</div>';
        return html;
    },
},
λ°˜μ‘ν˜•