Skip to content

Commit

Permalink
[#1587] Chart > tooltip > formatter > html > 동적으로 사용여부 적용 불가한 이슈 발생
Browse files Browse the repository at this point in the history
#####
- formatter가 변경된 경우, tooltip DOM 제거 후 다시 생성하도록 로직 추가
  • Loading branch information
jhee564 committed Jan 19, 2024
1 parent 11762aa commit ded1846
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
78 changes: 50 additions & 28 deletions docs/views/lineChart/example/CustomTooltip.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<template>
<ev-chart
:data="chartData"
:options="chartOptions"
/>
<div class="case">
<ev-chart
:data="chartData"
:options="chartOptions"
/>
<div class="description">
<span class="toggle-label">HTML Tooltip 사용</span>
<ev-toggle
v-model="useHtml"
/>
</div>
</div>

</template>

<script>
import { onMounted, reactive } from 'vue';
import { onMounted, reactive, ref, watch } from 'vue';
import dayjs from 'dayjs';
export default {
Expand All @@ -25,6 +34,31 @@ export default {
},
});
const useHtml = ref(true);
const htmlTooltipFormatter = {
html: (seriesList) => {
let result = '<div class="ev-chart-tooltip-custom" style="width: 250px">';
result += `<div class="ev-chart-tooltip-custom__header"> ${dayjs(seriesList?.[0]?.data?.x).format('mm:ss')}</div>`;
result += '<div class="ev-chart-tooltip-custom__body">';
seriesList.forEach((series) => {
result += '<br/>';
result += '<div class="row">';
result += `<div class="color-circle" style="background-color: ${series.color}"></div>`;
result += `<div class="series-name">${series.name} 값 </div>`;
result += `<div class="value">${series.data?.y}</div>`;
result += '</div>';
result += '<div class="row">';
result += `<div class="color-circle" style="background-color: ${series.color}"></div>`;
result += '<div class="series-name">전체 합계 </div>';
result += `<div class="value">${chartData.data[series?.sId].reduce((a, b) => a + b, 0)}</div>`;
result += '</div>';
});
result += '</div></div>';
return result;
},
};
const chartOptions = reactive({
type: 'line',
width: '100%',
Expand All @@ -49,32 +83,19 @@ export default {
}],
tooltip: {
use: true,
formatter: {
html: (seriesList) => {
let result = '<div class="ev-chart-tooltip-custom" style="width: 250px">';
result += `<div class="ev-chart-tooltip-custom__header"> ${dayjs(seriesList?.[0]?.data?.x).format('mm:ss')}</div>`;
result += '<div class="ev-chart-tooltip-custom__body">';
seriesList.forEach((series) => {
result += '<br/>';
result += '<div class="row">';
result += `<div class="color-circle" style="background-color: ${series.color}"></div>`;
result += `<div class="series-name">${series.name} 값 </div>`;
result += `<div class="value">${series.data?.y}</div>`;
result += '</div>';
result += '<div class="row">';
result += `<div class="color-circle" style="background-color: ${series.color}"></div>`;
result += '<div class="series-name">전체 합계 </div>';
result += `<div class="value">${chartData.data[series?.sId].reduce((a, b) => a + b, 0)}</div>`;
result += '</div>';
});
result += '</div></div>';
return result;
},
},
},
});
watch(useHtml, () => {
if (useHtml.value) {
chartOptions.tooltip.formatter = htmlTooltipFormatter;
} else {
chartOptions.tooltip.formatter = null;
}
}, {
immediate: true,
});
let timeValue = dayjs().format('YYYY-MM-DD HH:mm:ss');
const addRandomChartData = () => {
timeValue = dayjs(timeValue).add(1, 'second');
Expand All @@ -94,6 +115,7 @@ export default {
return {
chartData,
chartOptions,
useHtml,
};
},
};
Expand Down
5 changes: 5 additions & 0 deletions src/components/chart/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@
watch(() => props.options, (chartOpt) => {
const newOpt = getNormalizedOptions(chartOpt);
const isUpdateLegendType = !isEqual(newOpt.legend.table, evChart.options.legend.table);
const isUpdateTooltipFormatter = !isEqual(
newOpt.tooltip.formatter,
evChart.options.tooltip.formatter,
);
evChart.options = cloneDeep(newOpt);
evChart.update({
updateSeries: false,
updateSelTip: { update: false, keepDomain: false },
updateLegend: isUpdateLegendType,
updateTooltipFormatter: isUpdateTooltipFormatter,
});
if (!injectIsChartGroup) {
Expand Down
16 changes: 15 additions & 1 deletion src/components/chart/chart.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,13 @@ class EvChart {
const groups = this.data.groups;
const series = this.data.series;

const { updateSeries, updateSelTip, updateLegend, updateData } = updateInfo;
const {
updateSeries,
updateSelTip,
updateLegend,
updateData,
updateTooltipFormatter,
} = updateInfo;

if (!this.isInit) {
return;
Expand Down Expand Up @@ -789,6 +795,7 @@ class EvChart {
this.hideTitle();
}

// legend Update
if (options.legend.show) {
const useTable = !!options.legend?.table?.use
&& options.type !== 'heatMap'
Expand All @@ -810,6 +817,13 @@ class EvChart {
} else if (this.isInitLegend) {
this.hideLegend();
}

// Tooltip Update
if (updateTooltipFormatter) {
this.tooltipDOM.innerHTML = '';
this.createTooltipDOM();
}

this.chartRect = this.getChartRect();

this.minMax = this.getStoreMinMax();
Expand Down

0 comments on commit ded1846

Please sign in to comment.