I'm facing a scope problem with state
within a react
app.
I'm adding some buttons to my chart
component at the top of high chart component.
Now, what I'd like is to add those buttons inside chart space (under title) to save some space.
The solution I found on older posts and forum was to use this method:
chart.events.render(this.renderer.button.add())
I'm not sure if this method is deprecated in favor of SVGRenderer
.
My approach was
chart: {
events: {
render() {
let chart = this;
console.log("this chart", chart);
const handlePeriodClick = (periodType) => {
const filteredData = chart.options.data.map((option) =>({
...option,
data: option.data.filter((item) =>
moment(item[0]).diff(moment(item[0]).endOf(periodType), 'days') === 0)
}));
// need to acces states from React within Highchart.chart component
this.setState({ filteredData });
};
chart.renderer.button('D',10, 55)
.on('click', function () {
handlePeriodClick('day');
})
.add();
chart.renderer.button('W',35, 55)
.on('click', function () {
handlePeriodClick('week');
})
.add();
chart.renderer.button('M',60, 55)
.on('click', function () {
handlePeriodClick('month');
})
.add();
}
}
},
I need to update state
inside that function call so as to reflect a data
update. Here is a working demo.
I've also tried to use SVGRenderer
instead of renderer
but couldn't make it work.