dynamically remove the whole Column from a single series column chart - Javascript highcharts

Javascript examples for highcharts:Column Chart

Description

dynamically remove the whole Column from a single series column chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from w  w  w  . j  av  a  2  s  .co  m
const options = {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Monthly Average Rainfall'
  },
  subtitle: {
    text: 'Source: WorldClimate.com'
  },
  xAxis: {
    categories: [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec'
    ],
    crosshair: true
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Rainfall (mm)'
    }
  },
  tooltip: {
    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
      '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
    footerFormat: '</table>',
    shared: true,
    useHTML: true
  },
  plotOptions: {
    column: {
      pointPadding: 0.2,
      borderWidth: 0
    }
  },
  series: [{
    name: 'Tokyo',
    data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
  }]
}
// Run our method
const chart = Highcharts.chart('container', options)
removeCategoryAndData(chart, 'Feb')
removeCategoryAndData(chart, 'May')
function removeCategoryAndData (chart, categoryName) {
   const { categories } = chart.xAxis[0]
  const categoryIndex = categories.indexOf(categoryName)
  const { data } = chart.series[0]
  const filteredData = data.map((p) => p.y).filter((v, i) => i !== categoryIndex)
  const filterdCategories = categories.filter((v, i) => i !== categoryIndex)
  chart.update({ xAxis: { categories: filterdCategories }, series: { data: filteredData } })
}
    }

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <div id="container"></div>  
   </body>
</html>

Related Tutorials