Jugal | Getting Y value from X value in a series | Highchart & Highstock

http://stackoverflow.com/questions/12014412/highcharts-getting-y-value-in-one-series-based-on-x-value-from-another-series/12016845#12016845 Jugal Thakkar http://jugal.me/

by Jugal Thakkar

HTML

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js" ></script>
<script type="text/javascript" src="http://code.jugal.me/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

function getYValue(chartObj, seriesIndex, xValue) {
    var yValue = null;
    var points = chartObj.series[seriesIndex].points;
    for (var i = 0; i < points.length; i++) {
        if (points[i].x >= xValue) break;
        yValue = points[i].y;
    }
    return yValue;
}

var chartingOptions ={
    chart: {
        renderTo: 'container',
        events: {
            click: function(evt) {
                var seriesIndex = 1;
                var xAxisIndex = 0;
                // All you need is the index of the series that you want
                // And the index of the xAxis bound to that series
                // default to 0 if you have only one series
                alert(getYValue(this, seriesIndex, evt.xAxis[xAxisIndex].value));
            }

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