Highcharts (Highstock) - Highcharts does not remov

2019-09-12 00:16发布

I am facing problem in removing yaxis in highcharts(highstock). I have the below snippet to explain the issue in a better way.

My chart loads with 3 initial yAxis and then I try to add or delete yAxis dynamically on the fly. For some reason its not removing the appropriate yaxis but removes series from another yAxis.

To reproduce the issue try to execute the following steps -

  1. Click on "Add yAxis" button
  2. Enter 1 in the text field and click on "delete yAxis" button
  3. Click on "delete yAxis" button again
  4. Click on add yAxis to add another axis
  5. Click again on add yAxis to add another axis
  6. Enter 2 in the text field and click on "delete yAxis" button

You will observe the yAxis we tried to remove got removed but it also removed the series from the last yAxis we added.

var chart;
$(function () {
  var index = 0;
  var data1 = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
    
  var highchartOptions = {
    chart:{
      renderTo:'container'
    },
    navigator: {
      outlineColor: '#0066DD',
      outlineWidth: 1
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis:[{
      title:{
        text:"initial data"
      },
      id:'myaxis-'+ index++,
      height:'14%',
      top:'0%'
  },
  {
    title:{
      text:"initial data"
    },
    id:'myaxis-'+ index++,
    top:'15%',
    height:'14%'
  },
  {
    title:{
      text:"initial data"
    },
    id:'myaxis-'+ index++,
    top:'30%',
    height:'14%'
  }],

  series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
  },
  {
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
    yAxis:1
  },
  {
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
    yAxis:2
  }]
};
    
  chart = new Highcharts.StockChart(highchartOptions);
  $button = $('#button');
  $delButton = $('#delbutton');
	var axisCount = 4; // axisCount is 4 because the chart is loaded with 3 axis + 1 for the navigator
  $button.click(function () {
        var axisObj = {
          title: {
              text: "axis-" + axisCount,
              id:'myaxis-'+ (index++)
          },
        };
		chart.addAxis(axisObj, false);
        
		var seriesData = new Object();
		seriesData.name = 'axis-' + axisCount;
    seriesData.yAxis = axisCount;
    seriesData.data = data1;
    seriesData.type = 'line';
    chart.addSeries(seriesData);
    chart.yAxis[axisCount].update({ height: '14%',top: (axisCount*15) + '%',offset:0 });
    axisCount++;
  });
    
    
  $delButton.click(function () {
  
    var delAxis = $('#delAxis').val();
    chart.yAxis[delAxis].remove();
    for(var i=delAxis;i<axisCount-1; i++){
      chart.yAxis[i].update({ height: '14%',top: (i*15) + '%',offset:0 });
    }
    axisCount--;
  });
});
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="http://code.highcharts.com/stock/highstock.js"></script>
    <script src="http://code.highcharts.com/stock/highcharts-more.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>

<button id="button" class="autocompare">Add yAxis</button><br>
Entrt yAxis index to delete:<input type='text' id="delAxis"/>
<button id="delbutton" class="autocompare">Delete yAxis</button>
<div id="container" style="height: 800px"></div>

0条回答
登录 后发表回答