Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent stuck tooltip for line & candlestick charts #3664

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/charts/BoxCandleStick.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import Utils from '../utils/Utils'
**/

class BoxCandleStick extends Bar {
draw(series, seriesIndex) {
draw(series, ctype, seriesIndex) {
let w = this.w
let graphics = new Graphics(this.ctx)
let type = w.globals.comboCharts ? ctype : w.config.chart.type
let fill = new Fill(this.ctx)

this.candlestickOptions = this.w.config.plotOptions.candlestick
Expand All @@ -28,7 +29,7 @@ class BoxCandleStick extends Bar {
this.barHelpers.initVariables(series)

let ret = graphics.group({
class: `apexcharts-${w.config.chart.type}-series apexcharts-plot-series`
class: `apexcharts-${type}-series apexcharts-plot-series`
})

for (let i = 0; i < series.length; i++) {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ export default class Core {
}
if (candlestickSeries.series.length > 0) {
elGraph.push(
boxCandlestick.draw(candlestickSeries.series, candlestickSeries.i)
boxCandlestick.draw(candlestickSeries.series, 'candlestick', candlestickSeries.i)
)
}
if (boxplotSeries.series.length > 0) {
elGraph.push(boxCandlestick.draw(boxplotSeries.series, boxplotSeries.i))
elGraph.push(boxCandlestick.draw(boxplotSeries.series, 'boxPlot', boxplotSeries.i))
}
if (rangeBarSeries.series.length > 0) {
elGraph.push(
Expand Down Expand Up @@ -306,11 +306,11 @@ export default class Core {
break
case 'candlestick':
let candleStick = new BoxCandleStick(this.ctx, xyRatios)
elGraph = candleStick.draw(gl.series)
elGraph = candleStick.draw(gl.series, 'candlestick')
break
case 'boxPlot':
let boxPlot = new BoxCandleStick(this.ctx, xyRatios)
elGraph = boxPlot.draw(gl.series)
elGraph = boxPlot.draw(gl.series, 'boxPlot')
break
case 'rangeBar':
elGraph = this.ctx.rangeBar.draw(gl.series)
Expand Down
15 changes: 12 additions & 3 deletions src/modules/tooltip/Position.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export default class Position {
}
}

moveStickyTooltipOverBars(j) {
moveStickyTooltipOverBars(j, capturedSeries) {
const w = this.w
const ttCtx = this.ttCtx

Expand All @@ -377,6 +377,15 @@ export default class Position {
let jBar = w.globals.dom.baseEl.querySelector(
`.apexcharts-bar-series .apexcharts-series[rel='${i}'] path[j='${j}'], .apexcharts-candlestick-series .apexcharts-series[rel='${i}'] path[j='${j}'], .apexcharts-boxPlot-series .apexcharts-series[rel='${i}'] path[j='${j}'], .apexcharts-rangebar-series .apexcharts-series[rel='${i}'] path[j='${j}']`
)
if (!jBar && typeof capturedSeries == 'number') {
// Try with captured series index
jBar = w.globals.dom.baseEl.querySelector(
`.apexcharts-bar-series .apexcharts-series[data\\:realIndex='${capturedSeries}'] path[j='${j}'],
.apexcharts-candlestick-series .apexcharts-series[data\\:realIndex='${capturedSeries}'] path[j='${j}'],
.apexcharts-boxPlot-series .apexcharts-series[data\\:realIndex='${capturedSeries}'] path[j='${j}'],
.apexcharts-rangebar-series .apexcharts-series[data\\:realIndex='${capturedSeries}'] path[j='${j}']`
);
}

let bcx = jBar ? parseFloat(jBar.getAttribute('cx')) : 0
let bcy = jBar ? parseFloat(jBar.getAttribute('cy')) : 0
Expand All @@ -386,9 +395,9 @@ export default class Position {
const elGrid = ttCtx.getElGrid()
let seriesBound = elGrid.getBoundingClientRect()

const isBoxOrCandle =
const isBoxOrCandle = jBar && (
jBar.classList.contains('apexcharts-candlestick-area') ||
jBar.classList.contains('apexcharts-boxPlot-area')
jBar.classList.contains('apexcharts-boxPlot-area'))
if (w.globals.isXNumeric) {
if (jBar && !isBoxOrCandle) {
bcx = bcx - (barLen % 2 !== 0 ? bw / 2 : 0)
Expand Down
17 changes: 11 additions & 6 deletions src/modules/tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@ export default class Tooltip {
let j = capj.j
let capturedSeries = capj.capturedSeries

if (w.globals.collapsedSeriesIndices.includes(capturedSeries))
capturedSeries = null

const bounds = opt.elGrid.getBoundingClientRect()
if (capj.hoverX < 0 || capj.hoverX > bounds.width) {
this.handleMouseOut(opt)
Expand All @@ -681,7 +684,8 @@ export default class Tooltip {
// couldn't capture any series. check if shared X is same,
// if yes, draw a grouped tooltip
if (this.tooltipUtil.isXoverlap(j) || w.globals.isBarHorizontal) {
this.create(e, this, 0, j, opt.ttItems)
const firstVisibleSeries = w.globals.series.findIndex((s,i) => !w.globals.collapsedSeriesIndices.includes(i))
this.create(e, this, firstVisibleSeries, j, opt.ttItems)
}
}
}
Expand All @@ -708,7 +712,8 @@ export default class Tooltip {
}
} else {
if (this.tooltipUtil.isXoverlap(j)) {
this.create(e, this, 0, j, opt.ttItems)
const firstVisibleSeries = w.globals.series.findIndex((s,i) => !w.globals.collapsedSeriesIndices.includes(i));
this.create(e, this, firstVisibleSeries, j, opt.ttItems)
}
}
}
Expand Down Expand Up @@ -788,7 +793,7 @@ export default class Tooltip {

if (shared === null) shared = this.tConfig.shared

const hasMarkers = this.tooltipUtil.hasMarkers()
const hasMarkers = this.tooltipUtil.hasMarkers(capturedSeries)

const bars = this.tooltipUtil.getElBars()

Expand Down Expand Up @@ -847,7 +852,7 @@ export default class Tooltip {
}
}

if (this.tooltipUtil.hasBars()) {
else if (this.tooltipUtil.hasBars()) {
this.barSeriesHeight = this.tooltipUtil.getBarsHeight(bars)
if (this.barSeriesHeight > 0) {
// hover state, activate snap filter
Expand All @@ -859,7 +864,7 @@ export default class Tooltip {
// de-activate first
this.deactivateHoverFilter()

this.tooltipPosition.moveStickyTooltipOverBars(j)
this.tooltipPosition.moveStickyTooltipOverBars(j, capturedSeries)

for (let b = 0; b < paths.length; b++) {
graphics.pathMouseEnter(paths[b])
Expand All @@ -875,7 +880,7 @@ export default class Tooltip {
})

if (this.tooltipUtil.hasBars()) {
ttCtx.tooltipPosition.moveStickyTooltipOverBars(j)
ttCtx.tooltipPosition.moveStickyTooltipOverBars(j, capturedSeries)
}

if (hasMarkers) {
Expand Down
11 changes: 8 additions & 3 deletions src/modules/tooltip/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ export default class Utils {
return totalHeight
}

getElMarkers() {
getElMarkers(capturedSeries) {
if (typeof capturedSeries == 'number') {
return this.w.globals.dom.baseEl.querySelectorAll(
`.apexcharts-series[data\\:realIndex='${capturedSeries}'] .apexcharts-series-markers`
)
}
return this.w.globals.dom.baseEl.querySelectorAll(
' .apexcharts-series-markers'
)
Expand Down Expand Up @@ -308,8 +313,8 @@ export default class Utils {
return markers
}

hasMarkers() {
const markers = this.getElMarkers()
hasMarkers(capturedSeries) {
const markers = this.getElMarkers(capturedSeries)
return markers.length > 0
}

Expand Down