您可以DayPicker
在单击默认输入元素后显示组件并使用xAxis.setExtremes
方法来应用所选日期。
class HighchartsChart extends React.Component {
constructor(props) {
super(props);
this.state = {
minInput: false,
maxInput: false
};
}
selectDay(time, isMin){
const timestamp = time.getTime();
const xAxis = this.internalChart.xAxis[0];
xAxis.setExtremes(
isMin ? timestamp : xAxis.min,
isMin ? xAxis.max : timestamp
);
this.setState(isMin ? {minInput: false} : {maxInput: false});
}
render() {
return (
<div>
<HighchartsReact
constructorType={'stockChart'}
highcharts={Highcharts}
options={{
chart: {
events: {
load: (function(component){
return function(){
const inputGroup = this.rangeSelector.inputGroup.element.children;
inputGroup[1].addEventListener('click', function(){
component.setState({minInput: true});
});
inputGroup[3].addEventListener('click', function(){
component.setState({maxInput: true});
});
component.internalChart = this;
}
})(this),
}
},
...options
}}
/>
{
this.state.minInput &&
<DayPicker onDayClick={(time) => {
this.selectDay(time, true);
}} />
}
{
this.state.maxInput &&
<DayPicker onDayClick={(time) => {
this.selectDay(time);
}} />
}
</div>
);
}
}
现场演示: https ://codesandbox.io/s/highcharts-react-demo-mqqhu
API 参考: https ://api.highcharts.com/class-reference/Highcharts.Axis#setExtremes