highchartsjstockchart

How do I add a real gap in high stocks so it would be visible on the chart?


I have this chart

<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 300px"></div>

var seriesOptions = [],
    seriesCounter = 0,
    names = ['MSFT', 'AAPL', 'GOOG'];

/**
 * Create the chart when all data is loaded
 * @returns {undefined}
 */
function createChart() {

    Highcharts.stockChart('container', {

       plotOptions: {
            series: {
                gapSize: 5 * 24 * 3600 * 1000,
                gapUnit: 'relative'
            }
        },

        rangeSelector: {
            selected: 5
        },

        yAxis: {
            labels: {
                formatter: function () {
                    return (this.value > 0 ? ' + ' : '') + this.value + '%';
                }
            },
            plotLines: [{
                value: 0,
                width: 2,
                color: 'silver'
            }]
        },

        plotOptions: {
            series: {
                compare: 'percent',
                showInNavigator: true
            }
        },

        tooltip: {
            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
            valueDecimals: 2,
            split: true
        },

        series: seriesOptions
    });
}

$.each(names, function (i, name) {

    $.getJSON('https://www.highcharts.com/samples/data/' + name.toLowerCase() + '-c.json',    function (data) {

        if (i==0) {
            var first = [], last = [];
            first.push.apply(first, data.slice(0,1)[0]);
            last.push.apply(first, data.slice(0,1)[0]);
            first[0] = first[0] - 1900 * 24 * 3600 * 1000;
            last[0] =   last[0] - 130 * 24 * 3600 * 1000;
            data = [];
            data.push(first);
            data.push(last);
        }
        seriesOptions[i] = {
            name: name,
            data: data
        };

        // As we're loading the data asynchronously, we don't know what order it will arrive. So
        // we keep a counter and create the chart when all the data is loaded.
        seriesCounter += 1;

        if (seriesCounter === names.length) {
            createChart();
        }
    });
});

and as you can see there are three stocks shown. If you hover the chart with the mouse and go to the beginning you'll notice MSFT stock which has only 2 points and that's intentional. After MSFT there should be about 6 year gap, however on the chart it's shown in a few pixels.

How can I configure stockChart to show real gaps? In other words, I want to see the gap of 6 years so from 2005 till 2011 there will be empty space proportional to the whole chart?


Solution

  • The discussion in the comment section of the first answer reveals that OP wants to hide no-data periods only in some cases.

    The solution here might be to set ordinal to false (as @ewolden) suggested and use breaks instead:

      xAxis: {
          breaks: [{
          breakSize: 24 * 3600 * 1000,
          from: Date.UTC(2017, 0, 6),
          to: Date.UTC(2017, 0, 9)
        }],
        ordinal: false
      },
    
      series: [{
        data: [
          [Date.UTC(2017, 0, 2), 6],
          [Date.UTC(2017, 0, 3), 7],
          [Date.UTC(2017, 0, 4), 3],
          [Date.UTC(2017, 0, 5), 4],
          [Date.UTC(2017, 0, 6), 1],
          [Date.UTC(2017, 0, 9), 8],
          [Date.UTC(2017, 0, 10), 9],
          [Date.UTC(2017, 6, 1), 4],
          [Date.UTC(2017, 6, 2), 5]
        ]
    

    Example: http://jsfiddle.net/BlackLabel/ocg0dujg/

    In the above demo I was able to hide the weekend (7 and 8 Jan) and maintain the space between January and July.

    API reference: https://api.highcharts.com/highstock/xAxis.breaks