Jugal | Add plot lines on click | Highchart & Highstock

http://stackoverflow.com/questions/12061230/highcharts-keep-vertical-line-on-click-event Jugal Thakkar http://jugal.me/

by dirtyd77

HTML

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="http://jugal.me/code/js/jugalsLib.js" ></script>
<script type="text/javascript" src="http://code.highcharts.com/stock/highstock.src.js" ></script>
<script type="text/javascript" src="http://code.highcharts.com/stock/modules/exporting.src.js" ></script>

<div id="container"></div>

CSS

#container{    min-width:500px;
}

JavaScript

var myPlotLineId="myPlotLine";
var chartingOptions = {
    chart: {
        renderTo: 'container',
        events: {
            click: function(evt) {
                var xValue = evt.xAxis[0].value;
                var xAxis = evt.xAxis[0].axis;
                
                $.each(xAxis.plotLinesAndBands,function(){
                    if(this.id===myPlotLineId)
                    {
                        this.destroy();
                    }
                });
                xAxis.addPlotLine({
                    value: xValue,
                    width: 1,
                    color: 'red',
                    //dashStyle: 'dash',                   
                    id: myPlotLineId
                });

            }

        }
    }
};

chartingOptions = $.extend({}, jugalsLib.getBasicChartOptions(), chartingOptions);
var chart = new Highcharts.StockChart(chartingOptions);