score:12

Accepted answer

updated

here is how you remove it jsfiddle

  function(chart) { // on complete

    chart.renderer.path(['m', 0, 0, 'l', 100, 100, 200, 50, 300, 100])
        .attr({
            'stroke-width': 2,
            stroke: 'red'
        })
        .add();
    $(":button").click(function(){
        $('path[d="m 0 0 l 100 100 200 50 300 100"]').remove() ;
         });
    });

remove path by id

jsfiddle

    function(chart) { // on complete

    chart.renderer.path(['m', 0, 0, 'l', 100, 100, 200, 50, 300, 100])
        .attr({
            'stroke-width': 2,
            stroke: 'red' ,
            id :'mypath'
        })
        .add();
    $(":button").click(function(){
        $("#mypath").remove() ;
                                        });
     });

score:0

for anyone using polymer or who just wants to do this using pure js, here's how to manage an object that will be created on load and then redrawn every time the chart updates:

http://jsfiddle.net/57xw879k/1/

the advantage of this is that it's added to the chart object, so you're not relying on the dom or any particular method of accessing it.

http://api.highcharts.com/highcharts/chart.events is also worth a read if you want to perform actions at different times.

html:

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

<button id="remove">
  remove label
</button>
<button id="add">
  add label
</button>

js:

const chart = highcharts.chart('container', {

  chart: {
    events: {
      render: function() {
        handlelabel(this)
        var label = this.renderer.label('the chart was just redrawn', 100, 120)
        .attr({
          fill: highcharts.getoptions().colors[0],
          padding: 10,
          r: 5,
          zindex: 8
        })
        .css({
          color: '#ffffff'
        })
        .add()
        settimeout(function () {
          label.fadeout()
        }, 1000)
      }
    }
  },
  title: {text: 'highcharts label actions'},
  xaxis: {
    categories: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
  },    
  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]
  }]    
})

function handlelabel(chart) {
    if (chart.mylabel) {
    chart.mylabel.destroy()
    addlabel(chart)
  } else {
    addlabel(chart)
  }
}

function addlabel(chart) {
  var point = chart.series[0].points[8];
  chart.mylabel = chart.renderer.label('max observation', 270, 50, 'callout', point.plotx + chart.plotleft, point.ploty + chart.plottop)
    .css({
        color: '#ffffff'
    })
    .attr({
      fill: 'rgba(0, 0, 0, 0.75)',
      padding: 8,
      r: 5,
      zindex: 6
    })
    .add()
}

function removelabel(chart) {
  !objectisempty(chart.mylabel) && chart.mylabel && chart.mylabel.destroy()
}

function objectisempty(obj) {
  return object.keys(obj).length === 0 && obj.constructor === object
}

document.getelementbyid('remove').addeventlistener('click', () => removelabel(chart))
document.getelementbyid('add').addeventlistener('click', () => addlabel(chart))

score:1

i have found that element.remove() works fine in chrome, but not when running in a webview on android, for example, and may therefore not work in other browser environments.

delving through the object's properties and methods, i found a saferemovechild() method, which seems to work even in a webview. so that's something along the lines of:

var path = chart.renderer.path(['m', 1200, 10, 'v', 1500, 0])
.attr({
    'stroke-width': 2,
    stroke: 'red'
})
.add();

// remove the path
path.saferemovechild(path.element);

score:15

for future googlers, this will work:

var path = chart.renderer.path(['m', 1200, 10, 'v', 1500, 0])
.attr({
    'stroke-width': 2,
    stroke: 'red'
})
.add();

// remove the path
path.element.remove();

Related Query

More Query from same tag