Anagio
Anagio

Reputation: 3075

How to format the series data date value to compare with database field?

I am using highcharts with this code to display dates in the x-axis which it gets from the data series. I'd like to compare these dates against a field in a database, and if a match exists run through the formatter condition to display the icon where a match exists. How do I set the return value to match the date formats in the series? Thanks

See in jsfiddle

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
    },
    xAxis: {        
        type: 'datetime',
        labels: {
            formatter: function() {
                return Highcharts.dateFormat('%a %d %b', this.value);
                if (checkDatabase(this.value)) {
                    return '&nbsp;<img src="http://highcharts.com/demo/gfx/sun.png"/>';
                } else {
                    return '';
                }
            }
        }
    },

    series: [{
        data: [ [Date.UTC(2011, 11, 4), 21],[Date.UTC(2011, 11, 3), 27],[Date.UTC(2011, 10, 11), 19] ]
    }]
});

    function checkDatabase(val) {
    // Connect and query the db
    return val === 'Saturday, Dec 3, 2011';
}

Upvotes: 0

Views: 522

Answers (1)

eolsson
eolsson

Reputation: 12727

In the example the checkDatabase()is never invoked, there is a return statement just ahead of it. But I guess that is not the actual question here..

The === (strict equal operator) will not do type conversion, so what happens here is that the timestamp, e.g. Date.UTC(2011, 11, 4) (which is the number of milliseconds from 1970, see Date) is being compared with the string 'Saturday, Dec 3, 2011' and that will always return false.

For the comparison to work either turn both timestamps to numbers:

1322956800000 === Date.UTC(2011, 11, 4) // true

or compare the strings:

'Sun, 04 Dec 2011 00:00:00 GMT' === new Date(Date.UTC(2011, 11, 4)).toUTCString() // true

Upvotes: 1

Related Questions