그래프 호버시 생성되는 tooltip과 맨오른쪽 label에 로고와 함께 출력하려고 한다.
option 생성의 tooltip옵션 formatter를 이용해 이미지가 추가된 html템플릿을 출력한다.
SeriesOption
의 rich속성을 통해 label에 이미지를 추가한다.
EChartsOption
의 tooltip.formatter를 통해 string(html 템플릿을 반환한다.
public generateOption(teamList: TTeamList): EChartsOption {
return {
title: { text: '팀 순위 그래프' }, // 그래프 타이틀
tooltip: {
// 호버시 띄울 tooltip
trigger: 'item',
formatter: function (params) {
if (Array.isArray(params)) return ``;
return makeTooltipHTMLWithLogo(
params.seriesName as string,
params.name,
params.value as string | number,
teamList,
);
},
},
...
};
}
makeTooltipHTMLWithLogo
함수를 통해 html 템플릿을 반환function makeTooltipHTMLWithLogo(
seriesName: string, // 팀이름
name: string, // 주차
value: string | number, // 순위
teamList: TTeamList,
): string {
const teamLogo = teamList.find((team) => team.leagueTeamName === seriesName)?.logo;
return `
<div style="display: flex">
${teamLogo ? `<img style="width:24px; height:24px; margin-right: 4px" src=${teamLogo} alt=${teamLogo}/>` : null} <span>${seriesName}</span>
</div>
<div style="display: flex; justify-content: space-between;">
<span>${name}</span>
<span style="font-weight: bold">${value}위</span>
</div>`;
}
private generateSeriesList(teamList: TTeamList): SeriesOption[] {
const rich = this.makeRich(teamList);
return Object.entries(this.rankingData).map(([name, data]) => ({
name,
symbolSize: 15,
type: 'line',
smooth: true,
emphasis: { focus: 'series' },
endLabel: {
show: true,
formatter: (params) => {
const teamName = params.seriesName?.replace(reg, '');
return `{${teamName}|} ${params.seriesName}`;
},
distance: 20,
rich,
},
lineStyle: { width: 4 },
data,
}));
}
teamName
의 string 템플릿을 만들어 출력한다.private makeRich(teamList: TTeamList) {
const rich: {
[key: number | string]: {
backgroundColor: {
image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | string;
};
height: number;
};
} = {};
Object.entries(this.rankingData).forEach(([name]) => {
const teamName = name.replace(reg, '');
const teamLogo = teamList.find((team) => team.leagueTeamName === name)?.logo;
rich[teamName] = {
backgroundColor: {
image: teamLogo || '',
},
height: 20,
};
});
return rich;
}
formatter: (params) => {
const teamName = params.seriesName?.replace(reg, '');
return `{${teamName}|} ${params.seriesName}`;
},
https://echarts.apache.org/en/option.html#series-line.endLabel.formatter