Highcharts Stock Demo

author(s): Torstein Hønsi

HTML

<div id="container" style="height: 400px; min-width: 600px"></div>
<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<script type="text/javascript" src="http://www.highcharts.com/samples/data/usdeur.js"></script>

JavaScript

$(function () {

    (function (H) {
        function collide(a, b) {
            return !(b.x > a.x + a.width || b.x + b.width < a.x || b.y > a.y + a.height || b.y + b.height < a.y);
        }

        H.wrap(H.seriesTypes.flags.prototype, 'drawPoints', function (p) {
            var series = this,
                chart = series.chart,
                overlap = true,
                counter = 0,
                index,
                offset = series.options.stackDistance,
                currentBBox,
                compareBBox,
                compareSeries;

            p.call(this);

            while (overlap && counter < 100) { // as long as flags do overlap, move them. Extra limiter up to 100 iterations.
                overlap = false;
                H.each(series.points, function (currentPoint) {
                    if (currentPoint.graphic) { // only existing point with label

                        index = 0;
                        currentBBox = {
                            x: currentPoint.graphic.translateX,
                            y: currentPoint.graphic.translateY,
                            width: currentPoint.graphic.width,
                            height: currentPoint.graphic.height
                        };

                        for (; series.index - index >= 0; index++) { // compare only with previous series

                            compareSeries = chart.series[index];

                            if (compareSeries.options.type === "flags") { // only flag type seires

                                H.each(compareSeries.points, function (comparePoint) { // compare current label with all others
                                    if (compareSeries === series && comparePoint.index >= currentPoint.index ) {
                                    	return; // perf
                                    }
                                    
                                    if (comparePoint.graphic) { // only existing point with...