Removing or Hiding a series in a chart via javascript

  • 1
  • Question
  • Updated 3 years ago
  • (Edited)
I am attempting to remove a series from a chart but can only find examples using a click action. 

I've tried using remove() but can't it won't recognize the function or the object no matter what I try.

I've been able to simply make the series invisible, but I'd love to be able actually remove it.
Here's the part of my javascript snippet for that.


var blankSeries = {       
        id:'TestBlank',
        showInLegend : false,
        series: {
            visible: false,
        }
};
    
chartObj.series[1] = blankSeries;
Photo of Josh Merritt

Josh Merritt

  • 918 Points 500 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Josh,

When are you trying to run this snippet. If you run a before-render snippet for the chart, you should be able to delete a series.

maybe:
delete chartObj.series[1];
Photo of Josh Merritt

Josh Merritt

  • 918 Points 500 badge 2x thumb
Thanks for the reply, Matt.

I am running this as a before-render snippet on the chart.

Here's part of the code where I tried adding in your snippet and the error that is thrown. My javascript skills are strictly amateur, so maybe I'm missing something 'obvious'. 

    var newSeries = {        
        id: 'Cumulative',
        name: 'Percent of ECN',
        data: cumulative,
        type: 'column',
        yAxis: chartObj.series[0].yAxis,
    };
    
    // replace first series with new cumulative series
    chartObj.series[0] = newSeries;  delete chartObj.series[1];


Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
A couple quesitons:
  • Does chartObj.series[1] exist? That assumes there are at least two series to start with. the first series you're replacing with 'newSeries' and the second you're trying to delete?
  • What part of the code is causing the error? (Expand the error to see the call stack and see if you can trace it back to any of your own code.