Reputation: 29
I want to highlight the selected Y-axis label in Gantt charts. Also is there any way through which we can give some background color to the y-axis title?
yAxis: { className: "highcharts-color-0", uniqueNames: true, title: { text: "Data" }, labels: { events: { click: function () { alert("hellowww"); var chart = this, series = chart.series, plotLeft = chart.plotLeft, plotTop = chart.plotTop, plotWidth = chart.plotWidth; if (chart.myBackground) { chart.myBackground.destroy(); }
chart.myBackground = chart.renderer
.rect(10, plotTop, 500, 30, 1)
.attr({
"stroke-width": 2,
stroke: "red",
fill: "yellow",
opacity: 0.5,
zIndex: -1
})
.add();
}
}
}
}
link to code pen https://codepen.io/mehrotrarohit07/pen/PoKxvQp?editors=1010
Upvotes: 0
Views: 470
Reputation: 11633
Try to use this approach:
yAxis: {
labels: {
events: {
click: function() {
const chart = this.chart;
const axis = this.axis;
const labelPos = this.pos;
const tick = axis.ticks[labelPos]
const x = chart.marginRight;
const width = tick.slotWidth;
const height = axis.height / (axis.tickPositions.length);
const y = axis.top + labelPos * height;
chart.renderer
.rect(x, y, tick.slotWidth, height)
.attr({
fill: 'yellow',
zIndex: 0
})
.add();
}
}
}
},
I hope that everything is clear from the code - in case of any doubts feel free to ask.
Demo: https://jsfiddle.net/BlackLabel/qyj327Lx/
API: https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#rect
Upvotes: 1