evann
evann

Reputation: 93

How to get current date and time on highcharts

I cannot get the current date and time to appear on the x axis of my chart. As of now it is just a random date and time. The price is loading in the chart correctly, but the date and time are not. Using highcharts. Any help is appreciated thanks.

    $(document).ready(function () {

    $.ajax({
        url: "/chart/ajax_get_chart", // the URL of the controller action method
        dataType: "json",
        type: "GET",
        success: function (result) {
            var result = JSON.parse(result);
            var date = new Array();
            var price = new Array();
            var d = new Date();
            series = [];
            for (var i = 0; i < result.length; i++) {
                tempArray = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
                tempArray = parseFloat(result[i]['price']);
                tempArray = [result[i]['date']*1000, parseFloat(result[i]['price'])];
                series.push(tempArray);
            }



            console.log(tempArray);
            // var now = new Date();

            Highcharts.setOptions({
                global: {
                    useUTC: false
                }
            });
            $('#container').highcharts({
                chart: {
                    type: 'line',
                },
                title: {
                    text: 'Bitcoin Price',
                },
                subtitle: {
                    text: 'Source: MtGox.com',
                },
                xAxis: {
                    type: 'datetime'
                },
                plotOptions: {
                    series: {
                        pointStart: d.getUTCDate()*1000,
                        pointInterval: 24 * 3600 * 1000 // one day
                    }
                },
                yAxis: {
                    title: {
                        text: 'Price'
                    },
                },

                series: [{
                    name: 'Bitcoin',
                    data: series
                }]
            });

        }
    });

});

Upvotes: 1

Views: 2927

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

Okay, you are using in the same time pointStart+pointInterval and [timestamp, value] - so this won't work. Remove that pointStart+pointInterval.

 tempArray = [result[i]['date'], parseFloat(result[i]['price'])];

Is yout date as timestamp in miliseconds (number, not string)?

Upvotes: 1

Related Questions