Я могу создать диаграмму и передать функцию для события mouseOver
, которая просто регистрирует значения x и y маркера, над которым я наведите курсор:
// Works
import React from 'react';
import { render } from 'react-dom';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
class App extends React.Component {
render = () => {
const options = {
chart: {
type: 'spline'
},
title: {
text: 'My chart'
},
series: [
{
data: [1, 2, 1, 4, 3, 6]
}
],
plotOptions: {
series: {
point: {
events: {
mouseOver: function () {
const point = { x: this.x, y: this.y };
console.log(point);
}
}
}
}
}
};
return (
<div>
<HighchartsReact highcharts={Highcharts} options={options} />
</div>
)
}
}
render(<App />, document.getElementById('root'));
В конечном итоге я хочу иметь возможность передать это point
обратно в мой компонент и использовать его в другом месте моего приложения.
Простая идея вызова функции в моем компоненте изнутри события не работает - this
, который я передаю в plotOptions
, больше не относится к моему компоненту, но относится к точке на диаграмме:
// Does not work
// Uncaught TypeError: this.handleMouseOver is not a function
import React from 'react';
import { render } from 'react-dom';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
class App extends React.Component {
handleMouseOver = point => {
console.log(`handleMouseOver gets ${point}`);
}
render = () => {
const options = {
chart: {
type: 'spline'
},
title: {
text: 'My chart'
},
series: [
{
data: [1, 2, 1, 4, 3, 6]
}
],
plotOptions: {
series: {
point: {
events: {
mouseOver: function () {
const point = { x: this.x, y: this.y };
this.handleMouseOver(point); <--- not the `this` of my component
}
}
}
}
}
};
return (
<div>
<HighchartsReact highcharts={Highcharts} options={options} />
</div>
)
}
}
render(<App />, document.getElementById('root'));
Не удивительно, ошибка, которую я получаю в браузере при наведении курсора на точку,
Uncaught TypeError: this.handleMouseOver is not a function
Есть предложения?
Спасибо.