import Formatters from '../Formatters' import DateTime from '../../utils/DateTime' import Utils from './Utils' /** * ApexCharts Tooltip.Labels Class to draw texts on the tooltip. * This file deals with printing actual text on the tooltip. * * @module Tooltip.Labels **/ export default class Labels { constructor(tooltipContext) { this.w = tooltipContext.w this.ctx = tooltipContext.ctx this.ttCtx = tooltipContext this.tooltipUtil = new Utils(tooltipContext) } drawSeriesTexts({ shared = true, ttItems, i = 0, j = null, y1, y2, e }) { let w = this.w if (w.config.tooltip.custom !== undefined) { this.handleCustomTooltip({ i, j, y1, y2, w }) } else { this.toggleActiveInactiveSeries(shared) } let values = this.getValuesToPrint({ i, j }) this.printLabels({ i, j, values, ttItems, shared, e }) // Re-calculate tooltip dimensions now that we have drawn the text const tooltipEl = this.ttCtx.getElTooltip() this.ttCtx.tooltipRect.ttWidth = tooltipEl.getBoundingClientRect().width this.ttCtx.tooltipRect.ttHeight = tooltipEl.getBoundingClientRect().height } printLabels({ i, j, values, ttItems, shared, e }) { const w = this.w let val let goalVals = [] const hasGoalValues = (gi) => { return ( w.globals.seriesGoals[gi] && w.globals.seriesGoals[gi][j] && Array.isArray(w.globals.seriesGoals[gi][j]) ) } const { xVal, zVal, xAxisTTVal } = values let seriesName = '' let pColor = w.globals.colors[i] // The pColor here is for the markers inside tooltip if (j !== null && w.config.plotOptions.bar.distributed) { pColor = w.globals.colors[j] } for ( let t = 0, inverset = w.globals.series.length - 1; t < w.globals.series.length; t++, inverset-- ) { let f = this.getFormatters(i) seriesName = this.getSeriesName({ fn: f.yLbTitleFormatter, index: i, seriesIndex: i, j }) if (w.config.chart.type === 'treemap') { seriesName = f.yLbTitleFormatter(String(w.config.series[i].data[j].x), { series: w.globals.series, seriesIndex: i, dataPointIndex: j, w }) } const tIndex = w.config.tooltip.inverseOrder ? inverset : t if (w.globals.axisCharts) { const getValBySeriesIndex = (index) => { return f.yLbFormatter(w.globals.series[index][j], { series: w.globals.series, seriesIndex: index, dataPointIndex: j, w }) } if (shared) { f = this.getFormatters(tIndex) seriesName = this.getSeriesName({ fn: f.yLbTitleFormatter, index: tIndex, seriesIndex: i, j }) pColor = w.globals.colors[tIndex] val = getValBySeriesIndex(tIndex) if (hasGoalValues(tIndex)) { goalVals = w.globals.seriesGoals[tIndex][j].map((goal) => { return { attrs: goal, val: f.yLbFormatter(goal.value, { seriesIndex: tIndex, dataPointIndex: j, w }) } }) } } else { // get a color from a hover area (if it's a line pattern then get from a first line) const targetFill = e?.target?.getAttribute('fill'); if (targetFill) { pColor = (targetFill.indexOf("url") !== -1) ? ( document .querySelector(targetFill.substr(4).slice(0, -1)) .childNodes[0] .getAttribute("stroke") ) : targetFill; } val = getValBySeriesIndex(i) if (hasGoalValues(i) && Array.isArray(w.globals.seriesGoals[i][j])) { goalVals = w.globals.seriesGoals[i][j].map((goal) => { return { attrs: goal, val: f.yLbFormatter(goal.value, { seriesIndex: i, dataPointIndex: j, w }) } }) } } } // for pie / donuts if (j === null) { val = f.yLbFormatter(w.globals.series[i], { ...w, seriesIndex: i, dataPointIndex: i }) } this.DOMHandling({ i, t: tIndex, j, ttItems, values: { val, goalVals, xVal, xAxisTTVal, zVal }, seriesName, shared, pColor }) } } getFormatters(i) { const w = this.w let yLbFormatter = w.globals.yLabelFormatters[i] let yLbTitleFormatter if (w.globals.ttVal !== undefined) { if (Array.isArray(w.globals.ttVal)) { yLbFormatter = w.globals.ttVal[i] && w.globals.ttVal[i].formatter yLbTitleFormatter = w.globals.ttVal[i] && w.globals.ttVal[i].title && w.globals.ttVal[i].title.formatter } else { yLbFormatter = w.globals.ttVal.formatter if (typeof w.globals.ttVal.title.formatter === 'function') { yLbTitleFormatter = w.globals.ttVal.title.formatter } } } else { yLbTitleFormatter = w.config.tooltip.y.title.formatter } if (typeof yLbFormatter !== 'function') { if (w.globals.yLabelFormatters[0]) { yLbFormatter = w.globals.yLabelFormatters[0] } else { yLbFormatter = function (label) { return label } } } if (typeof yLbTitleFormatter !== 'function') { yLbTitleFormatter = function (label) { return label } } return { yLbFormatter, yLbTitleFormatter } } getSeriesName({ fn, index, seriesIndex, j }) { const w = this.w return fn(String(w.globals.seriesNames[index]), { series: w.globals.series, seriesIndex, dataPointIndex: j, w }) } DOMHandling({ i, t, j, ttItems, values, seriesName, shared, pColor }) { const w = this.w const ttCtx = this.ttCtx const { val, goalVals, xVal, xAxisTTVal, zVal } = values let ttItemsChildren = null ttItemsChildren = ttItems[t].children if (w.config.tooltip.fillSeriesColor) { ttItems[t].style.backgroundColor = pColor ttItemsChildren[0].style.display = 'none' } if (ttCtx.showTooltipTitle) { if (ttCtx.tooltipTitle === null) { // get it once if null, and store it in class property ttCtx.tooltipTitle = w.globals.dom.baseEl.querySelector( '.apexcharts-tooltip-title' ) } ttCtx.tooltipTitle.innerHTML = xVal } // if xaxis tooltip is constructed, we need to replace the innerHTML if (ttCtx.isXAxisTooltipEnabled) { ttCtx.xaxisTooltipText.innerHTML = xAxisTTVal !== '' ? xAxisTTVal : xVal } const ttYLabel = ttItems[t].querySelector( '.apexcharts-tooltip-text-y-label' ) if (ttYLabel) { ttYLabel.innerHTML = seriesName ? seriesName : '' } const ttYVal = ttItems[t].querySelector('.apexcharts-tooltip-text-y-value') if (ttYVal) { ttYVal.innerHTML = typeof val !== 'undefined' ? val : '' } if ( ttItemsChildren[0] && ttItemsChildren[0].classList.contains('apexcharts-tooltip-marker') ) { if ( w.config.tooltip.marker.fillColors && Array.isArray(w.config.tooltip.marker.fillColors) ) { pColor = w.config.tooltip.marker.fillColors[t] } ttItemsChildren[0].style.backgroundColor = pColor } if (!w.config.tooltip.marker.show) { ttItemsChildren[0].style.display = 'none' } const ttGLabel = ttItems[t].querySelector( '.apexcharts-tooltip-text-goals-label' ) const ttGVal = ttItems[t].querySelector( '.apexcharts-tooltip-text-goals-value' ) if (goalVals.length && w.globals.seriesGoals[t]) { const createGoalsHtml = () => { let gLabels = '