start xAxis on an arbitrary time in line chart - Javascript highcharts

Javascript examples for highcharts:Chart Axis

Description

start xAxis on an arbitrary time in line chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
function getTimes(numTimes, interval) {
    var ms = (new Date(2012, 02, 30, 10, 33)).getTime();
    var times = [];//from   w w w . j a v a  2 s .c om
    var startDate = new Date(ms);
    times.push(startDate.getHours() + ":" + startDate.getMinutes());
    for (var i = 1; i< numTimes; i++)
    {
        ms += interval;
        var nextTime = (new Date()).setTime(ms);
        var nextDate = new Date(nextTime);
        times.push(nextDate.getHours() + ":" + pad(nextDate.getMinutes(), 2));
    }
    return times;
}
function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}
var data = [1, 2, 3, 4, 5, 3, 2, 5, 7, 6, 4];
var interval = 10*60*1000
var timeCategories = getTimes(data.length, interval);
$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                zoomType: 'x',
                spacingRight: 20
            },
            title: {
                text: 'Time series'
            },
            xAxis: {
                categories: timeCategories,
                title: {
                    text: null
                },
                startOnTick: false
            },
            yAxis: {
                title: {
                    text: 'Exchange rate'
                },
                startOnTick: false,
                showFirstLabel: true
            },
            tooltip: {
                shared: true
            },
            legend: {
                enabled: false
            },
            series: [{
                type: 'line',
                name: 'time series',
                data: [
                    1, 2, 3, 4, 5, 3, 2, 5, 7, 6, 4
                ]
            }]
        });
    });
});

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <script src="https://code.highcharts.com/modules/exporting.js"></script> 
      <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>  
   </body>
</html>

Related Tutorials