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;
},
},