HighStock:使用zoomType =' xy'进行缩放时趋势跳出图表区域或zoomType =' y'

时间:2018-05-08 23:03:35

标签: highstock

我正在使用HighCharts(版本:^ 5.0.14)和React(反应:^ 15.0.1,react-dom:^ 15.0.1)。

我正面临HighStock Chart Zooming的问题。使用zoomType =' xy'或zoomType =' y',X轴和Y轴的放大HighStock趋势跳跃。下面是图片。但是,如果缩放类型zoomType =' X'没有问题。包含zoomType =' y'

时会遇到问题

enter image description here

以下是代码:

import React from 'react';
import highcharts from 'highcharts/highstock';

export default class Chart extends React.Component {

  constructor (props) {
     super(props);
     this.state = {
       chartName: `chart${this.props.chartNum}`
      };
   }

componentDidMount(){

let chartOptions = {};

chartOptions = {
        rangeSelector: {enabled: false },
    credits: { enabled: false },
    tooltip: {
    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y:.2f}</b> <br/>'
    },
    navigator: {
    enabled: false
    }, 
    scrollbar: {
    enabled: false
    },
    plotOptions:{
    series:{
        turboThreshold:5000//set it to a larger threshold, it is by default to 1000,
    }
    },
    xAxis:{
    type: 'dateTime',
    minRange:3600000,
    events:{
        setExtremes:function(event){
        this.min=event.min;
        this.max=event.max;
        }
    }
    },
    yAxis:   {
    offset: 15,
    labels: {
        align: 'center',
        x: -5,
        y: 0
    },
    opposite:false,
    showLastLabel: true,
    min: this.dataMin,
    max: this.dataMax,
    events:{
        setExtremes:function(events){
        console.log("Card::YAxis::setExtremes::events.min and events.max = " + events.min +", "+ events.max);
        this.dataMin = events.min;
        this.dataMax = events.Max;
        this.min = events.min;
        this.max = events.max;
        }
    },
    tickPositioner: function () {
        var positions;
        var Maxdata = this.max;
        var MinData = this.min;
        if(ticksData != null){
        positions = ticksData;
        }else{
        positions = [];
        var tick = Math.floor(this.min);
        if(this.min == this.max){
            positions.push(this.min);
        }else{
            if (this.max !== null && this.min !== null) {
            Maxdata = Maxdata.toFixed(2);
            MinData = MinData.toFixed(2);
            tick = parseFloat(MinData);
            var increment = (Maxdata - MinData)/5;
            increment=increment.toFixed(2);
            for (tick; tick-increment<=  Maxdata; tick=(parseFloat(increment)+parseFloat(tick)).toFixed(2)) {
                if(positions.includes(tick)){
                break;
                }
                else{
                positions.push(tick);
                }                                
            }
            }else{
            positions = [0];
            }
        }     
        }
        return positions;
    }
        }

};

chartOptions.chart = {
    renderTo: document.getElementById(this.state.chartName),
    zoomType: 'xy',
    spacingBottom: 35,
    resetZoomButton: {
        position: {
            align: 'right', // by default
            verticalAlign: 'bottom', // by default
            x: 8,
            y: 20
        }
    },
    events:{
        redraw: function(){
            var serie1 = this.series[0],
            yExtremes = this.yAxis[0].getExtremes();

            var yMin = yExtremes.min;
            var yMax = yExtremes.max;


            var dataSet = serie1.data;
            var zoomedDataSet= [];
            var dataGroup;
            if(dataSet != undefined && dataSet != null){
                for(var i=0; i<dataSet.length; i++)
                {
                    if(dataSet[i] != undefined){
                        dataGroup = dataSet[i];
                        if(dataGroup[1] > yMin && dataGroup[1]< yMax){
                            zoomedDataSet.push(dataGroup);
                        }
                    }
                }
                serie1.data = zoomedDataSet;
            }
        }
    }

};

// Generate chart.
this.generateChart(this.props.data, chartOptions);

}

componentWillUpdate(nextProps){

let chartOptions = {};
chartOptions = {
    rangeSelector: {enabled: false },
    credits: { enabled: false },
    tooltip: {
    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y:.2f}</b> <br/>'
    },
    navigator: {
    enabled: false
    }, 
    scrollbar: {
    enabled: false
    },
    plotOptions:{
    series:{
        turboThreshold:5000//set it to a larger threshold, it is by default to 1000,
    }
    },
    xAxis:{
    type: 'dateTime',
    minRange:3600000,
    events:{
        setExtremes:function(event){
        this.min=event.min;
        this.max=event.max;
        }
    }
    },
    yAxis:   {
    offset: 15,
    labels: {
        align: 'center',
        x: -5,
        y: 0
    },
    opposite:false,
    showLastLabel: true,
    min: this.dataMin,
    max: this.dataMax,
    events:{
        setExtremes:function(events){
        console.log("Card::YAxis::setExtremes::events.min and events.max = " + events.min +", "+ events.max);
        this.dataMin = events.min;
        this.dataMax = events.Max;
        this.min = events.min;
        this.max = events.max;
        }
    },
    tickPositioner: function () {
        var positions;
        var Maxdata = this.max;
        var MinData = this.min;
        if(ticksData != null){
        positions = ticksData;
        }else{
        positions = [];
        var tick = Math.floor(this.min);
        if(this.min == this.max){
            positions.push(this.min);
        }else{
            if (this.max !== null && this.min !== null) {
            Maxdata = Maxdata.toFixed(2);
            MinData = MinData.toFixed(2);
            tick = parseFloat(MinData);
            var increment = (Maxdata - MinData)/5;
            increment=increment.toFixed(2);
            for (tick; tick-increment<=  Maxdata; tick=(parseFloat(increment)+parseFloat(tick)).toFixed(2)) {
                if(positions.includes(tick)){
                break;
                }
                else{
                positions.push(tick);
                }                                
            }
            }else{
            positions = [0];
            }
        }     
        }
        return positions;
    }
    }

};



  chartOptions.chart = {
      renderTo: document.getElementById(this.state.chartName),
      zoomType: 'xy',
      spacingBottom:35,
      resetZoomButton: {
          position: {
              align: 'right', // by default
              verticalAlign: 'bottom', // by default
              x: 8,
              y: 20
          }
      },
      events:{
          redraw: function(){
              var serie1 = this.series[0],
              yExtremes = this.yAxis[0].getExtremes(),
              var yMin = yExtremes.min;
              var yMax = yExtremes.max;


              var dataSet = serie1.data;
              var zoomedDataSet= [];
              if(dataSet != undefined && dataSet != null ){
                  for(var i=0; i<dataSet.length; i++)
                  {   
                    if(dataSet[i] != undefined){
              dataGroup = dataSet[i];
              if(dataGroup[1] >= yMin && dataGroup[1]<=yMax  && dataGroup[0] >= xMin && dataGroup[0]<= xMax){
              zoomedDataSet.push(dataGroup);
              }
                    }
                  }
                  serie1.data = zoomedDataSet;
              }
          }
      }
  };
this.generateChart(nextProps.data, chartOptions);

}

generateChart(data,chartOptions){

chartOptions.series = [
  {
    name: this.props.title,
    type: this.props.status ? 'area' : 'line',
    data: data,
    clip: false
  }
];
let chart = new highcharts.StockChart(chartOptions);

}

render(){

  const selfStyle = {
     height: '200px'
  };

  return (
            <div 
                 className="med-chart col-md-9" 
                 id={this.state.chartName} 
                 style={selfStyle}>
            </div>
  );
}

}

我已将setExtremes设置为Y轴的事件最小值和最大值,但它没有帮助。如果您有任何人遇到类似问题,请求分享您解决此问题的方法。非常感谢您提前阅读帖子和宝贵意见。

0 个答案:

没有答案